0

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.

Suvasis
  • 1,451
  • 4
  • 24
  • 42

2 Answers2

0

You can use Field.getType().isPrimitive() to find out if a field is of a primitive type (int, bool, &c.). If it's not, you have some object type, and if the field's value is not null you should recursively traverse the referenced object's fields.

It seems there's no built-in way of discriminating between primitive wrappers and other Java classes. https://stackoverflow.com/a/1704658/1015327 has a suggestion for a work-around, manually gathering the primitive wrapper classes.

Community
  • 1
  • 1
JimmyB
  • 12,101
  • 2
  • 28
  • 44
  • By the time it reaches validator layer, both Wrapper and User defined class type object will be not null. It would have instantiated/ populated. – Suvasis Nov 06 '14 at 10:10
0

There is no simple or straightforward way to do what you want. There is a large number of possible field types. The field can be primitive, Collection, costume type, Date, UUID, JLabel, etc.

If you know the set of possible built-in types that you'll encounter, you might be able to hack your way through it. First get the type of the field, and then run the type through a series of checks against the possible types. If a match is found, cast and retrieve the value(s). If no match is found, then you can assume that this is a custom type with its own fields.

You still have to worry about custom types that extends built-in types (e.g. custom Map). Honestly, I don't think reflection is the way to go here.

Khalid
  • 2,212
  • 11
  • 12
  • May be I could put the question in better way. A class can have not only Wrapper Classes like Integer or String etc but it can have any other Java provided classes like Set<>, List<>, Date etc. I am editing the question. – Suvasis Nov 06 '14 at 14:56