2 Replies Latest reply on Aug 17, 2005 8:14 AM by m morel

    initialization of static arrays

    m morel Newbie


      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);

      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}");

      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,