我有一个Eclipse包,想要从这个包中获取所有类的列表。
我该怎么做?
答案 0 :(得分: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);
}
}
将className转换为类类型:
bundle.loadClass(className);