如何在 OSGi Bundle 中创建/初始化 Javassist ClassPool?
我在 Eclipse 中创建了一个 Bundle,并使用“Overview”选项卡的“Launch the Framework”链接。我在我的包中创建了一个接口(TestService),以及应该为其生成代理的代码(在 Helper 中)。我在 Bundle 的 Activator 中调用这段代码,并得到:
Caused by: javassist.NotFoundException: com.test.services.TestService
at javassist.ClassPool.get(ClassPool.java:436)
at com.test.services.Helper.get(Helper.java:46)
它在第一个 ClassPool.get() 处抛出:
ClassPool pool = ClassPool.getDefault();
CtClass result = pool.get(TestService.class.getName());
...
TestService 是公共的并且在同一个包中,而 Bundle 作为 Helper,它生成异常。显然,仅仅执行“ClassPool.getDefault()”还不够好。那么我需要做什么才能让 ClassPool 看到 Bundle 中的类呢?我必须导入自己的包吗?
I created a Bundle in Eclipse, and used the "Launch the Framework" link for the "Overview" tab. I have created an interface (TestService) in my bundle, and code that should generate a proxy for it (in Helper). I call this code in the Activator of the Bundle, and get:
Caused by: javassist.NotFoundException: com.test.services.TestService
at javassist.ClassPool.get(ClassPool.java:436)
at com.test.services.Helper.get(Helper.java:46)
It throws right at the first ClassPool.get():
ClassPool pool = ClassPool.getDefault();
CtClass result = pool.get(TestService.class.getName());
...
TestService is public and in the same package, and Bundle, as the Helper, which generates the Exception. Obviously, it's not good enough to just do "ClassPool.getDefault()". So what do I need to do so that ClassPool sees the classes inside the Bundle? Do I have to import my own packages?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果“源”是由 OSGi 包类加载器加载的对象,您可以将相应的“类路径”(或类加载器)添加到 ClassPool,如下所示:
或者在您的情况下只是
希望它有所帮助。
If "source" is an object loaded by an OSGi bundle classloader, you can the corresponding "classpath" (or classloader) to the ClassPool, like this:
or in your case simply
Hope it helps.
我在一些 OSGi 包中使用 javassist 来动态修改/生成 Java 类。你可以看看 这里。希望对您有帮助。
I use javassist in some OSGi bundles to modify/generate Java classes on the fly. You can just look here. Hope it helps you.