【发布时间】: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 的类?
【问题讨论】: