简体   繁体   中英

Access Nested Field using Java Reflection

Suppose that I have several classes in this style:

public class A {
     public B element;
}

public class B {
     public C anotherElement; // where C refers to another type which contains types D, E, F, etc.....
}

I want to iterate over all of the contained sub-classes (not inherited) and get a list of all fields in the nested object tree. However, when I use reflection code like this:

    Field[] myFields = providedObject.getClass().getDeclaredFields();
    for (Field myField : myFields) {
        // do something here?? to access the sub-fields of the class
        // if I print out myField.getName(), I get the element of class A, but I also want to access the anotherElement of class B (without hard-coding the name 'anotherElement', I want a full traversal of all nested fields)
    }

At the 'do something here' step, I want to access the sub-fields of myField, but I don't see anything in the Field api that directly lets me do this. I've tried:

myField.getDeclaringClass().getDeclaredFields() -> this seems to return the same element that we already saw and not the sub-elements

and

myField.getClass().getDeclaredFields() -> this seems to return the fields of the 'Field' class and not my class A or B

So how would I access the nested fields from the reflection api?

Thanks.

I've added following statement to quit recursive method. You can change it whatever you want.

        if (clazz == null || clazz.isPrimitive() || !clazz.getPackage().getName().startsWith("com.vvv.stack02")) {
            return;
        }

The main idea is you need to convert field to a class via typing following line.

Class<?> fieldClass = myField.getType();

public class Main {

    public static void main(String[] args) {
        printFields(A.class);
    }

    public static void printFields(Class<?> clazz) {

        if (clazz == null || clazz.isPrimitive() || !clazz.getPackage().getName().startsWith("com.vvv.stack02")) {
            return;
        }

        Field[] myFields = clazz.getDeclaredFields();
        for (Field myField : myFields) {
            System.out.println(clazz.getSimpleName() + "->" + myField.getType().getSimpleName() + ":" + myField.getName());
            Class<?> fieldClass = myField.getType();
            printField(fieldClass);
        }

    }

    public static class A {
        public B element;
    }

    public static class B {
        public C anotherElement; // where C refers to another type which contains types D, E, F, etc.....
    }

    public static class C {
        public Integer a;
    }

}

It's just a bit of recursion:

public static void traverseDepthFirst(Object obj) { // May need throws/catch!
    if (obj == null) {
        // ... do something for null ...
        return;
    }
    // ... perhaps do something different for arrays, primitives, String, etc.
    for (Field field : obj.getClass().getDeclaredFields()) {
        field.setAccessible(true);
        // ... pre-traversal code ...
        traverseDepthFirst(field.get(obj));
        // ... post-traversal code ...
    }
}

Edit: That will only consider the leaf class. To include super classes.

public static void traverseDepthFirst(Object obj) { // May need throws/catch!
    if (obj == null) {
        // ... do something for null ...
        return;
    }
    // ... perhaps do something different for arrays, primitives, String, etc.
    for (
        Class<?> clazz = obj.getClass();
        clazz != null;
        clazz = clazz.getSuperclass()
    ) {
        for (Field field : clazz.getDeclaredFields()) {
            field.setAccessible(true);
            // ... pre-traversal code ...
            traverseDepthFirst(field.get(obj));
            // ... post-traversal code ...
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM