/** * Searches the method methodToFind in given class cls. If the method is found returns it, else return null. * * @param methodToFind is the method to search * @param cls is the class or interface where to search * @return method if it is found */ public static Method findMethod(Method methodToFind, Class<?> cls) { if (cls == null) { return null; } String methodToSearch = methodToFind.getName(); Class<?>[] soughtForParameterType = methodToFind.getParameterTypes(); Type[] soughtForGenericParameterType = methodToFind.getGenericParameterTypes(); for (Method method : cls.getMethods()) { if (method.getName().equals(methodToSearch) && method.getReturnType().isAssignableFrom(methodToFind.getReturnType())) { Class<?>[] srcParameterTypes = method.getParameterTypes(); Type[] srcGenericParameterTypes = method.getGenericParameterTypes(); if (soughtForParameterType.length == srcParameterTypes.length && soughtForGenericParameterType.length == srcGenericParameterTypes.length) { if (hasIdenticalParameters(srcParameterTypes, soughtForParameterType, srcGenericParameterTypes, soughtForGenericParameterType)) { return method; } } } } return null; }
/** * Searches the method methodToFind in given class cls. If the method is found returns it, else return null. * * @param methodToFind is the method to search * @param cls is the class or interface where to search * @return method if it is found */ public static Method findMethod(Method methodToFind, Class<?> cls) { if (cls == null) { return null; } String methodToSearch = methodToFind.getName(); Class<?>[] soughtForParameterType = methodToFind.getParameterTypes(); Type[] soughtForGenericParameterType = methodToFind.getGenericParameterTypes(); for (Method method : cls.getMethods()) { if (method.getName().equals(methodToSearch) && method.getReturnType().isAssignableFrom(methodToFind.getReturnType())) { Class<?>[] srcParameterTypes = method.getParameterTypes(); Type[] srcGenericParameterTypes = method.getGenericParameterTypes(); if (soughtForParameterType.length == srcParameterTypes.length && soughtForGenericParameterType.length == srcGenericParameterTypes.length) { if (hasIdenticalParameters(srcParameterTypes, soughtForParameterType, srcGenericParameterTypes, soughtForGenericParameterType)) { return method; } } } } return null; }