在另一个bundle中获取私人软件包的捆绑类



我有两个OSGI捆绑包A和B。捆绑包A的所有软件包都是私有的。在捆绑中,我从https://stackoverflow.com/a/22800118/5057736使用了代码:

bundlea

class ClassA extends ClassB{
    ClassA(){
      super(ClassA.class);
    }
}

捆绑b

class ClassB{
...
public void doFoo(){
{ 
Bundle bundle = FrameworkUtil.getBundle(ClassAReference);  
BundleWiring bundleWiring = bundle.adapt(BundleWiring.class);
// Getting all the class files (also from imported packages)
Collection<String> resources = bundleWiring.listResources("/", "*.class", BundleWiring.LISTRESOURCES_RECURSE);
List<String> classNamesOfCurrentBundle = new ArrayList<String>();
for (String resource : resources) {
    URL localResource = bundle.getEntry(resource);
    // Bundle.getEntry() returns null if the resource is not located in the specific bundle
    if (localResource != null) {
        String className = resource.replaceAll("/", ".");
        classNamesOfCurrentBundle.add(className);
    }
}

但是,我获得了资源 - 所有捆绑加载类的类。但是我只需要捆绑捆绑中的类的类。我的意思是我需要属于捆绑的类。要获得它们,因为我知道我需要localresource。但是,LocalResource始终是无效的,但是这些类正好在捆绑中 - 例如Classa。如何获得属于classB中捆绑A的类?

而不是

Collection<String> resources = bundleWiring.listResources("/", "*.class", BundleWiring.LISTRESOURCES_RECURSE);

使用

List<URL> resources = bundleWiring.findEntries("/", "*.class", BundleWiring.FINDENTRIES_RECURSE);

第二个功能仅在特定捆绑包及其碎片束中寻找资源。请参阅Javadoc:https://osgi.org/javadoc/r4v43/core/org/osgi/framework/framework/wiring/bundlewiring.html#findentries(Java.java.lang.lang.string,sctring,;p>

最新更新