@Test public void test_findMethodByMethodSignature_notFound() 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")); } }
public static Method findMethodByMethodSignature(Class<?> clazz, String methodName, String[] parameterTypes) throws NoSuchMethodException, ClassNotFoundException { String signature = methodName; if (parameterTypes != null && parameterTypes.length > 0) { signature = methodName + StringUtils.join(parameterTypes); } Method method = Signature_METHODS_CACHE.get(signature); if (method != null) { return method; } if (parameterTypes == null) { List<Method> finded = new ArrayList<>(); 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; }
ReflectUtils { public static Method findMethodByMethodSignature(Class<?> clazz, String methodName, String[] parameterTypes) throws NoSuchMethodException, ClassNotFoundException { String signature = methodName; if (parameterTypes != null && parameterTypes.length > 0) { signature = methodName + StringUtils.join(parameterTypes); } Method method = Signature_METHODS_CACHE.get(signature); if (method != null) { return method; } if (parameterTypes == null) { List<Method> finded = new ArrayList<>(); 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; } }
ReflectUtils { public static Method findMethodByMethodSignature(Class<?> clazz, String methodName, String[] parameterTypes) throws NoSuchMethodException, ClassNotFoundException { String signature = methodName; if (parameterTypes != null && parameterTypes.length > 0) { signature = methodName + StringUtils.join(parameterTypes); } Method method = Signature_METHODS_CACHE.get(signature); if (method != null) { return method; } if (parameterTypes == null) { List<Method> finded = new ArrayList<>(); 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(); }
ReflectUtils { public static Method findMethodByMethodSignature(Class<?> clazz, String methodName, String[] parameterTypes) throws NoSuchMethodException, ClassNotFoundException { String signature = methodName; if (parameterTypes != null && parameterTypes.length > 0) { signature = methodName + StringUtils.join(parameterTypes); } Method method = Signature_METHODS_CACHE.get(signature); if (method != null) { return method; } if (parameterTypes == null) { List<Method> finded = new ArrayList<>(); 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 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<?> name2class(String name); static Class<?> desc2class(String desc); static Class<?>[] desc2classArray(String desc); static Method findMethodByMethodSignature(Class<?> clazz, String methodName, String[] parameterTypes); 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 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); }
ReflectUtils { public static Method findMethodByMethodSignature(Class<?> clazz, String methodName, String[] parameterTypes) throws NoSuchMethodException, ClassNotFoundException { String signature = methodName; if (parameterTypes != null && parameterTypes.length > 0) { signature = methodName + StringUtils.join(parameterTypes); } Method method = Signature_METHODS_CACHE.get(signature); if (method != null) { return method; } if (parameterTypes == null) { List<Method> finded = new ArrayList<>(); 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 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<?> name2class(String name); static Class<?> desc2class(String desc); static Class<?>[] desc2classArray(String desc); static Method findMethodByMethodSignature(Class<?> clazz, String methodName, String[] parameterTypes); 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 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 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; }