为什么reflections.getSubTypesOf(Object.class)找不到枚举? [英] Why doesn't reflections.getSubTypesOf(Object.class) find enums?

查看:832
本文介绍了为什么reflections.getSubTypesOf(Object.class)找不到枚举?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有

Reflections reflections = new Reflections("my.package", classLoader, new SubTypesScanner(false));

然后找到我的枚举类

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());
    }
}

枚举类别:

package cupawntae;

public enum MyEnum {
}

输出:

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:


请注意,使用构造函数 new Reflections( my.package),将仅扫描前缀为 my.package 的网址,而不会扫描其他网址中的所有传递类(例如,如果 my.package.SomeClass 扩展了 other.package.OtherClass ,以后将不进行扫描)。在这种情况下,请使用其他构造函数并指定相关的包/ URL

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:后来修订


确保扫描所有与传递相关的软件包。例如
,假设您的类C扩展了B扩展了A,并且B和A都位于C之外的另一个包中,则仅扫描C的包-然后查询A的子类型不返回任何值(可传递) ,但查询B的子类型将返回C(直接)。在这种情况下,请确保先扫描所有相关软件包。

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 被视为传递类(例如 other.package.OtherClass ),因此不包括在扫描中,这意味着<$ c $的子类c>枚举不包括在内。

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.

类似地,如果我们在 Reflections 中进行问题的示例在目标包之外扩展了某些内容,例如

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

这篇关于为什么reflections.getSubTypesOf(Object.class)找不到枚举?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆