Search code examples
javaosgiclassloaderbnd

OSGi classloading: why does BND imports classes that are not referenced directly?


Bundle-A:

FooA.java

package com.foo.a;

import com.foo.b.FooB;

class FooA {
    FooB b = new FooB();
}

Bundle-B:

FooB.java:

package com.foo.b;

import com.foo.c.FooC;

class FooB {

    public FooC foo() {
       ...
    }
}

Bundle-C: ...

So in short, I have 3 bundles - A, B and C.

Bundle A references bundle B directly, and Bundle B references C. As you see, FooA doesn't use the method in FooB that returns FooC, so FooC is not referenced in bundle A directly.

Why is BND including the OSGi import-package to com.foo.c then? The way I understand it - bundle A only needs bundle B in order to be able to resolve itself. Bundle B on the other hand needs C. But why should A require C directly, if it's not used there?


Solution

  • I think that bnd imports all classes that are visible to the outside for classes you use. As you use class FooB you might need access to all classes it may need as parameters or return as results.

    If you want to avoid the dependency you can create an interface that only shows the methods you really need. You can then create a service with that interface in bundle B and only access the service using the interface from bundle A.