为什么 reflections.getSubTypesOf(Object.class) 找不到枚举?
Why doesn't reflections.getSubTypesOf(Object.class) find enums?
如果我有
Reflections reflections = new Reflections("my.package", classLoader, new SubTypesScanner(false));
然后这会找到我的枚举 classes
Set<Class<? extends Enum>> enums = reflections.getSubTypesOf(Enum.class);
但这不是
Set<Class<?>> classes = reflections.getSubTypesOf(Object.class);
这有什么原因吗?
可重现的例子:
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());
}
}
枚举class:
package cupawntae;
public enum MyEnum {
}
输出:
Objects: [class cupawntae.Reflector]
Enums: [class cupawntae.MyEnum]
Enum's superclass: class java.lang.Object
这实际上是 documented behaviour,尽管可以说它不是特别清楚或直观:
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
编辑:该文档的 later revision 说:
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.
在这种情况下,java.lang.Enum
被视为可传递的 class(如 other.package.OtherClass
),因此不包括在扫描中,这意味着 class 是 Enum
不包括在内。
类似地,如果我们在问题示例中使 Reflections
扩展目标包之外的内容,例如
public class Reflector extends Exception {
然后 class 不再在扫描中找到
Objects: []
Enums: [class cupawntae.MyEnum]
Enum's superclass: class java.lang.Object
如果我有
Reflections reflections = new Reflections("my.package", classLoader, new SubTypesScanner(false));
然后这会找到我的枚举 classes
Set<Class<? extends Enum>> enums = reflections.getSubTypesOf(Enum.class);
但这不是
Set<Class<?>> classes = reflections.getSubTypesOf(Object.class);
这有什么原因吗?
可重现的例子:
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());
}
}
枚举class:
package cupawntae;
public enum MyEnum {
}
输出:
Objects: [class cupawntae.Reflector]
Enums: [class cupawntae.MyEnum]
Enum's superclass: class java.lang.Object
这实际上是 documented behaviour,尽管可以说它不是特别清楚或直观:
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 ifmy.package.SomeClass
extendsother.package.OtherClass
, than the later will not be scanned). in that case use the other constructors and specify the relevant packages/urls
编辑:该文档的 later revision 说:
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.
在这种情况下,java.lang.Enum
被视为可传递的 class(如 other.package.OtherClass
),因此不包括在扫描中,这意味着 class 是 Enum
不包括在内。
类似地,如果我们在问题示例中使 Reflections
扩展目标包之外的内容,例如
public class Reflector extends Exception {
然后 class 不再在扫描中找到
Objects: []
Enums: [class cupawntae.MyEnum]
Enum's superclass: class java.lang.Object