If I have
Reflections reflections = new Reflections("my.package", classLoader, new SubTypesScanner(false));
then this finds my enum classes
Set<Class<? extends Enum>> enums = reflections.getSubTypesOf(Enum.class);
but this doesn't
Set<Class<?>> classes = reflections.getSubTypesOf(Object.class);
Is there a reason for this?
Reproducible example:
package cupawntae;
import org.reflections.Reflections;
import org.reflections.scanners.SubTypesScanner;
public class Reflector {
public static void main(String[] args) {
Reflections reflections = new Reflections("cupawntae", Reflector.class.getClassLoader(), new SubTypesScanner(false));
System.out.println("Objects: " + reflections.getSubTypesOf(Object.class));
System.out.println("Enums: " + reflections.getSubTypesOf(Enum.class));
System.out.println("Enum's superclass: " + Enum.class.getSuperclass());
}
}
Enum class:
package cupawntae;
public enum MyEnum {
}
Output:
Objects: [class cupawntae.Reflector]
Enums: [class cupawntae.MyEnum]
Enum's superclass: class java.lang.Object
This is actually documented behaviour, although it's arguably not particularly clear or intuitive:
edit: later revision of that doc says:
In this case
java.lang.Enum
counts as a transitive class (likeother.package.OtherClass
), and is therefore not included in the scan, meaning subclasses ofEnum
are not included.Similarly, if we make
Reflections
in the question's example extend something outside the target package, e.g.then the class is no longer found in the scan