Class TestContextAnnotationUtils
TestContextAnnotationUtils
is a collection of utility methods that
complements the standard support already available in AnnotationUtils
and AnnotatedElementUtils
, while transparently honoring
@NestedTestConfiguration
semantics.
Mainly for internal use within the Spring TestContext Framework but also supported for third-party integrations with the TestContext framework.
Whereas AnnotationUtils
and AnnotatedElementUtils
provide
utilities for getting or finding annotations,
TestContextAnnotationUtils
goes 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 an TestContextAnnotationUtils.AnnotationDescriptor
.
The additional information provided by an AnnotationDescriptor
is
required by the Spring TestContext Framework in order to be able to
support class inheritance and enclosing class hierarchy traversals for
annotations such as @ContextConfiguration
,
@TestExecutionListeners
, and
@ActiveProfiles
which offer support for merging and
overriding various inherited annotation attributes — for
example, ContextConfiguration.inheritLocations()
.
- Since:
- 5.3, though originally since 4.0 as
org.springframework.test.util.MetaAnnotationUtils
- Author:
- Sam Brannen
- See Also:
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic class
Descriptor for anAnnotation
, including the class on which the annotation is declared as well as the merged annotation instance.static class
Untyped extension ofTestContextAnnotationUtils.AnnotationDescriptor
that is used to describe the declaration of one of several candidate annotation types where the actual annotation type cannot be predetermined. -
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic <T extends Annotation>
@Nullable TestContextAnnotationUtils.AnnotationDescriptor<T>findAnnotationDescriptor
(Class<?> clazz, Class<T> annotationType) Find theTestContextAnnotationUtils.AnnotationDescriptor
for the suppliedannotationType
on the suppliedClass
, traversing its annotations, interfaces, superclasses, and enclosing classes if no annotation can be found on the given class itself.findAnnotationDescriptorForTypes
(Class<?> clazz, Class<? extends Annotation>... annotationTypes) Find theTestContextAnnotationUtils.UntypedAnnotationDescriptor
for the firstClass
in the inheritance hierarchy of the specifiedclazz
(including the specifiedclazz
itself) which declares at least one of the specifiedannotationTypes
.static <T extends Annotation>
@Nullable TfindMergedAnnotation
(Class<?> clazz, Class<T> annotationType) Find the first annotation of the specifiedannotationType
within the annotation hierarchy above the supplied class, merge that annotation's attributes with matching attributes from annotations in lower levels of the annotation hierarchy, and synthesize the result back into an annotation of the specifiedannotationType
.static <T extends Annotation>
Set<T>getMergedRepeatableAnnotations
(Class<?> clazz, Class<T> annotationType) Get all repeatable annotations of the specifiedannotationType
within the annotation hierarchy above the supplied class; and for each annotation found, merge that annotation's attributes with matching attributes from annotations in lower levels of the annotation hierarchy and synthesize the results back into an annotation of the specifiedannotationType
.static boolean
hasAnnotation
(Class<?> clazz, Class<? extends Annotation> annotationType) Determine if an annotation of the specifiedannotationType
is present or meta-present on the suppliedClass
according to the search algorithm used infindMergedAnnotation(Class, Class)
.static boolean
searchEnclosingClass
(Class<?> clazz) Determine if annotations on the enclosing class of the supplied class should be searched by annotation search algorithms within the Spring TestContext Framework.
-
Constructor Details
-
TestContextAnnotationUtils
public TestContextAnnotationUtils()
-
-
Method Details
-
hasAnnotation
Determine if an annotation of the specifiedannotationType
is present or meta-present on the suppliedClass
according to the search algorithm used infindMergedAnnotation(Class, Class)
.If this method returns
true
, thenfindMergedAnnotation(...)
will return a non-null value.- Parameters:
clazz
- the class to look for annotations onannotationType
- the type of annotation to look for- Returns:
true
if a matching annotation is present- Since:
- 5.3.3
- See Also:
-
findMergedAnnotation
public static <T extends Annotation> @Nullable T findMergedAnnotation(Class<?> clazz, Class<T> annotationType) Find the first annotation of the specifiedannotationType
within the annotation hierarchy above the supplied class, merge that annotation's attributes with matching attributes from annotations in lower levels of the annotation hierarchy, and synthesize the result back into an annotation of the specifiedannotationType
.In the context of this method, the term "above" means within the superclass hierarchy or within the enclosing class hierarchy of the supplied class. The enclosing class hierarchy will only be searched according to
@NestedTestConfiguration
semantics.@AliasFor
semantics are fully supported, both within a single annotation and within annotation hierarchies.- Parameters:
clazz
- the class to look for annotations onannotationType
- the type of annotation to look for- Returns:
- the merged, synthesized
Annotation
, ornull
if not found - See Also:
-
getMergedRepeatableAnnotations
public static <T extends Annotation> Set<T> getMergedRepeatableAnnotations(Class<?> clazz, Class<T> annotationType) Get all repeatable annotations of the specifiedannotationType
within the annotation hierarchy above the supplied class; and for each annotation found, merge that annotation's attributes with matching attributes from annotations in lower levels of the annotation hierarchy and synthesize the results back into an annotation of the specifiedannotationType
.This method will find
@Inherited
annotations declared on superclasses if the supplied class does not have any local declarations of the repeatable annotation. If no inherited annotations are found, this method will search within the enclosing class hierarchy of the supplied class. The enclosing class hierarchy will only be searched according to@NestedTestConfiguration
semantics.The container type that holds the repeatable annotations will be looked up via
Repeatable
.@AliasFor
semantics are fully supported, both within a single annotation and within annotation hierarchies.- Parameters:
clazz
- the class on which to search for annotations (nevernull
)annotationType
- the annotation type to find (nevernull
)- Returns:
- the set of all merged repeatable annotations found, or an empty set if none were found
- See Also:
-
findAnnotationDescriptor
public static <T extends Annotation> @Nullable TestContextAnnotationUtils.AnnotationDescriptor<T> findAnnotationDescriptor(Class<?> clazz, Class<T> annotationType) Find theTestContextAnnotationUtils.AnnotationDescriptor
for the suppliedannotationType
on the suppliedClass
, traversing its annotations, interfaces, superclasses, and enclosing classes if no annotation can be found on the given class itself.This method explicitly handles class-level annotations which are not declared as inherited as well as meta-annotations.
The algorithm operates as follows:
- Search for the annotation on the given class and return a corresponding
AnnotationDescriptor
if 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.
- Recursively search through the enclosing class hierarchy of the given class
if appropriate according to
@NestedTestConfiguration
semantics.
In this context, the term recursively means that the search process continues by returning to step #1 with the current annotation, interface, superclass, or enclosing class 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:
- Search for the annotation on the given class and return a corresponding
-
findAnnotationDescriptorForTypes
public static @Nullable TestContextAnnotationUtils.UntypedAnnotationDescriptor findAnnotationDescriptorForTypes(Class<?> clazz, Class<? extends Annotation>... annotationTypes) Find theTestContextAnnotationUtils.UntypedAnnotationDescriptor
for the firstClass
in the inheritance hierarchy of the specifiedclazz
(including the specifiedclazz
itself) which declares at least one of the specifiedannotationTypes
.This method traverses the annotations, interfaces, superclasses, and enclosing classes of the specified
clazz
if no annotation can be found on the given class itself.This method explicitly handles class-level annotations which are not declared as inherited as 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
UntypedAnnotationDescriptor
if 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.
- Recursively search through the enclosing class hierarchy of the given class
if appropriate according to
@NestedTestConfiguration
semantics.
In this context, the term recursively means that the search process continues by returning to step #1 with the current annotation, interface, superclass, or enclosing class 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:
- Search for a local declaration of one of the annotation types on the
given class and return a corresponding
-
searchEnclosingClass
Determine if annotations on the enclosing class of the supplied class should be searched by annotation search algorithms within the Spring TestContext Framework.- Parameters:
clazz
- the class whose enclosing class should potentially be searched- Returns:
true
if the supplied class is an inner class whose enclosing class should be searched- See Also:
-