The reason for this is that
ref.getSubtypesOf(Object.class);
returns only direct subclasses of the object. If you want to get all classes from a scanned package, you must do the following:
Reflections ref = new Reflections(new ConfigurationBuilder().setScanners(new SubTypesScanner(false), new ResourcesScanner(), new TypeElementsScanner()) ... Set<String> typeSet = reflections.getStore().getStoreMap().get("TypeElementsScanner").keySet(); HashSet<Class<? extends Object>> classes = Sets.newHashSet(ReflectionUtils.forNames(typeSet, reflections .getConfiguration().getClassLoaders()));
This may look a bit hacky, but this is the only way I've found so far. Here's a little explanation of what this does: When Reflections is done with a scan, it puts all the elements in a map with multiple values. In the sample code that I inserted, the results are placed inside the map with the following keys:
SubTypesScanner, ResourcesScanner, TypeElementsScanner
ResourceScanner excludes all files ending with .class. TypeElementsScanner is a map with a key containing the name of the class, and the value of the fields, etc. Therefore, if you want to get only class names, you basically get a set of keys and later, convert it to a set if classes. SubTypesScanner is also a map with the key of all superclasses (including Object and interfaces) and values ββ- classes that implement / extend these interfaces / classes.
You can also use SubTypesScanner, if you like, by iterating over the key set and getting all the values, but if a particular class implements an interface, you will have to deal with duplicate objects (as each class extends Object).
Hope this helps.
source share