public void onWrite(ClassPool pool, String className) throws NotFoundException, CannotCompileException { CtClass cc = pool.get(className); try { if (isPersistent(className)) { CtClass base = cc.getSuperclass(); CtConstructor cons = new CtConstructor(constructorParams, cc); if (base.subclassOf(persistent) || base == object) { cons.setBody(null); cc.addConstructor(cons); if (base == object) { cc.setSuperclass(persistent); } } else { if (!isPersistent(base.getName())) { throw new NotFoundException( "Base class " + base.getName() + " was not declared as persistent"); } cons.setBody("super($0);"); cc.addConstructor(cons); } preprocessMethods(cc, true, true); if (base == persistent || base == object) { CtMethod m = new CtMethod(isRecursive, cc, null); m.setBody("return false;"); cc.addMethod(m); addSerializeMethods(cc, false); } else if (base.subtypeOf(serializable)) { addSerializeMethods(cc, true); } if ((cc.getModifiers() & Modifier.PRIVATE) == 0) { CtClass f = pool.makeClass(className + "LoadFactory"); f.addInterface(factory); CtMethod c = new CtMethod(create, f, null); c.setBody("return new " + className + "($1);"); f.addMethod(c); CtNewConstructor.defaultConstructor(f); } } else { preprocessMethods( cc, cc.subtypeOf(persistent) && cc != persistent, !className.startsWith("org.nachodb")); } } catch (Exception x) { x.printStackTrace(); } }
public void testConstBody() throws Exception { CtClass cc = sloader.get("test2.ConstBody"); CtConstructor cons = new CtConstructor( new CtClass[] {sloader.get("java.lang.String"), sloader.get("java.lang.Integer")}, cc); cons.setBody("super((String)$1, (Integer)$2);"); cc.addConstructor(cons); cc.writeFile(); Object obj = make(cc.getName()); assertEquals(1, invoke(obj, "bar")); }
public void testStaticArrays() throws Exception { CtClass cc = sloader.makeClass("StaticArrays"); CtField f = new CtField(sloader.get("test2.StaticArraysMem[]"), "myStaticField", cc); f.setModifiers(Modifier.STATIC); cc.addField(f); CtConstructor init = cc.makeClassInitializer(); String body = "{\n"; body += ("myStaticField = new test2.StaticArraysMem[2];\n"); body += ("\n}"); init.setBody(body); }