javassist

Loading external .class files with Javassist


I have a directory called 'TestDir' which contains several external .class files that i would like to load and modify at runtime with JAssist.

I understand this is how you are supposed to load external classes with javassist:

    ClassPool pool = ClassPool.getDefault();
    
    pool.insertClassPath("C:\\Users\\MainPC\\Documents\\TestDir");
    CtClass clazz = pool.getCtClass("TestClass");

This works without throwing any exceptions, however the moment try to call any methods of 'clazz', like for example:

    System.out.println(clazz.getGenericSignature());

I receive the following exception:

Exception in thread "main" java.lang.RuntimeException: cannot find TestClass: TestDir.TestClass found in TestClass.class
at javassist.CtClassType.getClassFile3(CtClassType.java:211)
at javassist.CtClassType.getClassFile2(CtClassType.java:178)
at javassist.CtClassType.getGenericSignature(CtClassType.java:379)
at ReflectionTests.main(ReflectionTests.java:33)

Can someone explain to me why this happens?


Solution

  • I did something like this javassist, and as far as I remember, the package name is part of the class file. That means, your code needs to be changed to:

    ClassPool pool = ClassPool.getDefault();
        
    pool.insertClassPath("C:\\Users\\MainPC\\Documents");
    CtClass clazz = pool.getCtClass("TestDir/TestClass");