Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you create/initialize Javassist ClassPool inside a OSGi Bundle?

Tags:

classpath

osgi

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?

like image 938
Sebastien Diot Avatar asked Sep 19 '11 13:09

Sebastien Diot


2 Answers

If "source" is an object loaded by an OSGi bundle classloader, you can the corresponding "classpath" (or classloader) to the ClassPool, like this:

pool.insertClassPath(new ClassClassPath(source.getClass()));

or in your case simply

pool.insertClassPath(new ClassClassPath(TestService.class));

Hope it helps.

like image 66
apanday Avatar answered Sep 30 '22 18:09

apanday


I use javassist in some OSGi bundles to modify/generate Java classes on the fly. You can just look here. Hope it helps you.

like image 27
Dmytro Pishchukhin Avatar answered Sep 30 '22 18:09

Dmytro Pishchukhin