Class MethodAnnotationScanner<T,A extends Annotation>
- Type Parameters:
T
- Java type to be introspectedA
- Java annotation type
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()
.
-
Nested Class Summary
Modifier and TypeClassDescriptionclass
Holds information about an annotated method detected by aMethodAnnotationScanner
. -
Field Summary
-
Constructor Summary
ConstructorDescriptionMethodAnnotationScanner
(Class<T> type, Class<A> annotationType) Constructor. -
Method Summary
Modifier and TypeMethodDescriptionprotected MethodAnnotationScanner<T,
A>.MethodInfo createMethodInfo
(Method method, A annotation) Create a newMethodAnnotationScanner<T,
instance corresponding to the given annotated method.A extends Annotation>.MethodInfo Build set of annotated methods, with overridden annotated methods omitted.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.protected A
getAnnotation
(Method method) Get the annotation on the given method.protected boolean
includeMethod
(Method method, A annotation) Determine whether the annotated method should be included.static boolean
Determine if one method strictly overrides another.
-
Field Details
-
type
-
annotationType
-
-
Constructor Details
-
MethodAnnotationScanner
Constructor.- Parameters:
type
- Java class to be introspectedannotationType
- Java annotation type to search for- Throws:
IllegalArgumentException
- if either parameter is null
-
-
Method Details
-
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
Scan the given type and all its supertypes for annotated methods and add them to the list.- Parameters:
klass
- type to scan, possibly nullset
- set to add methods to
-
getAnnotation
Get the annotation on the given method.The implementation in
MethodAnnotationScanner
just invokesmethod.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
Determine whether the annotated method should be included.The implementation in
MethodAnnotationScanner
returns true ifmethod
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 checkannotation
- method's annotation- Returns:
- true to include method, false to ignore it
- Throws:
RuntimeException
- if method is erroneously annotated
-
overrides
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 tooriginal
's declaring class.If both methods are in the same class, then
override
must have a narrower return type thanoriginal
. This would normally indicate thatoriginal
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 overridesoriginal
, otherwise false; ifoverride
equalsoriginal
, false is returned
-
createMethodInfo
Create a newMethodAnnotationScanner<T,
instance corresponding to the given annotated method.A extends Annotation>.MethodInfo The implementation in
MethodAnnotationScanner
just instantiates aMethodAnnotationScanner<T,
directly.A extends Annotation>.MethodInfo - Parameters:
method
- the methodannotation
- the annotation annotating the method- Returns:
- new
MethodAnnotationScanner<T,
instanceA extends Annotation>.MethodInfo
-