如何从给定的bundle中获取所有类的列表

dza*_*zav 2 eclipse osgi equinox

我有一个Eclipse包,想要从这个包中获取所有类的列表.

我该怎么做?

Bal*_*dos 5

捆绑包中的类名称最后将在classNameOfCurrentBundle列表中:

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);
    }
}
Run Code Online (Sandbox Code Playgroud)

将className转换为类类型:

bundle.loadClass(className);
Run Code Online (Sandbox Code Playgroud)