This content has been marked as final.
Show 2 replies
-
1. Re: Modify static block
yamazaki May 3, 2003 12:12 PM (in response to ikestrel)This answer is very tricky. I hope getClassInitializer() doesn't returns null but returns an empty CtConstructor.
ClassPool pool = ClassPool.getDefault();
CtClass clazz = pool.makeClass("A");
CtField field = new CtField(CtClass.intType, "dummy", clazz);
field.setModifiers(field.getModifiers() | Modifier.STATIC);
clazz.addField(field, "0");
clazz.writeFile();
clazz.defrost();
clazz.getClassInitializer().insertAfter("System.out.println(\"hello!\");");
clazz.writeFile(); -
2. Re: Modify static block
chiba May 6, 2003 11:05 AM (in response to ikestrel)getClassInitializer() should return non-null if there
is a class initializer. I'll add a method for creating
a new class initializer if there is not. Please wait
for the next release, which will come soon.