0 Replies Latest reply on May 16, 2006 10:05 AM by Yanic Inghelbrecht

    on-the-fly generation of recursively defined classes

    Yanic Inghelbrecht Newbie

      What is the reason why classes with a recursive definition cannot be generated?

      For example, the call to 'toClass()' in the following test case produces the exception stacktrace below :

      import javassist.CannotCompileException;
      import javassist.ClassPool;
      import javassist.CtField;
      import javassist.CtNewClass;
      import javassist.NotFoundException;
      import junit.framework.TestCase;
      
      public class TestRecursiveClass extends TestCase {
       public void test() throws NotFoundException, CannotCompileException {
       ClassPool pool=ClassPool.getDefault();
      
       CtNewClass recursiveClass = new CtNewClass("RecursiveClass", pool , false, pool.get("java.lang.Object"));
       CtField f=new CtField(recursiveClass, "instance", recursiveClass);
       recursiveClass.addField(f, "null");
      
       recursiveClass.toClass();
       }
      }
      


      Exception stacktrace :
      javassist.CannotCompileException: by javassist.CannotCompileException: cannot find RecursiveClass
      at javassist.ClassPool.toClass(ClassPool.java:816)
      at javassist.ClassPool.toClass(ClassPool.java:765)
      at javassist.CtClass.toClass(CtClass.java:985)
      at TestRecursiveClass.test(TestRecursiveClass.java:16)
      

      (rest of stack trace omitted)