Class MethodAnnotationScanner<T,A extends Annotation>

java.lang.Object
org.dellroad.stuff.java.MethodAnnotationScanner<T,A>
Type Parameters:
T - Java type to be introspected
A - Java annotation type

public class MethodAnnotationScanner<T,A extends Annotation> extends Object
Scan a class hierarchy for annotated methods in an override-aware manner.

Instances find all methods annotated with a specific annotation in a given Java class or any of its superclasses and superinterfaces, while also being override-aware, i.e., filtering out annotations on overridden supertype methods when the overriding method also has the annotation. This operation is performed by findAnnotatedMethods().

Subclasses may validate the annotations, and control which annotated methods to include and/or reject, by overriding getAnnotation() and/or includeMethod().

  • Field Details

    • type

      protected final Class<T> type
    • annotationType

      protected final Class<A extends Annotation> annotationType
  • Constructor Details

    • MethodAnnotationScanner

      public MethodAnnotationScanner(Class<T> type, Class<A> annotationType)
      Constructor.
      Parameters:
      type - Java class to be introspected
      annotationType - Java annotation type to search for
      Throws:
      IllegalArgumentException - if either parameter is null
  • Method Details

    • findAnnotatedMethods

      public Set<MethodAnnotationScanner<T,A>.MethodInfo> findAnnotatedMethods()
      Build set of annotated methods, with overridden annotated methods omitted.
      Returns:
      the set of all annotated methods, with overridden annotated methods removed when the overriding method is also annotated
    • findMethodInfos

      protected void findMethodInfos(Class<?> klass, Set<MethodAnnotationScanner<T,A>.MethodInfo> set)
      Scan the given type and all its supertypes for annotated methods and add them to the list.
      Parameters:
      klass - type to scan, possibly null
      set - set to add methods to
    • getAnnotation

      protected A getAnnotation(Method method)
      Get the annotation on the given method.

      The implementation in MethodAnnotationScanner just invokes method.getAnnotation(). Subclasses may override to automatically synthesize annotations, etc.

      Parameters:
      method - method in question
      Returns:
      annotation for method, or null if there is none
    • includeMethod

      protected boolean includeMethod(Method method, A annotation)
      Determine whether the annotated method should be included.

      The implementation in MethodAnnotationScanner returns true if method takes zero parameters and returns anything other than void, otherwise false. Note that this will include static methods.

      Subclasses may apply different tests and/or throw an exception if a method is improperly annotated.

      Parameters:
      method - method to check
      annotation - method's annotation
      Returns:
      true to include method, false to ignore it
      Throws:
      RuntimeException - if method is erroneously annotated
    • overrides

      public static boolean overrides(Method override, Method original)
      Determine if one method strictly overrides another.

      Both methods must be instance methods with the same name, and override's declaring class must be assignable to original's declaring class.

      If both methods are in the same class, then override must have a narrower return type than original. This would normally indicate that original is a bridge method.

      Parameters:
      override - possible overriding method (i.e., subclass method)
      original - possible overriding method (i.e., superclass method)
      Returns:
      true if override strictly overrides original, otherwise false; if override equals original, false is returned
    • createMethodInfo

      protected MethodAnnotationScanner<T,A>.MethodInfo createMethodInfo(Method method, A annotation)
      Create a new MethodAnnotationScanner<T,A extends Annotation>.MethodInfo instance corresponding to the given annotated method.

      The implementation in MethodAnnotationScanner just instantiates a MethodAnnotationScanner<T,A extends Annotation>.MethodInfo directly.

      Parameters:
      method - the method
      annotation - the annotation annotating the method
      Returns:
      new MethodAnnotationScanner<T,A extends Annotation>.MethodInfo instance