ClassUtils { public static Object getFieldValue(Object obj, String fieldName) { try { int p = fieldName.indexOf('.'); if (p >= 0) { String firstProperty = fieldName.substring(0, p); Object first = MethodUtils.invokeMethod(obj, GET + StringUtils.capitalize(firstProperty)); return first == null ? null : getFieldValue(first, fieldName.substring(p + 1)); } else { if (hasMethod(obj, GET + StringUtils.capitalize(fieldName))) { return MethodUtils.invokeMethod(obj, GET + StringUtils.capitalize(fieldName)); } else { return MethodUtils.invokeMethod(obj, IS + StringUtils.capitalize(fieldName)); } } } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) { throw new OCSRuntimeException("Error getting of " + obj + ":" + e.getMessage(), e); } } private  ClassUtils(); static boolean canSetProperty(Object obj, String fieldName); static void clearFieldValue(Object obj, String fieldName, Class<?> argType); static Class<?> forClass(final String clazz); static T getAnnotation(Class<?> clazz, String fieldName, Class<T> annotationClass); @SuppressWarnings("unchecked") static R getAnnotationAttributeValue(Field field, Class<T> annotationClass, String attributeName); static T getAnnotationOnField(Class<?> clazz, String fieldName, Class<T> annotationClass); @SuppressWarnings("unchecked") static T getAnnotationOnField(Field field, Class<T> annotationClass); static T getAnnotationOnMethod(Class<?> clazz, String fieldName, Class<T> annotationClass); @SuppressWarnings("unchecked") static T getAnnotationOnMethod(Method method, Class<T> annotationClass); static byte[] getBytes(Object obj, String fieldName); static Constructor<T> getConstructor(Class<T> clazz, Object... args); static Field getField(Class<?> clazz, String fieldName); static Object getFieldValue(Object obj, String fieldName); static String getFieldValueAsString(Object obj, String fieldName); static String getFieldValueAsString(Object obj, String fieldName, String defValue); static Method getGetterMethod(Class<?> clazz, String fieldName); static int getMaxLength(Class<?> clazz, String fieldName); static PropertyDescriptor getPropertyDescriptorForNestedProperty(Class<?> clazz, String property); static Class<?> getResolvedType(Class<T> type, String fieldName, int... indexes); static Class<?> getResolvedType(Object object, int... indexes); static boolean hasMethod(Object obj, String methodName); static T instantiateClass(Class<T> clazz, Object... args); static void setBytes(byte[] bytes, Object obj, String fieldName); static void setFieldValue(Object obj, String fieldName, Object value); static void copyFields(T from, S to);  }
@Test public void testGetFieldValue() { TestEntity entity = new TestEntity(); entity.setAge(12L); assertEquals(12L, ClassUtils.getFieldValue(entity, "age")); }
@Test public void testGetFieldValueDoesntExists() { TestEntity entity = new TestEntity(); entity.setAge(12L); assertThrows(OCSRuntimeException.class, () -> ClassUtils.getFieldValue(entity, "age2")); }
@Test public void testGetFieldValueRecursive() { TestEntity entity = new TestEntity(); entity.setAge(12L); TestEntity2 entity2 = new TestEntity2(); entity2.setTestEntity(entity); assertEquals(12L, ClassUtils.getFieldValue(entity2, "testEntity.age")); }