Pregunta

I have an Eclipse bundle and want to get a list of all classes from this bundle.

How can I do it?

¿Fue útil?

Solución

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);
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top