Add ReflectionUtil.readField to directly read an object's field's value
@@ -199,4 +199,21 @@ public static Method findMethod(Class<?> targetType, String methodName, Class<?>
return getTypeParameterForSuperInterface(targetClass.getGenericSuperclass(), superClass, index);
}
+ public static Object readField(Object object, String fieldName) {
+ Class<?> cls = object.getClass();
+ for (Class<?> c = cls; c != null; c = c.getSuperclass()) {
+ try {
+ final Field field = c.getDeclaredField(fieldName);
+ field.setAccessible(true);
+ return field.get(object);
+ } catch (final NoSuchFieldException e) {
+ // Try parent
+ } catch (Exception e) {
+ throw new IllegalArgumentException(
+ "Cannot access field " + cls.getName() + "." + fieldName, e);
+ }
+ }
+ throw new IllegalArgumentException(
+ "Cannot find field " + cls.getName() + "." + fieldName);
+ }
}
