ReflectionHelper { Method getMethod(Class<?> type, Class<?> returnType, String name, Class<?>... parameters) { Method method = null; try { method = type.getMethod(name, parameters); if (null != returnType && !returnType.isAssignableFrom(method.getReturnType())) { method = null; } } catch (NoSuchMethodException nsme) { log.trace(nsme.getMessage(), nsme); } if (null == method) { method = getNonPublicMethod(type, returnType, name, parameters); } return method; }  T newInstance(Class<T> clazz); T newInstance(String className);  }
@Test public void testGetMethod() throws Exception { Method method; method = reflectionHelper.getMethod(SimpleBaseTo.class, null, "setA", String.class); assertThat(method.getName()).isEqualTo("setA"); method = reflectionHelper.getMethod(SimpleBaseTo.class, null, "setA", Integer.class); assertThat(method).isNull(); method = reflectionHelper.getMethod(SimpleBaseTo.class, String.class, "getA"); assertThat(method.getName()).isEqualTo("getA"); method = reflectionHelper.getMethod(SimpleBaseTo.class, Integer.class, "getA"); assertThat(method).isNull(); method = reflectionHelper.getMethod(SimpleBaseTo.class, Object.class, "getA"); assertThat(method.getName()).isEqualTo("getA"); method = reflectionHelper.getMethod(SimpleBaseTo.class, null, "bla"); assertThat(method).isNull(); }
@Test public void testGetMethod_extendedAndPrivate() throws Exception { Method method; method = reflectionHelper.getMethod(MaleHumanTo.class, null, "setName", String.class); assertThat(method.getName()).isEqualTo("setName"); method = reflectionHelper.getMethod(MixedMethodAndFieldDomain.class, String.class, "getFieldWithPrivateAccessors"); assertThat(method.getName()).isEqualTo("getFieldWithPrivateAccessors"); method = reflectionHelper.getMethod(MaleHumanTo.class, null, "setNameAlternate", String.class); assertThat(method.getName()).isEqualTo("setNameAlternate"); method = reflectionHelper.getMethod(MixedMethodAndFieldDomain.class, String.class, "getFieldDouble"); assertThat(method.getName()).isEqualTo("getFieldDouble"); }