在另一个包中获取包 类 个私有包
Get bundle classes of private packages in another bundle
我有两个 osgi 包 A 和 B。包 A 的所有包都是私有的。在 bundle B 中,我使用了 :
中的代码
捆绑包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,但 类 恰好在 bundle A 中 - 例如 ClassA 。如何在 ClassB 中获取属于 bundle 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/wiring/BundleWiring.html#findEntries(java.lang.String,%20java.lang.String,%20int)
我有两个 osgi 包 A 和 B。包 A 的所有包都是私有的。在 bundle B 中,我使用了 :
中的代码捆绑包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,但 类 恰好在 bundle A 中 - 例如 ClassA 。如何在 ClassB 中获取属于 bundle 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/wiring/BundleWiring.html#findEntries(java.lang.String,%20java.lang.String,%20int)