ReflectUtils { @Deprecated public static Method findMethodByMethodSignature(Class<?> clazz, String methodName, String[] parameterTypes) throws NoSuchMethodException, ClassNotFoundException { String signature = clazz.getName() + "." + methodName; if (parameterTypes != null && parameterTypes.length > 0) { signature += StringUtils.join(parameterTypes); } Method method = SIGNATURE_METHODS_CACHE.get(signature); if (method != null) { return method; } if (parameterTypes == null) { List<Method> finded = new ArrayList<Method>(); for (Method m : clazz.getMethods()) { if (m.getName().equals(methodName)) { finded.add(m); } } if (finded.isEmpty()) { throw new NoSuchMethodException("No such method " + methodName + " in class " + clazz); } if (finded.size() > 1) { String msg = String.format("Not unique method for method name(%s) in class(%s), find %d methods.", methodName, clazz.getName(), finded.size()); throw new IllegalStateException(msg); } method = finded.get(0); } else { Class<?>[] types = new Class<?>[parameterTypes.length]; for (int i = 0; i < parameterTypes.length; i++) { types[i] = ReflectUtils.name2class(parameterTypes[i]); } method = clazz.getMethod(methodName, types); } SIGNATURE_METHODS_CACHE.put(signature, method); return method; } private  ReflectUtils(); static boolean isPrimitives(Class<?> cls); static boolean isPrimitive(Class<?> cls); static Class<?> getBoxedClass(Class<?> c); static boolean isCompatible(Class<?> c, Object o); static boolean isCompatible(Class<?>[] cs, Object[] os); static String getCodeBase(Class<?> cls); static String getName(Class<?> c); static Class<?> getGenericClass(Class<?> cls); static Class<?> getGenericClass(Class<?> cls, int i); static String getName(final Method m); static String getSignature(String methodName, Class<?>[] parameterTypes); static String getName(final Constructor<?> c); static String getDesc(Class<?> c); static String getDesc(final Class<?>[] cs); static String getDesc(final Method m); static String[] getDescArray(final Method m); static String getDesc(final Constructor<?> c); static String getDescWithoutMethodName(Method m); static String getDesc(final CtClass c); static String getDesc(final CtMethod m); static String getDesc(final CtConstructor c); static String getDescWithoutMethodName(final CtMethod m); static String name2desc(String name); static String desc2name(String desc); static Class<?> forName(String name); static Class<?> forName(ClassLoader cl, String name); static Class<?> name2class(String name); static Class<?> desc2class(String desc); static Class<?>[] desc2classArray(String desc); @Deprecated static Method findMethodByMethodSignature(Class<?> clazz, String methodName, String[] parameterTypes); @Deprecated static Method findMethodByMethodName(Class<?> clazz, String methodName); static Constructor<?> findConstructor(Class<?> clazz, Class<?> paramType); static boolean isInstance(Object obj, String interfaceClazzName); static Object getEmptyObject(Class<?> returnType); static Object defaultReturn(Method m); static Object defaultReturn(Class<?> classType); static boolean isBeanPropertyReadMethod(Method method); static String getPropertyNameFromBeanReadMethod(Method method); static boolean isBeanPropertyWriteMethod(Method method); static String getPropertyNameFromBeanWriteMethod(Method method); static boolean isPublicInstanceField(Field field); static Map<String, Field> getBeanPropertyFields(Class cl); static Map<String, Method> getBeanPropertyReadMethods(Class cl); static Type[] getReturnTypes(Method method); static Set<ParameterizedType> findParameterizedTypes(Class<?> sourceClass); static Set<Class<T>> findHierarchicalTypes(Class<?> sourceClass, Class<T> matchType); static T getProperty(Object bean, String methodName); static Class[] resolveTypes(Object... values); static final char JVM_VOID; static final char JVM_BOOLEAN; static final char JVM_BYTE; static final char JVM_CHAR; static final char JVM_DOUBLE; static final char JVM_FLOAT; static final char JVM_INT; static final char JVM_LONG; static final char JVM_SHORT; static final Class<?>[] EMPTY_CLASS_ARRAY; static final String JAVA_IDENT_REGEX; static final String JAVA_NAME_REGEX; static final String CLASS_DESC; static final String ARRAY_DESC; static final String DESC_REGEX; static final Pattern DESC_PATTERN; static final String METHOD_DESC_REGEX; static final Pattern METHOD_DESC_PATTERN; static final Pattern GETTER_METHOD_DESC_PATTERN; static final Pattern SETTER_METHOD_DESC_PATTERN; static final Pattern IS_HAS_CAN_METHOD_DESC_PATTERN; }
@Test public void testFindMethodByMethodSignature() throws Exception { Method m = ReflectUtils.findMethodByMethodSignature(TestedClass.class, "method1", null); assertEquals("method1", m.getName()); Class<?>[] parameterTypes = m.getParameterTypes(); assertEquals(1, parameterTypes.length); assertEquals(int.class, parameterTypes[0]); }
@Test public void testFindMethodByMethodSignatureOverrideMoreThan1() throws Exception { try { ReflectUtils.findMethodByMethodSignature(TestedClass.class, "overrideMethod", null); fail(); } catch (IllegalStateException expected) { assertThat(expected.getMessage(), containsString("Not unique method for method name(")); } }
@Test public void testFindMethodByMethodSignatureNotFound() throws Exception { try { ReflectUtils.findMethodByMethodSignature(TestedClass.class, "notExsited", null); fail(); } catch (NoSuchMethodException expected) { assertThat(expected.getMessage(), containsString("No such method ")); assertThat(expected.getMessage(), containsString("in class")); } }