ReflectionUtil { public static Object invokeMethod(Method method, Object target, Object...args) { if (method == null) { return null; } method.setAccessible(true); try { return method.invoke(target, args); } catch (Exception ex) { throw ExceptionUtil.toRuntimeException(ex); } }  static Method[] getAllMethodsOfClass(final Class<?> clazz); static Method[] getAllMethodsOfClass(Class<?> clazz, boolean accessible); static Method[] getAllInstanceMethods(final Class<?> clazz); static Method[] getAllInstanceMethods(Class<?> clazz, boolean accessible); static List<A> findAllMethodAnnotation(Class<T> clazz, Class<A> annotationType); static List<Method> getAnnotationMethods(Class<T> clazz, Class<A> annotationType); static Object invokeMethod(Method method, Object target, Object...args); static Object invokeMethod(Object object, String methodName, Object[] args, Class<?>...parameterTypes); static Object invokeStaticMethod(Class<?> clazz, String methodName, Object[] args, Class<?>...parameterTypes); static Method getMethod(Class<?> clazz, String methodName, Class<?>...parameterTypes); static Method getMethod(Object object, String methodName, Class<?>...parameterTypes); static Method[] getAccessibleMethods(Class<?> clazz); static Method[] getAccessibleMethods(Class<?> clazz, Class<?> limit); static boolean hasAnnotation(Method method, Class<A> annotationType); static Field[] getAllFieldsOfClass(Class<?> clazz); static Field[] getAllInstanceFields(Class<?> clazz); static Field[] getAllFieldsOfClass(Class<?> clazz, boolean accessible); static Field[] getAllFieldsOfClass(Object object); static Field[] getAllFieldsOfClass(Object object, boolean accessible); static Field[] getAllInstanceFields(Class<?> clazz, boolean accessible); static Field[] getAllInstanceFields(Object object); static Field[] getAllInstanceFields(Object object, boolean accessible); static Field[] getInstanceFields(Class<?> clazz, String[] includes); static Field[] getAnnotationFields(Class<?> clazz, Class<? extends Annotation> annotationClass); static Field getField(Object object, String fieldName); static Field getField(Class<?> clazz, String fieldName); @Deprecated static Field getFieldOfClass(Object object, String fieldName); static Field getField(Object object, String fieldName, boolean accessible); static Field getField(Class<?> clazz, String fieldName, boolean accessible); @Deprecated static Field getFieldOfClass(Class<?> clazz, String fieldName); @Deprecated static Field getFieldOfClass(Object object, String fieldName, boolean accessible); static Field[] getAccessibleFields(Class<?> clazz); static Field[] getAccessibleFields(Class<?> clazz, Class<?> limit); static T readField(Field field, Object target); static Object readField(String fieldName, Object target); static void writeField(Field field, Object target, Object value); static void writeField(Object target, String fieldName, Object value); static Object readStaticField(Field field); static Object readStaticField(Class<?> clazz, String fieldName); static void writeStaticField(Field field, Object value); static void writeStaticField(Class<?> clazz, String fieldName, Object value); static boolean notWriter(Field field); static boolean isFinal(Field field); static boolean hasSuperClass(Class<?> clazz); static boolean hasAnnotation(Field field, Class<A> annotationType); static Class<?> getComponentType(Class<?> clazz); static Class<?> getComponentType(Class<?> clazz, int index); static Class<?>[] getComponentTypes(Class<?> clazz); static Class<?>[] getComponentTypesRecursion(Class<?> clazz); static Class<?> getComponentTypeRecursion(Class<?> clazz); static Class<?> getComponentTypeRecursion(Class<?> clazz, int index); static Class<?> getComponentType(Type type); static Class<?> getComponentType(Type type, Class<?> implClass); static Class<?>[] getComponentTypes(Type type); static Class<?>[] getComponentTypes(Type type, Class<?> implClass); static Class<?>[] getGenericSuperTypes(Class<?> type); static Class<?> getGenericSuperType(Class<?> type); static Class<?> getRawType(Type type); static Class<?> getRawType(Type type, Class<?> implClass); static Type resolveVariable(TypeVariable<?> variable, final Class<?> implClass); static boolean isAccessible(Member m); static boolean isPublic(Member m); static void forceAccess(AccessibleObject object);  }
@Test public void invokeMethod() { assertNull(ReflectionUtil.invokeMethod(null, null)); assertNull(ReflectionUtil.invokeMethod(null, new Object(), new Object())); assertNull(ReflectionUtil.invokeMethod(null, new Object())); assertNull(ReflectionUtil.invokeMethod((Object) null, (String) null, null)); assertNull(ReflectionUtil.invokeMethod((Object) null, (String) null, (Object[]) null, (Class<?>) null)); assertNull(ReflectionUtil.invokeMethod("", (String) null, (Object[]) null, (Class<?>) null)); assertNull(ReflectionUtil.invokeMethod((Object) null, (String) null, new Object[] {}, (Class<?>) null)); assertNull(ReflectionUtil.invokeMethod((Object) null, (String) null, (Object[]) null, Emptys.EMPTY_CLASS_ARRAY)); assertNull(ReflectionUtil.invokeMethod(null, new Object(), new Object())); assertNull(ReflectionUtil.invokeMethod(null, new Object())); Method method = null; try { method = String.class.getMethod("valueOf", int.class); assertEquals("1", ReflectionUtil.invokeMethod(method, null, 1)); assertEquals("1", ReflectionUtil.invokeMethod(method, "", 1)); assertEquals("1", ReflectionUtil.invokeMethod(method, new Object(), 1)); method = String.class.getMethod("trim"); assertEquals("xxx", ReflectionUtil.invokeMethod(method, " xxx ")); assertEquals("xxx", ReflectionUtil.invokeMethod(method, new Object())); } catch (Exception e) { assertTrue(e instanceof RuntimeException); } List<String> list = CollectionUtil.createArrayList(); try { method = ArrayList.class.getDeclaredMethod("RangeCheck", int.class); ReflectionUtil.invokeMethod(method, list, Integer.MAX_VALUE); } catch (Exception e) { InvocationTargetException ex = (InvocationTargetException) e.getCause(); if (ex != null) { assertTrue(ex.getTargetException() instanceof IndexOutOfBoundsException); } } try { assertEquals("xxx", ReflectionUtil.invokeMethod(" xxx ", "trim", null)); assertEquals("xxx", ReflectionUtil.invokeMethod(new Object(), "trim", null)); } catch (Exception e) { assertTrue(e instanceof RuntimeException); } list = CollectionUtil.createArrayList(); try { ReflectionUtil.invokeMethod(list, "RangeCheck", new Object[] { Integer.MAX_VALUE }, int.class); } catch (Exception e) { if (e.getCause() instanceof NoSuchMethodException) { } else { InvocationTargetException ex = (InvocationTargetException) e.getCause(); assertTrue(ex.getTargetException() instanceof IndexOutOfBoundsException); } } }