从java中的对象获取类列表

我目前正在尝试创建一个不同的List<Class> classList,其中包含一个对象的所有类,例如


DemoObject.java


public class DemoObject {


    private Integer id;

    private String name;

    private BigDecimal price;

    private Boolean isActive;

    private List<NestedDemoObject> nested;

}

NestedDemoObject.java


public class NestedDemoObject {


    private Integer id;

    private String nameNest;

    private Boolean isActive;

}

我想要创建的是一个方法public List<Class> getDistinctClasses(Class cl);,例如您将其作为输入DemoObject.class并返回一个列表


[DemoObject.class, Integer.class, String.class, BigDecimal.class, Boolean.class, List.class, NestedDemoObject.class]


另一个例子NestedDemoObject.class是


[NestedDemoObject.class, Integer.class, String.class, Boolean.class]


我试图使用.getDeclaredClasses()fromClass没有任何运气。有什么方法可以使用反射 API 从对象中获取所有嵌套类?


任何帮助或方向表示赞赏。


繁星coding
浏览 114回答 2
2回答

幕布斯7119047

Mark 提供的解决方案是部分正确的。您正在尝试从声明的字段中检索类的正确方法。然而,getType()方法并没有揭示泛型类型。为了访问您应该使用的泛型类型Field.getGenericType()。它将类作为Type对象返回。该Field对象知道自己的类型(它们也不会被删除作为一个可能会误认为)。这是一个用泛型打印类型的 java 1.8+ 示例:Arrays.stream(DemoObject.class.getDeclaredFields())&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .map(Field::getGenericType)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .map(Type::getTypeName)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .distinct()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .forEach(System.out::println);它将打印以下结果:java.lang.Integerjava.lang.Stringjava.math.BigDecimaljava.lang.Booleanjava.util.List<com.eto.sandbox.NestedDemoObject>如果您想使用泛型类型或出于任何原因解析它们,那么您可以使用以下示例:&nbsp;Arrays.stream(DemoObject.class.getDeclaredFields())&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .map(Field::getGenericType)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .distinct()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .forEach(type -> {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (type instanceof Class) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // This is a simple class&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else if (type instanceof ParameterizedType) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // This is a generic type. You can parse its parameters recursively.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });

慕雪6442864

也许这为您指明了正确的方向:for (Field f : DemoObject.class.getDeclaredFields()) {&nbsp; &nbsp; System.out.println(f.getType().getName());}这打印:java.lang.Integerjava.lang.Stringjava.math.BigDecimaljava.lang.Booleanjava.util.List您可以通过类似Class.forName.我觉得很奇怪,这getDeclaredClasses对我也不起作用,我会调查一下。当我知道更多时,我会更新答案。更新getDeclaredClasses 打印在类中定义的类,如下所示:class DemoObject {&nbsp; &nbsp; private Integer id;&nbsp; &nbsp; private String name;&nbsp; &nbsp; private BigDecimal price;&nbsp; &nbsp; private Boolean isActive;&nbsp; &nbsp; private List<NestedDemoObject> nested;&nbsp; &nbsp; public class InnerClass {&nbsp; &nbsp; }}然后执行getDeclaredClasses:for (Class<?> f : DemoObject.class.getDeclaredClasses()) {&nbsp; &nbsp; System.out.println(f.getName());}打印值:DemoObject$InnerClass
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java