ClassUtil { public static Method getMethod(Class<?> type, String name, Class<?> returnType, Class<?> paramType, boolean caseSensitive) throws IllegalAccessException, NoSuchMethodException { boolean methodNameFound = false; Class<?> classType = type; while (classType != null && !classType.equals(Object.class)) { for (Method m : classType.getDeclaredMethods()) { if ((!caseSensitive && m.getName().equalsIgnoreCase(name)) || (caseSensitive && m.getName().equals(name))) { methodNameFound = true; if (returnType != null) { if (!m.getReturnType().equals(returnType)) { throw new NoSuchMethodException("Method '" + name + "' was found in " + type.getSimpleName() + ".class" + ", but the returnType " + m.getReturnType().getSimpleName() + ".class did not match expected " + returnType.getSimpleName() + ".class"); } } else { if (!m.getReturnType().equals(void.class)) { throw new NoSuchMethodException("Method '" + name + "' was found in " + type.getSimpleName() + ".class" + ", but the returnType " + m.getReturnType().getSimpleName() + ".class was expected to be void"); } } Class<?>[] paramTypes = m.getParameterTypes(); if (paramType != null) { if (paramTypes.length != 1) { continue; } else { if (!paramTypes[0].equals(paramType)) { continue; } } } else { if (paramTypes.length != 0) { continue; } } if (!Modifier.isPublic(m.getModifiers())) { throw new IllegalAccessException("Method '" + name + "' was found in " + type.getSimpleName() + ".class "+ ", but its not accessible since its " + Modifier.toString(m.getModifiers())); } return m; } } classType = classType.getSuperclass(); } String signature = "public " + (returnType == null ? "void" : returnType.getName()) + " " + name + "(" + (paramType == null ? "" : paramType.getName()) + ")"; if (methodNameFound) { throw new NoSuchMethodException("Method '" + signature + "' was found in " + type.getSimpleName() + ".class, but signature match failed"); } else { throw new NoSuchMethodException("Method '" + signature + "' was not found in " + type.getSimpleName() + ".class"); } }  static Object findEnumConstant(Class<?> type, String constantName); static Object findEnumConstant(Class<?> type, String constantName, boolean caseSensitive); static Class<?>[] getClassHierarchy(Class<?> type); static boolean hasBeanMethods(Class<?> type, String propertyName, Class<?> propertyType, boolean caseSensitive); static Method[] getBeanMethods(Class<?> type, String propertyName, Class<?> propertyType, boolean caseSensitive); static Method getMethod(Class<?> type, String name, Class<?> returnType, Class<?> paramType, boolean caseSensitive);  }
@Test public void getMethodCaseSensitiveGet() throws Exception { Method m = ClassUtil.getMethod(InternetPerson.class, "getFirstName", String.class, null, true); Method expectedMethod = Person.class.getMethod("getFirstName"); Assert.assertEquals(expectedMethod, m); }
@Test public void getMethodCaseSensitiveSet() throws Exception { Method m = ClassUtil.getMethod(InternetPerson.class, "setFirstName", null, String.class, true); Method expectedMethod = Person.class.getMethod("setFirstName", String.class); Assert.assertEquals(expectedMethod, m); }
@Test(expected=NoSuchMethodException.class) public void getMethodCaseSensitiveInvalidMethod() throws Exception { Method m = ClassUtil.getMethod(InternetPerson.class, "getfirstname", String.class, null, true); }
@Test(expected=IllegalAccessException.class) public void getMethodCaseSensitiveProtected() throws Exception { Method m = ClassUtil.getMethod(InternetPerson.class, "getLastName2", String.class, null, true); }
@Test(expected=NoSuchMethodException.class) public void getMethodCaseSensitiveBadReturnType() throws Exception { Method m = ClassUtil.getMethod(InternetPerson.class, "getLastName", Object.class, null, true); }
@Test(expected=NoSuchMethodException.class) public void getMethodCaseSensitiveBadParameterType() throws Exception { Method m = ClassUtil.getMethod(InternetPerson.class, "getLastName", String.class, String.class, true); }
@Test public void getMethodCaseSensitiveNoReturnOrParam() throws Exception { try { Method m = ClassUtil.getMethod(InternetPerson.class, "setLastName", null, null, true); } catch (NoSuchMethodException e) { if (e.getMessage().indexOf("signature") < 0) { Assert.fail("Expected exception was correct, but 'signature match failed' was not found in its message!"); } } }
@Test public void getMethodCaseSensitiveNoReturnBadParam() throws Exception { try { Method m = ClassUtil.getMethod(InternetPerson.class, "setLastName", null, Object.class, true); } catch (NoSuchMethodException e) { if (e.getMessage().indexOf("signature") < 0) { Assert.fail("Expected exception was correct, but 'signature match failed' was not found in its message!"); } } }
@Test public void getMethodCaseInsensitiveGet() throws Exception { Method m = ClassUtil.getMethod(InternetPerson.class, "getfirstName", String.class, null, false); Method expectedMethod = Person.class.getMethod("getFirstName"); Assert.assertEquals(expectedMethod, m); }
@Test public void getMethodCaseInsensitiveSet() throws Exception { Method m = ClassUtil.getMethod(InternetPerson.class, "setfirstname", null, String.class, false); Method expectedMethod = Person.class.getMethod("setFirstName", String.class); Assert.assertEquals(expectedMethod, m); }