jeudi 1 novembre 2018

Get List of Classes from an object in java

Iam currently trying to create a distinct List<Class> classList which contains all Classes of an object for example

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

What i want to create is a method public List<Class> getDistinctClasses(Class cl); which you give as input for example DemoObject.class and returns a list with

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

Another example for NestedDemoObject.class would be

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

I tried to use the .getDeclaredClasses() from Class without any luck. There is any way to get all nested classes from an object with Reflection API?

Any help or direction appreciated.





Aucun commentaire:

Enregistrer un commentaire