1 Reply Latest reply on May 10, 2003 8:14 PM by chiba

    Several problems with Javassist

    earwin

      I am making an Object tree - to - SAX event stream converter to use as a view in my mvc framework, and stumbled into two problems.

      First one.

      ------------------------------------------------------
      java.lang.IllegalAccessException: Class com.promtech.commons.o2x.O2XWriter can not access a member of class com.promtech.commons.o2x.com_promtech_tryouts_TestO2X_TestOne__handler with modifiers "public"
      at sun.reflect.Reflection.ensureMemberAccess(Reflection.java:57)
      at java.lang.Class.newInstance0(Class.java:300)
      at java.lang.Class.newInstance(Class.java:259)
      at com.promtech.commons.o2x.O2XWriter.createHandler(O2XWriter.java:140)
      at com.promtech.commons.o2x.O2XWriter.getRegisteredHandler(O2XWriter.java:121)
      at com.promtech.commons.o2x.O2XWriter.parseClass(O2XWriter.java:96)
      at com.promtech.tryouts.TestO2X.main(TestO2X.java:18)
      ------------------------------------------------------

      Exception pops when i create a new class with Javassist, load it with Loader object and call
      ------------------------------------------------------
      hnd = handlerClass.newInstance();
      ------------------------------------------------------

      As a workaround i can use:
      ------------------------------------------------------
      Constructor cnt = handlerClass.getConstructors()[0];
      cnt.setAccessible(true);
      hnd = cnt.newInstance(new Object[] {});
      ------------------------------------------------------

      But that is still ugly way..

      Second one.

      I have an abstract class, which i copy to a new class, implement abstract methods and set new classes superclass to the original abstract class:
      ------------------------------------------------------
      newClass = pool.getAndRename(baseClassName, newClassName);
      handlerBase = pool.get(baseClassName);
      newClass.setSuperclass(handlerBase);
      .....
      initializer = pool.getMethod(newClassName, "initialize");
      writeInstance = pool.getMethod(newClassName, "writeInstance");

      initializer.setBody(initializerBody);
      writeInstance.setBody(methodBody);

      newClass.setModifiers(Modifier.PUBLIC & Modifier.FINAL);
      ------------------------------------------------------
      i then use workaround mentioned in the first section
      and create an instance of my new class, then i try to cast it to a superclass so i can use overriden method:

      ------------------------------------------------------
      handler = (ClassHandler) hnd;
      ------------------------------------------------------
      that line of code fails with ClassCastException

      if you need more of my code to answer my question, i can surely post it here