Further to the question posted here: Can you find all classes in a package using reflection? I started using the Reflections library to find all classes that subclass a given type. The source code looks like this, from an answer to the linked SO question:
Reflections ref = new Reflections(new ConfigurationBuilder()
.setScanners(new SubTypesScanner(false /* don't exclude Object.class */), new ResourcesScanner())
.setUrls(ClasspathHelper.forPackage("org.somepackage"))
.filterInputsBy(new FilterBuilder().include(FilterBuilder.prefix("org.somepackage"))));
ref.getSubtypesOf(Object.class);
However, after using this code obliviously for a while, I’ve just discovered that it will only find classes that subclass another type within this package. It won’t find classes that subclass externally defined classes, say from another user-defined package.
I’m not sure how to get around this using the Reflections library. I want all classes that declare their package as ‘org.somepackage’, regardless of what their supertype is. Any help?
I wrote a library called Rebound (as opposed to Reflections) which searches for the subclasses of a given type and package prefix. If you set the prefix empty, it will search every class under the classpath, e.g.
But you should be careful, because searching everything in the classpath is a slow process.
The library is much simpler than Reflections and might not do what you want. I wrote this due to my frustration when I submitted my bug report but no one there tries to solve the problem.