user5182503
user5182503

Reputation:

OSGI: How object is created if used classes are in private packages

Let's suppose we have two bundles A and B.

Bundle A private package:

class ClassA extends ClassB{}

Bundle B - exported package:

class ClassB{
  private ClassC classC=new ClassC();
}

Bundle B - private package:

class ClassC{}

When I create instance of ClassA in bundle A it is successfully created. But why? We do need ClassC but bundle A classloader doesn't see it.

Upvotes: 1

Views: 112

Answers (1)

Christian Schneider
Christian Schneider

Reputation: 19606

Very good question. It actually took me some years until I figured out how it works though it is really simple after all.

When you do new ClassA() then you will use the classloader of the class your are currently in. This classloader knows class A either because it is in the same bundle or it is imported. If it is in the same bundle then this classloader is used. If it is in another bundle the class loading is delegated to the other bundle's classloader.

So we assume A is loaded by classloader of bundle A. It needs class B which is in an imported package. So the creation of B is delegated to the classloader of B. This means class B is loaded by classloader of bundle B. All code inside B is using the classloader of class B. So the creation of class C is done using the classloader of bundle B too. This is why it works.

Upvotes: 3

Related Questions