【问题标题】:Get bundle classes of private packages in another bundle在另一个包中获取私有包的包类
【发布时间】:2016-06-18 09:27:03
【问题描述】:

我有两个 osgi 包 A 和 B。包 A 的所有包都是私有的。在包 B 中,我使用了来自 https://stackoverflow.com/a/22800118/5057736:

的代码

捆绑A

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,但这些类恰好在包 A 中 - 例如 ClassA 。如何在 ClassB 中获取属于 bundle A 的类?

【问题讨论】:

    标签: java osgi


    【解决方案1】:

    代替

    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/wiring/BundleWiring.html#findEntries(java.lang.String,%20java.lang.String,%20int)

    【讨论】:

    • 我认为更重要的问题是:@JimJim2000 到底想做什么??
    • 可能是一些字节码操作、编织和 AOP。在 OSGi 中应该避免的所有事情。
    • 嗯...其他问题表明他没有听说过服务。从this question听起来他正在使用反射直接从其他包中访问类?我不禁想起来。
    猜你喜欢
    • 1970-01-01
    • 2012-06-14
    • 2014-09-26
    • 2018-11-08
    • 1970-01-01
    • 1970-01-01
    • 2019-06-19
    • 2017-01-04
    • 2020-12-03
    相关资源
    最近更新 更多