@Test public void invokeMethod() { TestBean bean = new TestBean(); assertThat((String) ReflectionUtil.invokeMethod(bean, "privateMethod", new Object[] { "calvin" })) .isEqualTo("hello calvin"); assertThat((String) ReflectionUtil.invokeMethod(bean, "privateMethod", new Object[] { "calvin" }, new Class[] { String.class })).isEqualTo("hello calvin"); assertThat((String) ReflectionUtil.invokeMethodByName(bean, "privateMethod", new Object[] { "calvin" })) .isEqualTo("hello calvin"); assertThat((int) ReflectionUtil.invokeMethod(bean, "intType", new Object[] { 1 }, new Class[] { int.class })) .isEqualTo(1); assertThat((int) ReflectionUtil.invokeMethod(bean, "integerType", new Object[] { 1 }, new Class[] { Integer.class })).isEqualTo(1); assertThat((int) ReflectionUtil.invokeMethod(bean, "listType", new Object[] { ListUtil.newArrayList("1", "2") }, new Class[] { List.class })).isEqualTo(2); assertThat((int) ReflectionUtil.invokeMethod(bean, "intType", 1)).isEqualTo(1); assertThat((int) ReflectionUtil.invokeMethod(bean, "integerType", 1)).isEqualTo(1); assertThat((int) ReflectionUtil.invokeMethod(bean, "listType", ListUtil.newArrayList("1", "2"))).isEqualTo(2); try { ReflectionUtil.invokeMethod(bean, "notExistMethod", new Object[] { "calvin" }, new Class[] { String.class }); failBecauseExceptionWasNotThrown(IllegalArgumentException.class); } catch (IllegalArgumentException e) { } try { ReflectionUtil.invokeMethod(bean, "privateMethod", new Object[] { "calvin" }, new Class[] { Integer.class }); failBecauseExceptionWasNotThrown(RuntimeException.class); } catch (RuntimeException e) { } try { ReflectionUtil.invokeMethodByName(bean, "notExistMethod", new Object[] { "calvin" }); failBecauseExceptionWasNotThrown(IllegalArgumentException.class); } catch (IllegalArgumentException e) { } }
public static <T> T invokeMethod(Object obj, String methodName, Object... args) { Object[] theArgs = ArrayUtils.nullToEmpty(args); final Class<?>[] parameterTypes = ClassUtils.toClass(theArgs); return invokeMethod(obj, methodName, theArgs, parameterTypes); }
ReflectionUtil { public static <T> T invokeMethod(Object obj, String methodName, Object... args) { Object[] theArgs = ArrayUtils.nullToEmpty(args); final Class<?>[] parameterTypes = ClassUtils.toClass(theArgs); return invokeMethod(obj, methodName, theArgs, parameterTypes); } }
ReflectionUtil { public static <T> T invokeMethod(Object obj, String methodName, Object... args) { Object[] theArgs = ArrayUtils.nullToEmpty(args); final Class<?>[] parameterTypes = ClassUtils.toClass(theArgs); return invokeMethod(obj, methodName, theArgs, parameterTypes); }  }
ReflectionUtil { public static <T> T invokeMethod(Object obj, String methodName, Object... args) { Object[] theArgs = ArrayUtils.nullToEmpty(args); final Class<?>[] parameterTypes = ClassUtils.toClass(theArgs); return invokeMethod(obj, methodName, theArgs, parameterTypes); }  static Method getSetterMethod(Class<?> clazz, String propertyName, Class<?> parameterType); static Method getGetterMethod(Class<?> clazz, String propertyName); static Method getMethod(final Class<?> clazz, final String methodName, Class<?>... parameterTypes); static Method getAccessibleMethodByName(final Class clazz, final String methodName); static Field getField(final Class clazz, final String fieldName); static T invokeGetter(Object obj, String propertyName); static void invokeSetter(Object obj, String propertyName, Object value); static T getFieldValue(final Object obj, final String fieldName); static T getFieldValue(final Object obj, final Field field); static void setFieldValue(final Object obj, final String fieldName, final Object value); static void setField(final Object obj, Field field, final Object value); static T getProperty(Object obj, String propertyName); static void setProperty(Object obj, String propertyName, final Object value); static T invokeMethod(Object obj, String methodName, Object... args); static T invokeMethod(final Object obj, final String methodName, final Object[] args,
			final Class<?>[] parameterTypes); static T invokeMethodByName(final Object obj, final String methodName, final Object[] args); static T invokeMethod(final Object obj, Method method, Object... args); static T invokeConstructor(final Class<T> cls, Object... args); static void makeAccessible(Method method); static void makeAccessible(Field field); static RuntimeException convertReflectionExceptionToUnchecked(Exception e); }
ReflectionUtil { public static <T> T invokeMethod(Object obj, String methodName, Object... args) { Object[] theArgs = ArrayUtils.nullToEmpty(args); final Class<?>[] parameterTypes = ClassUtils.toClass(theArgs); return invokeMethod(obj, methodName, theArgs, parameterTypes); }  static Method getSetterMethod(Class<?> clazz, String propertyName, Class<?> parameterType); static Method getGetterMethod(Class<?> clazz, String propertyName); static Method getMethod(final Class<?> clazz, final String methodName, Class<?>... parameterTypes); static Method getAccessibleMethodByName(final Class clazz, final String methodName); static Field getField(final Class clazz, final String fieldName); static T invokeGetter(Object obj, String propertyName); static void invokeSetter(Object obj, String propertyName, Object value); static T getFieldValue(final Object obj, final String fieldName); static T getFieldValue(final Object obj, final Field field); static void setFieldValue(final Object obj, final String fieldName, final Object value); static void setField(final Object obj, Field field, final Object value); static T getProperty(Object obj, String propertyName); static void setProperty(Object obj, String propertyName, final Object value); static T invokeMethod(Object obj, String methodName, Object... args); static T invokeMethod(final Object obj, final String methodName, final Object[] args,
			final Class<?>[] parameterTypes); static T invokeMethodByName(final Object obj, final String methodName, final Object[] args); static T invokeMethod(final Object obj, Method method, Object... args); static T invokeConstructor(final Class<T> cls, Object... args); static void makeAccessible(Method method); static void makeAccessible(Field field); static RuntimeException convertReflectionExceptionToUnchecked(Exception e);  }