Javassist: How do I add dynamically generated classes to a specific package?
I am generating classes that need to access protected fields from other existing 开发者_如何学Cclasses. Because of this, I am in need of specifying the classpath that it should end up in.
Does anyone know how to do this in Javassist?
An old question but I have come across the same problem. The solution is to use full class name, including the package, when creating the class.
ClassPool pool = ClassPool.getDefault();
String packageName = "yourpackage.";
String className = "NameOfTheClass";
CtClass dynamicClass = pool.makeClass(package+className);
This way you will be able to access protected fields of classes from the given package.
Make sure the generated classes are put into the same package as those that declare the protected members. Specifying 'the classpath that it should end up in' shouldn't really come into it, unless it is caused by classloading issues.
精彩评论