@Test public void testInvokeMethod() throws NoSuchMethodException, IllegalAccessException, InvocationTargetException { Assertions.assertEquals(0, ReflectionUtil.invokeMethod("", "length")); Assertions.assertEquals(3, ReflectionUtil.invokeMethod("foo", "length")); Assertions.assertThrows(NoSuchMethodException.class, () -> ReflectionUtil.invokeMethod(new String(), "size")); }
public static Object invokeMethod(Object target, String methodName) throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException { Class<?> cl = target.getClass(); int i = 0; while ((i++) < MAX_NEST_DEPTH && cl != null) { try { Method m = cl.getDeclaredMethod(methodName); m.setAccessible(true); return m.invoke(target); } catch (Exception e) { cl = cl.getSuperclass(); } } throw new NoSuchMethodException("class:" + target.getClass() + ", methodName:" + methodName); }
ReflectionUtil { public static Object invokeMethod(Object target, String methodName) throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException { Class<?> cl = target.getClass(); int i = 0; while ((i++) < MAX_NEST_DEPTH && cl != null) { try { Method m = cl.getDeclaredMethod(methodName); m.setAccessible(true); return m.invoke(target); } catch (Exception e) { cl = cl.getSuperclass(); } } throw new NoSuchMethodException("class:" + target.getClass() + ", methodName:" + methodName); } }
ReflectionUtil { public static Object invokeMethod(Object target, String methodName) throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException { Class<?> cl = target.getClass(); int i = 0; while ((i++) < MAX_NEST_DEPTH && cl != null) { try { Method m = cl.getDeclaredMethod(methodName); m.setAccessible(true); return m.invoke(target); } catch (Exception e) { cl = cl.getSuperclass(); } } throw new NoSuchMethodException("class:" + target.getClass() + ", methodName:" + methodName); }  }
ReflectionUtil { public static Object invokeMethod(Object target, String methodName) throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException { Class<?> cl = target.getClass(); int i = 0; while ((i++) < MAX_NEST_DEPTH && cl != null) { try { Method m = cl.getDeclaredMethod(methodName); m.setAccessible(true); return m.invoke(target); } catch (Exception e) { cl = cl.getSuperclass(); } } throw new NoSuchMethodException("class:" + target.getClass() + ", 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 invokeMethod(Object target, String methodName) throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException { Class<?> cl = target.getClass(); int i = 0; while ((i++) < MAX_NEST_DEPTH && cl != null) { try { Method m = cl.getDeclaredMethod(methodName); m.setAccessible(true); return m.invoke(target); } catch (Exception e) { cl = cl.getSuperclass(); } } throw new NoSuchMethodException("class:" + target.getClass() + ", 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; }