It would seem the answer is obvious:
(new Reflections("package")).getSubTypesOf(SuperClass.class) However, this construction does not find all the heirs, if the hierarchy is separated into several packages.
There are the following classes:
package1.ParentClass extemds SuperClass package2.ChildClass extends ParentClass Packages package1 and package2 not nested.
Design
(new Reflections("package1")).getSubTypesOf(SuperClass.class) Expected return package1.ParentClass
But the design
(new Reflections("package2")).getSubTypesOf(SuperClass.class) Our package2.ChildClass will not return, although it is an obvious successor to SuperClass .
I understand. I can manually run through all the classes in the package and collect the necessary ones. But is there really no standard way?
And yet, explain why the default ChildClass not located? For me, this was a very unexpected behavior from org.reflections.Reflections .
UPD : I studied the sources of the org.reflections kernel, everything fell into place. In short, java implements Reflections exactly like this. Each class in the reflection has only a link to the parent. Therefore, the search for subclasses is brute force anyway. Why in this search there are no super classes from other packages - a separate question. So implemented.
For each specific problem you need to use the optimal solution. But I would like to hear about existing solutions that allow you to find all descendants, except java org.reflections.
UPD : Another interesting and easy way.
Reflections reflections = new Reflections("package1", new SubTypesScanner()) Set<Class<?>> subTypes = new HashSet<>(); for (String className : reflections.getStore().get(SubTypesScanner.class.getSimpleName()).values()) { try { Class subType = Class.forName(className); if (SuperClass.class.isAssignableFrom(subType)) { subTypes.add(subType); } } catch (ClassNotFoundException e) { throw new RuntimeException("Этого не может быть:)", e); } } return subTypes; This solution enumerates all classes of the package (except for object heirs)
reflections.getStore().get(SubTypesScanner.class.getSimpleName()).values() and checks to see if they are the heir or implement SuperClass
SuperClass.class.isAssignableFrom(subType) By performance, at least, better than searching all the packages and subsequent filtering