Why doesn't reflections.getSubTypesOf(Object.c

2020-08-24 06:37发布

问题:

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

回答1:

This is actually documented behaviour, although it's arguably not particularly clear or intuitive:

be aware that when using the constructor new Reflections("my.package"), only urls with prefix 'my.package' will be scanned, and any transitive classes in other urls will not be scanned (for example if my.package.SomeClass extends other.package.OtherClass, than the later will not be scanned). in that case use the other constructors and specify the relevant packages/urls

edit: later revision of that doc says:

Make sure to scan all the transitively relevant packages. for instance, given your class C extends B extends A, and both B and A are located in another package than C, when only the package of C is scanned - then querying for sub types of A returns nothing (transitive), but querying for sub types of B returns C (direct). In that case make sure to scan all relevant packages a priori.

In this case java.lang.Enum counts as a transitive class (like other.package.OtherClass), and is therefore not included in the scan, meaning subclasses of Enum are not included.

Similarly, if we make Reflections in the question's example extend something outside the target package, e.g.

public class Reflector extends Exception {

then the class is no longer found in the scan

Objects: []
Enums: [class cupawntae.MyEnum]
Enum's superclass: class java.lang.Object