public abstract class TestContextAnnotationUtils extends Object
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.
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()
.
MetaAnnotationUtils
AnnotationUtils
,
AnnotatedElementUtils
,
TestContextAnnotationUtils.AnnotationDescriptor
Modifier and Type | Class and Description |
---|---|
static class |
TestContextAnnotationUtils.AnnotationDescriptor<T extends Annotation>
Descriptor for an
Annotation , including the class on which the annotation is declared
as well as the merged annotation instance. |
static class |
TestContextAnnotationUtils.UntypedAnnotationDescriptor
Untyped extension of
TestContextAnnotationUtils.AnnotationDescriptor that is used
to describe the declaration of one of several candidate annotation types
where the actual annotation type cannot be predetermined. |
Constructor and Description |
---|
TestContextAnnotationUtils() |
Modifier and Type | Method and Description |
---|---|
static <T extends Annotation> |
findAnnotationDescriptor(Class<?> clazz,
Class<T> annotationType)
Find the
TestContextAnnotationUtils.AnnotationDescriptor for the supplied annotationType
on the supplied Class , traversing its annotations, interfaces,
superclasses, and enclosing classes if no annotation can be found on the
given class itself. |
static TestContextAnnotationUtils.UntypedAnnotationDescriptor |
findAnnotationDescriptorForTypes(Class<?> clazz,
Class<? extends Annotation>... annotationTypes)
Find the
TestContextAnnotationUtils.UntypedAnnotationDescriptor for the first Class
in the inheritance hierarchy of the specified clazz (including
the specified clazz itself) which declares at least one of the
specified annotationTypes . |
static <T extends Annotation> |
findMergedAnnotation(Class<?> clazz,
Class<T> annotationType)
Find the first annotation of the specified
annotationType 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 specified annotationType . |
static <T extends Annotation> |
getMergedRepeatableAnnotations(Class<?> clazz,
Class<T> annotationType)
Get all repeatable annotations of the specified
annotationType
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 specified annotationType . |
static boolean |
hasAnnotation(Class<?> clazz,
Class<? extends Annotation> annotationType)
Determine if an annotation of the specified
annotationType is
present or meta-present on the supplied Class according to the
search algorithm used in findMergedAnnotation(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.
|
public static boolean hasAnnotation(Class<?> clazz, Class<? extends Annotation> annotationType)
annotationType
is
present or meta-present on the supplied Class
according to the
search algorithm used in findMergedAnnotation(Class, Class)
.
If this method returns true
, then findMergedAnnotation(...)
will return a non-null value.
clazz
- the class to look for annotations onannotationType
- the type of annotation to look fortrue
if a matching annotation is presentfindMergedAnnotation(Class, Class)
@Nullable public static <T extends Annotation> T findMergedAnnotation(Class<?> clazz, Class<T> annotationType)
annotationType
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 specified annotationType
.
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.
clazz
- the class to look for annotations onannotationType
- the type of annotation to look forAnnotation
, or null
if not foundAnnotatedElementUtils.findMergedAnnotation(java.lang.reflect.AnnotatedElement, Class)
,
findAnnotationDescriptor(Class, Class)
,
searchEnclosingClass(Class)
public static <T extends Annotation> Set<T> getMergedRepeatableAnnotations(Class<?> clazz, Class<T> annotationType)
annotationType
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 specified annotationType
.
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.
clazz
- the class on which to search for annotations (never null
)annotationType
- the annotation type to find (never null
)AnnotatedElementUtils.getMergedRepeatableAnnotations(java.lang.reflect.AnnotatedElement, Class)
,
searchEnclosingClass(Class)
@Nullable public static <T extends Annotation> TestContextAnnotationUtils.AnnotationDescriptor<T> findAnnotationDescriptor(Class<?> clazz, Class<T> annotationType)
TestContextAnnotationUtils.AnnotationDescriptor
for the supplied annotationType
on the supplied Class
, 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:
AnnotationDescriptor
if found.
@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.
clazz
- the class to look for annotations onannotationType
- the type of annotation to look fornull
findAnnotationDescriptorForTypes(Class, Class...)
@Nullable public static TestContextAnnotationUtils.UntypedAnnotationDescriptor findAnnotationDescriptorForTypes(Class<?> clazz, Class<? extends Annotation>... annotationTypes)
TestContextAnnotationUtils.UntypedAnnotationDescriptor
for the first Class
in the inheritance hierarchy of the specified clazz
(including
the specified clazz
itself) which declares at least one of the
specified annotationTypes
.
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:
UntypedAnnotationDescriptor
if found.
@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.
clazz
- the class to look for annotations onannotationTypes
- the types of annotations to look fornull
findAnnotationDescriptor(Class, Class)
public static boolean searchEnclosingClass(Class<?> clazz)
clazz
- the class whose enclosing class should potentially be searchedtrue
if the supplied class is an inner class whose enclosing
class should be searchedClassUtils.isInnerClass(Class)
,
@NestedTestConfiguration