3

Say I have a class that has some member variables one or more being another class also with member variables. How can I get the value of each variable in both classes?

I can get the variables by calling: getDeclaredFields(); And the values of the Foo Object by calling field.get(obj) then iterating of them again to get the sub class variables.

But how can I get the value for each sub class member variable without an instance of that class? My trivial example works because the variables are the same for each instance of the Bar class and I can call innerField.get(new Bar())

I guess it could work if I could somehow get the instance of the object from the field?

Thanks in advance

First Object:

public class Foo {
    public Foo(){}
    private Integer someInt = 123;
    private Long someLong = 987654321L;
    private String someString = "qwertyuiop";
    private Bar bar = new Bar();
}

Second Object:

public class Bar {
    public Bar(){}
    private Double someDouble = 123.456;
    private String innerString = "another String";
}

main/ calling method:

public static void main(String[] args) throws Exception {
    Foo foo = new Foo();
    getMemberFields(foo);
}

Getting variables and values:

public static void getMemberFields(Object obj) throws IllegalAccessException,               NoSuchFieldException {
    Class<?> objClass = obj.getClass();
    System.out.println("obj: " + obj.getClass());

    Field[] fields = objClass.getDeclaredFields();
    for(Field field : fields) {
        field.setAccessible(true);
        System.out.println("Field: " + field.getName() + " value: " + field.get(obj));

        Field[] innerFields = {};
        if (!field.getType().getName().contains("java.lang")) {
             innerFields = field.getType().getDeclaredFields();
        }

        for (Field innerField: innerFields) {
            if (!innerField.getName().contains("java.lang")) {
                Object subObj = field.getType();

                innerField.setAccessible(true);
                System.out.println("");

                System.out.println("Sub Field: " + innerField.getName());
                System.out.println( "Sub Field value: " + innerField.get(new Bar()));
            }

        }
    }
}

Output

obj: class Foo
Field: someInt value: 123
Field: someLong value: 987654321
Field: someString value: qwertyuiop
Field: bar value: Bar@61bbe9ba
Sub Field: someDouble
Sub Field value: 123.456
Sub Field: innerString
Sub Field value: another String
Kai
  • 1,709
  • 1
  • 23
  • 36

1 Answers1

2

The solution was rather simple. If anyone else comes accross the same issue here's how I solved it.

private static HashMap<String, Object> getMemberFields(Object obj) throws IllegalAccessException,
        NoSuchFieldException
{
     HashMap<String, Object> fieldValues = new HashMap<String, Object>();
            Class<?> objClass = obj.getClass();

    Field[] fields = objClass.getDeclaredFields();
    for(Field field : fields)
    {
        field.setAccessible(true);
        fieldValues.put(field.getName(), field.get(obj));
        if (!field.getType().isPrimitive() && !field.getType().getName().contains("java.lang"))
        {
            getMemberFields(field.get(obj));
        }
    }
    return fieldValues;
}
Kai
  • 1,709
  • 1
  • 23
  • 36