ObjectUtils { @Deprecated public static Object getValue(Object object, String name) { Class<?> objectClass = object.getClass(); for (Class<?> c = objectClass; c != null; c = c.getSuperclass()) { Method getter = null; try { getter = c.getDeclaredMethod(name); } catch (NoSuchMethodException error) { } if (getter == null) { try { getter = c.getDeclaredMethod("get" + capitalize(name)); } catch (NoSuchMethodException error) { } } if (getter != null) { getter.setAccessible(true); try { return getter.invoke(object); } catch (IllegalAccessException ex) { throw new IllegalStateException(ex); } catch (InvocationTargetException ex) { Throwable cause = ex.getCause(); throw cause instanceof RuntimeException ? (RuntimeException) cause : new RuntimeException(cause); } } else { Field field = null; try { field = c.getDeclaredField(name); } catch (NoSuchFieldException error) { } if (field == null) { try { field = c.getDeclaredField("_" + name); } catch (NoSuchFieldException error) { } } if (field != null) { try { return field.get(object); } catch (IllegalAccessException ex) { throw new IllegalStateException(ex); } } } } throw new IllegalArgumentException(String.format( "[%s] class does not contain a getter named [%s]!", objectClass.getName(), name)); }  static boolean equals(Object object1, Object object2); static int hashCode(Object... objects); static T firstNonNull(T... values); static T firstNonBlank(T... values); @Deprecated static T coalesce(T... values); static ClassLoader getCurrentClassLoader(); static Class<?> getClassFromLoader(ClassLoader loader, String name); static Class<?> getClassByName(String name); static int compare(Object object1, Object object2, boolean isNullGreatest); static void sort(List<?> list, boolean isNullGreatest); static boolean isBlank(Object object); @Deprecated static Set<Class<? extends T>> findClassesFromLoader(ClassLoader loader, Class<T> baseClass); @Deprecated static Set<Class<? extends T>> findClasses(Class<T> baseClass); static Object to(Type returnType, Object object); static T to(Class<T> returnClass, Object object); static T to(TypeReference<T> returnTypeReference, Object object); static Object toOrError(Type returnType, Object object); static T toOrError(Class<T> returnClass, Object object); static T toOrError(TypeReference<T> returnTypeReference, Object object); static Object fromJson(String string); static Object fromJson(byte[] bytes); static String toJson(Object object); static String toJson(Object object, boolean isIndentOutput); @Deprecated static String toJson(Object object, boolean isIndentOutput, Transformer transformer); static String getContentType(String path); static double jitterAbsolutely(double number, double amount); static long jitterAbsolutely(long number, long amount); static double jitter(double number, double scale); static long jitter(long number, double scale); @Deprecated static int compareTo(Object object1, Object object2); @Deprecated static T newInstance(Class<T> objectClass); @Deprecated static Object getValue(Object object, String name); @Deprecated @SuppressWarnings("all") static Set<Class<?>> findClasses(ClassLoader loader, Class<?> baseClass); @Deprecated static Byte asByte(Object byteLike); @Deprecated static Date asDate(Object dateLike); @Deprecated static T asEnum(Class<T> type, Object enumLike); @Deprecated static Double asDouble(Object doubleLike); @Deprecated static Float asFloat(Object floatLike); @Deprecated static Integer asInteger(Object integerLike); @Deprecated static Long asLong(Object longLike); @Deprecated static Short asShort(Object shortLike); @Deprecated static UUID asUuid(Object uuidLike); @Deprecated static void errorIf(boolean condition, String parameterName, String message); @Deprecated static void errorIfNull(Object object, String parameterName); @Deprecated static void errorIfBlank(Object object, String parameterName); @Deprecated static Object getByPath(Object object, String path); static final String DEFAULT_CONTENT_TYPE; }
@Test public void test_getValue_parent_field() { ChildClass object = new ChildClass(); assertEquals("string1", ObjectUtils.getValue(object, "string1")); }
@Test public void test_getValue_child_field() { ChildClass object = new ChildClass(); assertEquals("string3", ObjectUtils.getValue(object, "string3")); }
@Test public void test_getValue_parent_method() { ChildMethod object = new ChildMethod(); assertEquals("method parent result", ObjectUtils.getValue(object, "method_parent")); }
@Test public void test_getValue_child_method() { ChildMethod object = new ChildMethod(); assertEquals("method child result", ObjectUtils.getValue(object, "method_child")); }
@Test public void test_getValue_parent_getter() { ChildGetter object = new ChildGetter(); assertEquals("getter parent result", ObjectUtils.getValue(object, "parent")); }
@Test public void test_getValue_child_getter() { ChildGetter object = new ChildGetter(); assertEquals("getter child result", ObjectUtils.getValue(object, "child")); }
@Test public void test_getValue_parent_underscore() { ChildUnderscore object = new ChildUnderscore(); assertEquals("value parent", ObjectUtils.getValue(object, "field_parent")); }
@Test public void test_getValue_child_underscore() { ChildUnderscore object = new ChildUnderscore(); assertEquals("value child", ObjectUtils.getValue(object, "field_child")); }
@Test (expected=IllegalStateException.class) public void test_getValue_field_private() { PrivateClass object = new PrivateClass(); ObjectUtils.getValue(object, "privateField"); }
@Test public void test_getValue_method_private() { PrivateClass object = new PrivateClass(); assertEquals("private method", ObjectUtils.getValue(object, "privateMethod")); }
@Ignore @Test (expected=IllegalArgumentException.class) public void test_getValue_unknown() { UnknownClass object = new UnknownClass(); ObjectUtils.getValue(object, "unknown"); }
@Test public void test_getValue_priority_method_getter() { final class TestClass { public String getItem() { return "getter value"; } public String item() { return "method value"; } } TestClass object = new TestClass(); assertEquals("method value", ObjectUtils.getValue(object, "item")); }
@Test public void test_getValue_priority_getter_field() { final class TestClass { public String item = "item value"; public String getItem() { return "getter value"; } } TestClass object = new TestClass(); assertEquals("getter value", ObjectUtils.getValue(object, "item")); }
@Test public void test_getValue_priority_field_underscore() { final class TestClass { public String item = "item value"; public String _item = "_item value"; } TestClass object = new TestClass(); assertEquals("item value", ObjectUtils.getValue(object, "item")); }