4

I would like to get the names and values of all attributes of a class instance. That class can be any customized any class, and it can also have object lists, hashmap, tables etc., and can be extented from another class. Actually I mean I would like to get name and value of all attr of a class. so what I need to do should be kind of template. Is it possible? I have written that up to now. any suggestion would be appreciated.

    public static void getObjectIntoMap(Object obj) throws IllegalArgumentException, IllegalAccessException {
    Field[] field = obj.getClass().getDeclaredFields();
    Class<?> c = obj.getClass().getSuperclass();
    if(c != Object.class)
        getObjectIntoMap(c.getClass());
    System.out.println("SSS : "+field.length);
    for (Field f : field) {
        if(f.getType() == java.util.List.class){
            java.util.List<Object> ll = (java.util.List<Object>) f.get(obj);
            for (Object o : ll) {
                Field[] ff = o.getClass().getDeclaredFields();
                for (Field field2 : ff) {
                    print(field2.getName(), field2.get(o).toString());
                }
            }
        }else if(f.getType() == Hashtable.class){

        }
        else if(f.getType() == HashMap.class){

        }else if(f.getType() == Object[].class){

        }   
        else{
            print(f.getName(), f.get(obj).toString());
        }
    }
}
Henry
  • 42,982
  • 7
  • 68
  • 84
Emilla
  • 484
  • 1
  • 10
  • 23
  • make **f.setAccessible(true)** for reading private varibles – Jagadesh Seeram Jan 03 '14 at 08:03
  • thank you. I was getting exception there. do you have any advice here. Because I can not consider about all things a class can have at runtime. Reflections adviced below seems to me confused. – Emilla Jan 03 '14 at 08:14

3 Answers3

5

try http://code.google.com/p/reflections/ ReflectionUtils.getAllFields returns a Set of all class fields including all super types fields. Now we only need to iterate over them and read instance field values:

  Set<Field> fields = ReflectionUtils.getAllFields(X.class, new Predicate() {
    public boolean apply(Object input) {
        return true;
    }});
   Map<Field, Object) values = ...
   for(Field f : fields) {
        f.setAccessible(true);
        values.put(f, f.get(obj);
    }
Evgeniy Dorofeev
  • 133,369
  • 30
  • 199
  • 275
  • But Reflections does not have a method getAllFields. could you please give an example? – Emilla Jan 03 '14 at 09:04
  • I have added reflections-0.9.9-RC1-uberjar.jar to my project. but really Reflections doesnt have a method named getAllFields. – Emilla Jan 03 '14 at 09:18
  • I used 0.9.8 and it is there, but I checked 0.9.9 - they really removed it, use ReflectionUtils instead. – Evgeniy Dorofeev Jan 03 '14 at 09:23
3

use #getDeclaredFields() to include private fields, #getFields() is for public.

with private field, due to restrictions refer #setAccessible() method also.

Ashish
  • 735
  • 1
  • 6
  • 15
  • when I set setAccessible() true it returns me all attr of class even which assigned by jvm. I dont need that and this mix everything. I just need to get 'public' 'private' 'protected' defined variables – Emilla Jan 03 '14 at 09:49
  • check http://docs.oracle.com/javase/tutorial/reflect/member/field.html and http://docs.oracle.com/javase/tutorial/reflect/member/fieldValues.html – Ashish Jan 03 '14 at 09:59
0

Just a quick addendum:

if(f.getType() == Object[].class

This is probably not what you want (I'm guessing you want to detect all arrays, not just arrays of type Object[]). Instead, you'll want to use something like this:

if(f.getType().getComponentType()!=null)

or, if you want all kinds of object arrays, including e.g. String[], but no primitive arrays, then you could do this:

if(Object[].class.isAssignableFrom(f.getType()))

Apart from that, there are more standardized ways to do what you want to do, which I have shown in previous answers. You can:

Community
  • 1
  • 1
Sean Patrick Floyd
  • 292,901
  • 67
  • 465
  • 588
  • yes for the object array or list I want get whole as string just if its type is primitive type or String, Double, Long etc. But for custom object list and array i need to get their attr name and value one by one. This is one of what I am trying to do – Emilla Jan 03 '14 at 08:57