How I can get list of all classes from given bundle

老子叫甜甜 提交于 2019-12-06 11:54:16

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!