@Test public void shouldReturnDefaultValueWhenMethodIsNull() { assertThat(Reflection.invokeWithDefault(null, null, "the default")) .isEqualTo("the default"); }
@SuppressWarnings("unchecked") public static <T> T invokeWithDefault(@Nullable Method method, Object obj, T defaultValue) { if (method == null) { return defaultValue; } try { Object value = method.invoke(obj); if (value == null) { return defaultValue; } return (T) value; } catch (Throwable t) { logger.warn("error calling {}.{}()", method.getDeclaringClass().getName(), method.getName(), t); return defaultValue; } }
Reflection { @SuppressWarnings("unchecked") public static <T> T invokeWithDefault(@Nullable Method method, Object obj, T defaultValue) { if (method == null) { return defaultValue; } try { Object value = method.invoke(obj); if (value == null) { return defaultValue; } return (T) value; } catch (Throwable t) { logger.warn("error calling {}.{}()", method.getDeclaringClass().getName(), method.getName(), t); return defaultValue; } } }
Reflection { @SuppressWarnings("unchecked") public static <T> T invokeWithDefault(@Nullable Method method, Object obj, T defaultValue) { if (method == null) { return defaultValue; } try { Object value = method.invoke(obj); if (value == null) { return defaultValue; } return (T) value; } catch (Throwable t) { logger.warn("error calling {}.{}()", method.getDeclaringClass().getName(), method.getName(), t); return defaultValue; } } private  Reflection(); }
Reflection { @SuppressWarnings("unchecked") public static <T> T invokeWithDefault(@Nullable Method method, Object obj, T defaultValue) { if (method == null) { return defaultValue; } try { Object value = method.invoke(obj); if (value == null) { return defaultValue; } return (T) value; } catch (Throwable t) { logger.warn("error calling {}.{}()", method.getDeclaringClass().getName(), method.getName(), t); return defaultValue; } } private  Reflection(); static @Nullable Method getMethod(@Nullable Class<?> clazz, String methodName); static @Nullable Field getDeclaredField(@Nullable Class<?> clazz, String fieldName); @SuppressWarnings("unchecked") static T invoke(@Nullable Method method, Object obj); @SuppressWarnings("unchecked") static T invokeWithDefault(@Nullable Method method, Object obj, T defaultValue); static @Nullable Object getFieldValue(@Nullable Field field, Object obj); }
Reflection { @SuppressWarnings("unchecked") public static <T> T invokeWithDefault(@Nullable Method method, Object obj, T defaultValue) { if (method == null) { return defaultValue; } try { Object value = method.invoke(obj); if (value == null) { return defaultValue; } return (T) value; } catch (Throwable t) { logger.warn("error calling {}.{}()", method.getDeclaringClass().getName(), method.getName(), t); return defaultValue; } } private  Reflection(); static @Nullable Method getMethod(@Nullable Class<?> clazz, String methodName); static @Nullable Field getDeclaredField(@Nullable Class<?> clazz, String fieldName); @SuppressWarnings("unchecked") static T invoke(@Nullable Method method, Object obj); @SuppressWarnings("unchecked") static T invokeWithDefault(@Nullable Method method, Object obj, T defaultValue); static @Nullable Object getFieldValue(@Nullable Field field, Object obj);  }