Reflections { public static Method method(Class clazz, String name, Class<?>... params) throws NoSuchMethodException { List<String> paramClassNames = new ArrayList<>(); for (Class<?> c : params) { paramClassNames.add(c.getCanonicalName()); } final String fullName = clazz.getCanonicalName() + "." + name + "(" + join("+", paramClassNames) + ")"; if (!methods.containsKey(fullName)) { Method method = clazz.getMethod(name, params); methods.put(fullName, method); } return methods.get(fullName); }  static Method method(Class clazz, String name, Class<?>... params); static Constructor constructor(Class clazz, Class<?>... params);  }
@Test public void test_method_no_params_object() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException { SomeModel byHandModel = new SomeModel(); Method method = Reflections.method(SomeModel.class, "getA"); assertEquals(method.invoke(byHandModel), byHandModel.getA()); Reflections.method(SomeModel.class, "setA", int.class); Method method2 = Reflections.method(SomeModel.class, "getA"); assertEquals(method, method2); }
@Test public void test_method_with_params_object() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException { SomeModel byHandModel = new SomeModel(); byHandModel.setA(10); SomeModel autoModel = new SomeModel(); Method method = Reflections.method(SomeModel.class, "setA", int.class); method.invoke(autoModel, 10); assertEquals(autoModel.getA(), byHandModel.getA()); }
@Test public void test_method_static() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException { Method method = Reflections.method(SomeModel.class, "staticZero"); assertEquals(method.invoke(null), SomeModel.staticZero()); }