使用 Javassist 编译的类的类路径

发布于 2024-09-15 02:17:49 字数 134 浏览 1 评论 0原文

正如标题所示,用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

清音悠歌 2024-09-22 02:17:49

类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 of A must be a parent (or parent of parent, or ...) of the class loader of class B.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文