Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 classes. 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?

like image 294
user321605 Avatar asked Nov 14 '22 20:11

user321605


1 Answers

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.

like image 53
B.J. Smegma Avatar answered Dec 18 '22 05:12

B.J. Smegma