0

We have 2 different OSGI bundles.

In first bundle exist class which parses JSON string to MyClass.MyClass annotated by Gson annotations.MyClass located in Second bundle. I have a lot of problem with it. Eventually in debug mode I have noticed that

MyClass.class.getDeclaredField("fieldName").getAnnotation(AnnotationType.class)

returns null.

Hence CQ5 somewhere losed the annotations.

I created absolutely new project, copy parser class and MyClass to this. This code really works normally.

Eventually we have understood that 'MyClass' and Gson was loaded by different classloaders and after we began load Gson same classloader and problem has been fixed. But it is very clumsy solution.

What do you think about it?

How does it fix it more elegant?

gstackoverflow
  • 36,709
  • 117
  • 359
  • 710

1 Answers1

0

There are two places from which your bundles can import the GSON dependency:

  1. it can be put into the OSGi container as a standalone bundle,
  2. it can be embedded.

In order to find out which is true in your case, open the bundle in Apache Felix console and look for com.google.gson in the Imported Packages section. If it looks like this:

com.google.gson,version=2.2.4 from com.google.gson (343)

it means that your bundle imports GSON from standalone bundle 343 (case 1). On the other hand, if there is no such entry, but you can find GSON jar name:

gson-2.2.4.jar

in the Bundle Classpath, it means that you are embedding GSON into your bundle (case 2).

You found out that the cause of the problem is that GSON library in bundle1 and bundle2 is loaded by different class loaders. It means that at least one bundle uses the embedded version of the library (case 2) rather than the standalone (case 1). In order to fix this, you need to review <Embed-Depdency> directive of maven-bundle-plugin in pom.xml and remove gson from there. You may also try changing the scope of gson in your <dependencies> section to provided.

Tomek Rękawek
  • 9,204
  • 2
  • 27
  • 43