Java Reflection - Getting value of nested object, list and array within object

java, reflection

Solution

The method `Class.getDeclaredFields` will get you an array of `Field`s representing each field of the class. You could loop over these and check the type returned by `Field.getType`. Filtering fields of type `List` to `List<String>` is trickier - see this post for help with that.

After doing the first dynamic lookup of the fields you want, you should keep track of (memoize) the relevant `Field` objects for better performance.

Here's a quick example:

//for each field declared in User,
for (Field field : User.class.getDeclaredFields()) {
    //get the static type of the field
    Class<?> fieldType = field.getType();
    //if it's String,
    if (fieldType == String.class) {
        // save/use field
    }
    //if it's String[],
    else if (fieldType == String[].class) {
        // save/use field
    }
    //if it's List or a subtype of List,
    else if (List.class.isAssignableFrom(fieldType)) {
        //get the type as generic
        ParameterizedType fieldGenericType =
                (ParameterizedType)field.getGenericType();
        //get it's first type parameter
        Class<?> fieldTypeParameterType =
                (Class<?>)fieldGenericType.getActualTypeArguments()[0];
        //if the type parameter is String,
        if (fieldTypeParameterType == String.class) {
            // save/use field
        }
    }
}

Note that I used reference equality (`==`) instead of `isAssignableFrom` to match `String.class` and `String[].class` since `String` is `final`.

Edit: Just noticed your bit about finding nested `UserDefinedObject`s. You could apply recursion to the above strategy in order to search for those.

Problem

I am new to reflection and trying to create a generalized function which will take in object and parse all the fields that are of type `String`, `String[]` or `List<String>`. Any `String`, `String[]` or `List<String>` that is present in nested object has also to be parsed. Is there any utility which can help me in doing that? Getting the values from parent object (`User`) was simple - used `BeanUtils.describe(user);` - it gives the `String` values in parent object but not `String[]`, `List<String>` and nested object. I am sure I might not be the first one who needs this feature? Are there any utilities or code which could help me achieve do this? ``` public class User { private String somevalue; private String[] thisArray; private List<String> thisList; private UserDefinedObject myObject; . . . } ```

Original source

Related problems