Class AnnotationUtils
As a general rule for runtime-retained annotations (e.g. for transaction control,
authorization or service exposure), always use the lookup methods on this class (e.g., findAnnotation(Method, Class), getAnnotation(Method, Class), and getAnnotations(Method)) instead of the plain annotation lookup methods in the JDK. You can
still explicitly choose between lookup on the given class level only (getAnnotation(Method, Class)) and lookup in the entire inheritance hierarchy of the given
method (findAnnotation(Method, Class)).
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic <A extends Annotation>
AfindAnnotation(Class<?> clazz, Class<A> annotationType) Find a singleAnnotationofannotationTypefrom the suppliedClass, traversing its interfaces and super classes if no annotation can be found on the given class itself.static <A extends Annotation>
AfindAnnotation(Method method, Class<A> annotationType) Get a singleAnnotationofannotationTypefrom the suppliedMethod, traversing its super methods if no annotation can be found on the given method itself.static Class<?>findAnnotationDeclaringClass(Class<? extends Annotation> annotationType, Class<?> clazz) Find the firstClassin the inheritance hierarchy of the specifiedclazz(including the specifiedclazzitself) which declares an annotation for the specifiedannotationType, ornullif not found.static <A extends Annotation>
AgetAnnotation(Method method, Class<A> annotationType) getAnnotationAttributes(Annotation annotation) Retrieve the given annotation's attributes as a Map.static Annotation[]getAnnotations(Method method) Get allAnnotationsfrom the suppliedMethod.static ObjectgetDefaultValue(Annotation annotation) Retrieve the default value of the"value"attribute of a single-element Annotation, given an annotation instance.static ObjectgetDefaultValue(Annotation annotation, String attributeName) Retrieve the default value of a named Annotation attribute, given an annotation instance.static ObjectgetDefaultValue(Class<? extends Annotation> annotationType) Retrieve the default value of the"value"attribute of a single-element Annotation, given theannotation type.static ObjectgetDefaultValue(Class<? extends Annotation> annotationType, String attributeName) Retrieve the default value of a named Annotation attribute, given theannotation type.static ObjectgetValue(Annotation annotation) Retrieve the value of the"value"attribute of a single-element Annotation, given an annotation instance.static ObjectgetValue(Annotation annotation, String attributeName) Retrieve the value of a named Annotation attribute, given an annotation instance.static booleanisAnnotationDeclaredLocally(Class<? extends Annotation> annotationType, Class<?> clazz) Determine whether an annotation for the specifiedannotationTypeis declared locally on the suppliedclazz.static booleanisAnnotationInherited(Class<? extends Annotation> annotationType, Class<?> clazz) Determine whether an annotation for the specifiedannotationTypeis present on the suppliedclazzand isinherited(i.e., not declared locally for the class).
-
Constructor Details
-
AnnotationUtils
public AnnotationUtils()
-
-
Method Details
-
getAnnotations
Get allAnnotationsfrom the suppliedMethod.Correctly handles bridge
Methodsgenerated by the compiler.- Parameters:
method- the method to look for annotations on- Returns:
- the annotations found
- See Also:
-
BridgeMethodResolver.findBridgedMethod(Method)
-
getAnnotation
Get a singleAnnotationofannotationTypefrom the suppliedMethod.Correctly handles bridge
Methodsgenerated by the compiler.- Parameters:
method- the method to look for annotations onannotationType- the annotation class to look for- Returns:
- the annotations found
- See Also:
-
BridgeMethodResolver.findBridgedMethod(Method)
-
findAnnotation
Get a singleAnnotationofannotationTypefrom the suppliedMethod, traversing its super methods if no annotation can be found on the given method itself.Annotations on methods are not inherited by default, so we need to handle this explicitly.
- Parameters:
method- the method to look for annotations onannotationType- the annotation class to look for- Returns:
- the annotation of the given type, or
nullif none found
-
findAnnotation
Find a singleAnnotationofannotationTypefrom the suppliedClass, traversing its interfaces and super classes if no annotation can be found on the given class itself.This method explicitly handles class-level annotations which are not declared as
inheritedas well as annotations on interfaces.- Parameters:
clazz- the class to look for annotations onannotationType- the annotation class to look for- Returns:
- the annotation of the given type found, or
null
-
findAnnotationDeclaringClass
public static Class<?> findAnnotationDeclaringClass(Class<? extends Annotation> annotationType, Class<?> clazz) Find the firstClassin the inheritance hierarchy of the specifiedclazz(including the specifiedclazzitself) which declares an annotation for the specifiedannotationType, ornullif not found. If the suppliedclazzisnull,nullwill be returned.If the supplied
clazzis an interface, only the interface itself will be checked; the inheritance hierarchy for interfaces will not be traversed.The standard
ClassAPI does not provide a mechanism for determining which class in an inheritance hierarchy actually declares anAnnotation, so we need to handle this explicitly.- Parameters:
annotationType- the Class object corresponding to the annotation typeclazz- the Class object corresponding to the class on which to check for the annotation, ornull.- Returns:
- the first
Classin the inheritance hierarchy of the specifiedclazzwhich declares an annotation for the specifiedannotationType, ornullif not found. - See Also:
-
isAnnotationDeclaredLocally
public static boolean isAnnotationDeclaredLocally(Class<? extends Annotation> annotationType, Class<?> clazz) Determine whether an annotation for the specifiedannotationTypeis declared locally on the suppliedclazz. The suppliedClassobject may represent any type.Note: This method does not determine if the annotation is
inherited. For greater clarity regarding inherited annotations, consider usingisAnnotationInherited(Class, Class)instead.- Parameters:
annotationType- the Class object corresponding to the annotation typeclazz- the Class object corresponding to the class on which to check for the annotation- Returns:
trueif an annotation for the specifiedannotationTypeis declared locally on the suppliedclazz- See Also:
-
isAnnotationInherited
public static boolean isAnnotationInherited(Class<? extends Annotation> annotationType, Class<?> clazz) Determine whether an annotation for the specifiedannotationTypeis present on the suppliedclazzand isinherited(i.e., not declared locally for the class).If the supplied
clazzis an interface, only the interface itself will be checked. In accord with standard meta-annotation semantics, the inheritance hierarchy for interfaces will not be traversed. See theJavaDocfor the @Inherited meta-annotation for further details regarding annotation inheritance.- Parameters:
annotationType- the Class object corresponding to the annotation typeclazz- the Class object corresponding to the class on which to check for the annotation- Returns:
trueif an annotation for the specifiedannotationTypeis present on the suppliedclazzand isinherited- See Also:
-
getAnnotationAttributes
Retrieve the given annotation's attributes as a Map.- Parameters:
annotation- the annotation to retrieve the attributes for- Returns:
- the Map of annotation attributes, with attribute names as keys and corresponding attribute values as values
-
getValue
Retrieve the value of the"value"attribute of a single-element Annotation, given an annotation instance.- Parameters:
annotation- the annotation instance from which to retrieve the value- Returns:
- the attribute value, or
nullif not found - See Also:
-
getValue
Retrieve the value of a named Annotation attribute, given an annotation instance.- Parameters:
annotation- the annotation instance from which to retrieve the valueattributeName- the name of the attribute value to retrieve- Returns:
- the attribute value, or
nullif not found - See Also:
-
getDefaultValue
Retrieve the default value of the"value"attribute of a single-element Annotation, given an annotation instance.- Parameters:
annotation- the annotation instance from which to retrieve the default value- Returns:
- the default value, or
nullif not found - See Also:
-
getDefaultValue
Retrieve the default value of a named Annotation attribute, given an annotation instance.- Parameters:
annotation- the annotation instance from which to retrieve the default valueattributeName- the name of the attribute value to retrieve- Returns:
- the default value of the named attribute, or
nullif not found. - See Also:
-
getDefaultValue
Retrieve the default value of the"value"attribute of a single-element Annotation, given theannotation type.- Parameters:
annotationType- the annotation type for which the default value should be retrieved- Returns:
- the default value, or
nullif not found - See Also:
-
getDefaultValue
public static Object getDefaultValue(Class<? extends Annotation> annotationType, String attributeName) Retrieve the default value of a named Annotation attribute, given theannotation type.- Parameters:
annotationType- the annotation type for which the default value should be retrievedattributeName- the name of the attribute value to retrieve.- Returns:
- the default value of the named attribute, or
nullif not found - See Also:
-