这是我的场景,我有三个班级:
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());
我该怎么做?
答案 0 :(得分:1)
最简单的方法是在失败时依赖raw type
,因为它需要参数化类型的值(在签名T
中相对于public <T> Set<Class<? extends T>> getSubTypesOf(Class<T> type)
),这是您在此处无法提供的因为它是一种通用的方法
public Set<Class<?>> getGrandChildren() {
Reflections reflections = new Reflections(this.getClass().getPackage().getName());
Set grandChildren = reflections.getSubTypesOf(this.getClass());
return grandChildren;
}