Class MetaAnnotationUtils
- java.lang.Object
- org.springframework.test.util.MetaAnnotationUtils
public abstract class MetaAnnotationUtils extends Object
MetaAnnotationUtilsis a collection of utility methods that complements the standard support already available inAnnotationUtils.Whereas
AnnotationUtilsprovides utilities for getting or finding an annotation,MetaAnnotationUtilsgoes a step further by providing support for determining the root class on which an annotation is declared, either directly or indirectly via a composed annotation. This additional information is encapsulated in anMetaAnnotationUtils.AnnotationDescriptor.The additional information provided by an
AnnotationDescriptoris required by the Spring TestContext Framework in order to be able to support class hierarchy traversals for annotations such as@ContextConfiguration,@TestExecutionListeners, and@ActiveProfileswhich offer support for merging and overriding various inherited annotation attributes (e.g.ContextConfiguration.inheritLocations()).- Since:
- 4.0
- Author:
- Sam Brannen
- See Also:
AnnotationUtils,MetaAnnotationUtils.AnnotationDescriptor
Nested Class Summary
Nested Classes Modifier and Type Class Description static classMetaAnnotationUtils.AnnotationDescriptor<T extends Annotation>Descriptor for anAnnotation, including the class on which the annotation is declared as well as the actual annotation instance.static classMetaAnnotationUtils.UntypedAnnotationDescriptorUntyped extension ofAnnotationDescriptorthat is used to describe the declaration of one of several candidate annotation types where the actual annotation type cannot be predetermined.
Constructor Summary
Constructors Constructor Description MetaAnnotationUtils()
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static <T extends Annotation>
MetaAnnotationUtils.AnnotationDescriptor<T>findAnnotationDescriptor(Class<?> clazz, Class<T> annotationType)Find theMetaAnnotationUtils.AnnotationDescriptorfor the suppliedannotationTypeon the suppliedClass, traversing its annotations, interfaces, and superclasses if no annotation can be found on the given class itself.static MetaAnnotationUtils.UntypedAnnotationDescriptorfindAnnotationDescriptorForTypes(Class<?> clazz, Class<? extends Annotation>... annotationTypes)Find theMetaAnnotationUtils.UntypedAnnotationDescriptorfor the firstClassin the inheritance hierarchy of the specifiedclazz(including the specifiedclazzitself) which declares at least one of the specifiedannotationTypes.
Constructor Detail
MetaAnnotationUtils
public MetaAnnotationUtils()
Method Detail
findAnnotationDescriptor
@Nullable public static <T extends Annotation> MetaAnnotationUtils.AnnotationDescriptor<T> findAnnotationDescriptor(Class<?> clazz, Class<T> annotationType)
Find theMetaAnnotationUtils.AnnotationDescriptorfor the suppliedannotationTypeon the suppliedClass, traversing its annotations, interfaces, and superclasses 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 meta-annotations.
The algorithm operates as follows:
- Search for the annotation on the given class and return a corresponding
AnnotationDescriptorif found. - Recursively search through all annotations that the given class declares.
- Recursively search through all interfaces implemented by the given class.
- Recursively search through the superclass hierarchy of the given class.
In this context, the term recursively means that the search process continues by returning to step #1 with the current annotation, interface, or superclass as the class to look for annotations on.
- Parameters:
clazz- the class to look for annotations onannotationType- the type of annotation to look for- Returns:
- the corresponding annotation descriptor if the annotation was found; otherwise
null - See Also:
findAnnotationDescriptorForTypes(Class, Class...)
- Search for the annotation on the given class and return a corresponding
findAnnotationDescriptorForTypes
@Nullable public static MetaAnnotationUtils.UntypedAnnotationDescriptor findAnnotationDescriptorForTypes(Class<?> clazz, Class<? extends Annotation>... annotationTypes)
Find theMetaAnnotationUtils.UntypedAnnotationDescriptorfor the firstClassin the inheritance hierarchy of the specifiedclazz(including the specifiedclazzitself) which declares at least one of the specifiedannotationTypes.This method traverses the annotations, interfaces, and superclasses of the specified
clazzif 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 meta-annotations.
The algorithm operates as follows:
- Search for a local declaration of one of the annotation types on the given class and return a corresponding
UntypedAnnotationDescriptorif found. - Recursively search through all annotations that the given class declares.
- Recursively search through all interfaces implemented by the given class.
- Recursively search through the superclass hierarchy of the given class.
In this context, the term recursively means that the search process continues by returning to step #1 with the current annotation, interface, or superclass as the class to look for annotations on.
- Parameters:
clazz- the class to look for annotations onannotationTypes- the types of annotations to look for- Returns:
- the corresponding annotation descriptor if one of the annotations was found; otherwise
null - See Also:
findAnnotationDescriptor(Class, Class)
- Search for a local declaration of one of the annotation types on the given class and return a corresponding