I want to validate all properties in an object A (say a). Class A has composite object B which has its own properties.
Class A
public Class A {
private String name;
private B b;
private Set<SomeObject> fields = new HashSet<SomeObject>();
//getter and setters for name and b
}
Class B
public Class B {
private String address;
private String dob;
//getter and setters for address and dob
}
-----EDIT ----- Class SomeObject
public Class SomeObject{
private Double amount;
//getter and setters for amount
}
Logic to get value name and object b using reflection
Field[] fields = a.getClass().getDeclaredFields();
for (Field field : fields) {
String fieldName = field.getName();
System.out.println(" *** Field Name *** " + fieldName);
field.setAccessible(true);
Object obj1 = field.get(a);
System.out.println(" *** Object 1 *** " + obj1);
}
--EDIT ---
I use reflection to get value of name dynamically. But how do I know there is property B which is a composite object or a Set of another object and I should apply reflection on B to get values of address and dob. Again how can I get values from Set and apply reflection on iteration. Is there a better solution without iteration.
Other way round, I should determine if a class is Wrapper class (String, Integer etc) or User defined class(User Defined Bean). If wrapper class/primitive then no getter invocation. Again If it is collection or something I should iterate and apply reflection on each Object to get amount
Is there any java API available which can do this level of data extraction from a complex object. eg EquiBuilder, BeanUtils etc
Can someone help.