Hello,

I would like to generate a class with a static field which is an array :

  | CtClass generatedClass = pool.makeClass("TestClass");
  | CtField myStaticField = new 
CtField(pool.get(MyClass[].class.getComponentType().getName()), 
"myStaticField", generatedClass);
  | myStaticField.setModifiers(Modifier.STATIC);
  | generatedClass.addField(myStaticField);
  | 

I want to initialize the array when loading the class (in a static block):

  | CtConstructor classInitializer = generatedClass.makeClassInitializer();
  | String classInitializerBody = "{\n";
  | classInitializerBody += ("myStaticField = new MyClass[2];\n");
  | classInitializerBody += ("\n}");
  | classInitializer.setBody(classInitializerBody);
  | 

Unfortunately, I get the following error :

  | javassist.CannotCompileException: [source error] incompatible type for =
  |     at javassist.CtBehavior.setBody(CtBehavior.java:194)
  |     at javassist.CtBehavior.setBody(CtBehavior.java:163)
  |     at javassist.CtConstructor.setBody(CtConstructor.java:201)
  |     at test.Main.main(Main.java:41)
  | 

It seems that I get this error for any type of table, including tables on 
classes from the jdk, except for Object[].

My question is : is it impossible to initialize static arrays in static blocks 
with Javassist? Am I doing it the wrong way? Is there a walkaround?

Thanks for any help,


Matthieu

View the original post : 
http://www.jboss.org/index.html?module=bb&op=viewtopic&p=3888904#3888904

Reply to the post : 
http://www.jboss.org/index.html?module=bb&op=posting&mode=reply&p=3888904


-------------------------------------------------------
SF.Net email is Sponsored by the Better Software Conference & EXPO
September 19-22, 2005 * San Francisco, CA * Development Lifecycle Practices
Agile & Plan-Driven Development * Managing Projects & Teams * Testing & QA
Security * Process Improvement & Measurement * http://www.sqe.com/bsce5sf
_______________________________________________
JBoss-user mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/jboss-user

Reply via email to