/** * Returns a set of Methods that contain the given method annotation. This includes all public, protected, package * and private methods, as well as those of superclasses. Note that this does *not* include overridden methods. * * @param c class to inspect * @param annotationType the type of annotation to look for * @return List of Method objects that require injection. */ public static List<Method> getAllMethods(Class<?> c, Class<? extends Annotation> annotationType) { List<Method> annotated = new ArrayList<>(); inspectRecursively(c, annotated, annotationType); return annotated; }
/** * Inspects a class and its superclasses (all the way to {@link Object} for method instances that contain a given * annotation. This even identifies private, package and protected methods, not just public ones. */ private static void inspectRecursively(Class<?> c, List<Method> s, Class<? extends Annotation> annotationType) { for (Method m : c.getDeclaredMethods()) { // don't bother if this method has already been overridden by a subclass if (notFound(m, s) && m.isAnnotationPresent(annotationType)) { s.add(m); } } if (!c.equals(Object.class)) { if (!c.isInterface()) { inspectRecursively(c.getSuperclass(), s, annotationType); } for (Class<?> ifc : c.getInterfaces()) inspectRecursively(ifc, s, annotationType); } }