使用 Javassist 编译的类的类路径
正如标题所示,用Javassist编译的类的类路径是什么?
我的场景是:A类是用Javassist编译的。 B 类使用 Java 编译器 API 进行编译并引用 A 类。问题是 A 类对 B 类不可见...
有什么想法吗?
As the title suggests, what is the classpath of classes compiled with Javassist?
My scenario is: Class A is compiled with Javassist. Class B is compiled with Java Compiler API and references Class A. The problem is that Class A is not visible to Class B...
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
类B的“类路径”并不取决于该类是如何编译的(例如创建类文件),而是取决于该类是如何加载的。
Java 使用类的类加载器来查找该类引用的所有类。
每个类加载器都可以有一个父类加载器,通常 loader.loadClass 首先向其父类加载器询问任何类,并在失败的情况下尝试加载类本身。
因此,只需确保
A
类对于类 B 的类加载器可见,这意味着A
的类加载器必须是父类(或父类的父类,或...) 类B
的类加载器。The "classpath" of class B does not depends on how the class is compiled (e.g. the class file is created), but how the class is loaded.
Java uses the class loader of a class to look up also all classes which are referenced by this class.
Each class loader can have a parent class loader, and normally
loader.loadClass
first asks its parent class loader about any classes, and in case of failure tries to load the class itself.Thus, simply make sure the
A
class is visible to the class loader of class B, meaning that the class loader ofA
must be a parent (or parent of parent, or ...) of the class loader of classB
.