Example usage for org.apache.commons.lang3.reflect FieldUtils readField

List of usage examples for org.apache.commons.lang3.reflect FieldUtils readField

Introduction

In this page you can find the example usage for org.apache.commons.lang3.reflect FieldUtils readField.

Prototype

public static Object readField(final Object target, final String fieldName, final boolean forceAccess)
        throws IllegalAccessException 

Source Link

Document

Reads the named Field .

Usage

From source file:ubic.gemma.persistence.util.EntityUtils.java

/**
 * @param methodName     accessor e.g. "getId"
 * @param <T>            the type//from  ww  w .  j ava2 s  .  c  o  m
 * @param entities       entities
 * @param nestedProperty nested property
 * @return the created map
 */
public static <T> Map<Long, T> getNestedIdMap(Collection<? extends T> entities, String nestedProperty,
        String methodName) {
    Map<Long, T> result = new HashMap<>();

    for (T object : entities) {
        try {
            result.put(EntityUtils.getId(FieldUtils.readField(object, nestedProperty, true), methodName),
                    object);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }

    return result;
}