如何使用反射库获取子类型的子类型

How to use reflections library to get subtype of a subtype

这是我的场景,我有三个 类:

public abstract class A {

    public Set<Class<?>> getGrandChildren() {
        Reflections reflections = new Reflections(this.getClass().getPackage().getName());
        Set<Class<?>> grandChildren = reflections.getSubTypesOf(this.getClass());
        return grandChildren;
    }

}

public class B extends A {}

public class C extends B implements X {}

class D {

       public B client = new B();

       //I am trying to get all children of this class 
       client.getGrandChildren()    
}

我的编译器抱怨类型为:

Set<Class<?>> grandChilderen = reflections.getSubTypesOf(this.getClass());

我该怎么做?

最简单的方法是依赖 raw type,因为它失败了,因为它需要参数化类型的值(对应于签名 public <T> Set<Class<? extends T>> getSubTypesOf(Class<T> type) 中的 T),而您不能在这里提供,因为它是一种通用方法

public Set<Class<?>> getGrandChildren() {
    Reflections reflections = new Reflections(this.getClass().getPackage().getName());
    Set grandChildren = reflections.getSubTypesOf(this.getClass());
    return grandChildren;
}