获取另一个包中的私有包的包类 [英] Get bundle classes of private packages in another bundle

查看:151
本文介绍了获取另一个包中的私有包的包类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have two osgi bundles A and B. All packages of bundle A are private. In bundle B I used the code from 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);
    }
}

但是,我得到了资源-捆绑了一个类加载器的所有类.但是我只需要捆绑软件A中的类.我的意思是我需要属于捆绑软件A的类.按照我的理解,要获取它们,我需要localResource.但是,localResource始终为null,但是这些类完全在bundle A中-例如ClassA.如何获取ClassB中属于捆绑软件A的类?

However, I get resources - all classes which bundle A classloader loaded. But I need only the classes which are inside bundle A. I mean I need classes which belong to bundle A. To get them as I understand I need localResource. However,localResource is always null, but the classes are exactly in bundle A - for example ClassA . How to get classes which belong to bundle A in ClassB?

推荐答案

代替

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

使用

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

第二个功能仅在特定的包及其片段包中查找资源.请参阅Javadoc:

The second function only looks for resources in the specific bundle and in its fragment bundles. See the javadoc: https://osgi.org/javadoc/r4v43/core/org/osgi/framework/wiring/BundleWiring.html#findEntries(java.lang.String,%20java.lang.String,%20int)

这篇关于获取另一个包中的私有包的包类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆