问题
I have an Eclipse bundle and want to get a list of all classes from this bundle.
How can I do it?
回答1:
The name of the classes from the bundle will be in classNameOfCurrentBundle list in the end:
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);
}
}
Converting the className to class type:
bundle.loadClass(className);
来源:https://stackoverflow.com/questions/22688997/how-i-can-get-list-of-all-classes-from-given-bundle