@Test public void testInvokeMethod3() throws NoSuchMethodException, IllegalAccessException, InvocationTargetException { Assertions.assertEquals("0", ReflectionUtil.invokeStaticMethod( String.class, "valueOf", new Class<?>[]{int.class}, new Object[]{0})); Assertions.assertEquals("123", ReflectionUtil.invokeStaticMethod( String.class, "valueOf", new Class<?>[]{int.class}, new Object[]{123})); Assertions.assertThrows(NoSuchMethodException.class, () -> ReflectionUtil .invokeStaticMethod(String.class, "size", null, null)); }
public static Object invokeStaticMethod(Class<?> targetClass, String methodName, Class<?>[] parameterTypes, Object[] parameterValues) throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException { int i = 0; while ((i++) < MAX_NEST_DEPTH && targetClass != null) { try { Method m = targetClass.getMethod(methodName, parameterTypes); return m.invoke(null, parameterValues); } catch (Exception e) { targetClass = targetClass.getSuperclass(); } } throw new NoSuchMethodException("class:" + targetClass + ", methodName:" + methodName); }
ReflectionUtil { public static Object invokeStaticMethod(Class<?> targetClass, String methodName, Class<?>[] parameterTypes, Object[] parameterValues) throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException { int i = 0; while ((i++) < MAX_NEST_DEPTH && targetClass != null) { try { Method m = targetClass.getMethod(methodName, parameterTypes); return m.invoke(null, parameterValues); } catch (Exception e) { targetClass = targetClass.getSuperclass(); } } throw new NoSuchMethodException("class:" + targetClass + ", methodName:" + methodName); } }
ReflectionUtil { public static Object invokeStaticMethod(Class<?> targetClass, String methodName, Class<?>[] parameterTypes, Object[] parameterValues) throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException { int i = 0; while ((i++) < MAX_NEST_DEPTH && targetClass != null) { try { Method m = targetClass.getMethod(methodName, parameterTypes); return m.invoke(null, parameterValues); } catch (Exception e) { targetClass = targetClass.getSuperclass(); } } throw new NoSuchMethodException("class:" + targetClass + ", methodName:" + methodName); }  }
ReflectionUtil { public static Object invokeStaticMethod(Class<?> targetClass, String methodName, Class<?>[] parameterTypes, Object[] parameterValues) throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException { int i = 0; while ((i++) < MAX_NEST_DEPTH && targetClass != null) { try { Method m = targetClass.getMethod(methodName, parameterTypes); return m.invoke(null, parameterValues); } catch (Exception e) { targetClass = targetClass.getSuperclass(); } } throw new NoSuchMethodException("class:" + targetClass + ", methodName:" + methodName); }  static Class<?> getClassByName(String className); static Object getFieldValue(Object target, String fieldName); static Object invokeMethod(Object target, String methodName); static Object invokeMethod(Object target, String methodName, Class<?>[] parameterTypes, Object[] args); static Object invokeStaticMethod(Class<?> targetClass, String methodName, Class<?>[] parameterTypes,
                                            Object[] parameterValues); static Method getMethod(Class<?> classType, String methodName, Class<?>[] parameterTypes); static Set<Class<?>> getInterfaces(Class<?> clazz); }
ReflectionUtil { public static Object invokeStaticMethod(Class<?> targetClass, String methodName, Class<?>[] parameterTypes, Object[] parameterValues) throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException { int i = 0; while ((i++) < MAX_NEST_DEPTH && targetClass != null) { try { Method m = targetClass.getMethod(methodName, parameterTypes); return m.invoke(null, parameterValues); } catch (Exception e) { targetClass = targetClass.getSuperclass(); } } throw new NoSuchMethodException("class:" + targetClass + ", methodName:" + methodName); }  static Class<?> getClassByName(String className); static Object getFieldValue(Object target, String fieldName); static Object invokeMethod(Object target, String methodName); static Object invokeMethod(Object target, String methodName, Class<?>[] parameterTypes, Object[] args); static Object invokeStaticMethod(Class<?> targetClass, String methodName, Class<?>[] parameterTypes,
                                            Object[] parameterValues); static Method getMethod(Class<?> classType, String methodName, Class<?>[] parameterTypes); static Set<Class<?>> getInterfaces(Class<?> clazz); static final int MAX_NEST_DEPTH; }