Interface MergedAnnotations
- All Superinterfaces:
Iterable<MergedAnnotation<Annotation>>
Class or Method.
Each merged annotation represents a view where the attribute values may be "merged" from different source values, typically:
- Explicit and Implicit
@AliasFordeclarations on one or more attributes within the annotation - Explicit
@AliasFordeclarations for a meta-annotation - Convention based attribute aliases for a meta-annotation
- From a meta-annotation declaration
For example, a @PostMapping annotation might be defined as follows:
@Retention(RetentionPolicy.RUNTIME)
@RequestMapping(method = RequestMethod.POST)
public @interface PostMapping {
@AliasFor(attribute = "path")
String[] value() default {};
@AliasFor(attribute = "value")
String[] path() default {};
}
If a method is annotated with @PostMapping("/home") it will contain
merged annotations for both @PostMapping and the meta-annotation
@RequestMapping. The merged view of the @RequestMapping
annotation will contain the following attributes:
| Name | Value | Source |
|---|---|---|
| value | "/home" | Declared in @PostMapping |
| path | "/home" | Explicit @AliasFor |
| method | RequestMethod.POST | Declared in meta-annotation |
MergedAnnotations can be obtained from any Java AnnotatedElement. They may also be used for sources that
don't use reflection (such as those that directly parse bytecode).
Different search strategies can be used to locate
related source elements that contain the annotations to be aggregated. For
example, the following code uses MergedAnnotations.SearchStrategy.TYPE_HIERARCHY to
search for annotations on MyClass as well as in superclasses and implemented
interfaces.
MergedAnnotations mergedAnnotations =
MergedAnnotations.search(TYPE_HIERARCHY).from(MyClass.class);
From a MergedAnnotations instance you can either
get a single annotation, or stream all annotations or just those that match a specific type. You can also quickly tell if an annotation
is present.
Here are some typical examples:
// is an annotation present or meta-present?
mergedAnnotations.isPresent(ExampleAnnotation.class);
// get the merged "value" attribute of ExampleAnnotation (either directly or
// meta-present)
mergedAnnotations.get(ExampleAnnotation.class).getString("value");
// get all meta-annotations but no directly present annotations
mergedAnnotations.stream().filter(MergedAnnotation::isMetaPresent);
// get all ExampleAnnotation declarations (including any meta-annotations) and
// print the merged "value" attributes
mergedAnnotations.stream(ExampleAnnotation.class)
.map(mergedAnnotation -> mergedAnnotation.getString("value"))
.forEach(System.out::println);
NOTE: The MergedAnnotations API and its underlying model have
been designed for composable annotations in Spring's common component model,
with a focus on attribute aliasing and meta-annotation relationships.
There is no support for retrieving plain Java annotations with this API;
please use standard Java reflection or Spring's AnnotationUtils
for simple annotation retrieval purposes.
- Since:
- 5.2
- Author:
- Phillip Webb, Sam Brannen
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeInterfaceDescriptionstatic final classFluent API for configuring the search algorithm used in theMergedAnnotationsmodel and performing a search.static enumSearch strategies supported bysearch(SearchStrategy)as well asfrom(AnnotatedElement, SearchStrategy)and variants of that method. -
Method Summary
Modifier and TypeMethodDescriptionstatic MergedAnnotationsfrom(Annotation... annotations) Create a newMergedAnnotationsinstance from the specified annotations.static MergedAnnotationsfrom(Object source, Annotation... annotations) Create a newMergedAnnotationsinstance from the specified annotations.static MergedAnnotationsfrom(Object source, Annotation[] annotations, RepeatableContainers repeatableContainers) Create a newMergedAnnotationsinstance from the specified annotations.static MergedAnnotationsfrom(Object source, Annotation[] annotations, RepeatableContainers repeatableContainers, AnnotationFilter annotationFilter) Create a newMergedAnnotationsinstance from the specified annotations.static MergedAnnotationsfrom(AnnotatedElement element) Create a newMergedAnnotationsinstance containing all annotations and meta-annotations from the specified element.static MergedAnnotationsfrom(AnnotatedElement element, MergedAnnotations.SearchStrategy searchStrategy) Create a newMergedAnnotationsinstance containing all annotations and meta-annotations from the specified element and, depending on theMergedAnnotations.SearchStrategy, related inherited elements.static MergedAnnotationsfrom(AnnotatedElement element, MergedAnnotations.SearchStrategy searchStrategy, RepeatableContainers repeatableContainers) Create a newMergedAnnotationsinstance containing all annotations and meta-annotations from the specified element and, depending on theMergedAnnotations.SearchStrategy, related inherited elements.static MergedAnnotationsfrom(AnnotatedElement element, MergedAnnotations.SearchStrategy searchStrategy, RepeatableContainers repeatableContainers, AnnotationFilter annotationFilter) Create a newMergedAnnotationsinstance containing all annotations and meta-annotations from the specified element and, depending on theMergedAnnotations.SearchStrategy, related inherited elements.<A extends Annotation>
MergedAnnotation<A>Get the nearest matching annotation or meta-annotation of the specified type, orMergedAnnotation.missing()if none is present.<A extends Annotation>
MergedAnnotation<A>get(Class<A> annotationType, Predicate<? super MergedAnnotation<A>> predicate) Get the nearest matching annotation or meta-annotation of the specified type, orMergedAnnotation.missing()if none is present.<A extends Annotation>
MergedAnnotation<A>get(Class<A> annotationType, Predicate<? super MergedAnnotation<A>> predicate, MergedAnnotationSelector<A> selector) Get a matching annotation or meta-annotation of the specified type, orMergedAnnotation.missing()if none is present.<A extends Annotation>
MergedAnnotation<A>Get the nearest matching annotation or meta-annotation of the specified type, orMergedAnnotation.missing()if none is present.<A extends Annotation>
MergedAnnotation<A>get(String annotationType, Predicate<? super MergedAnnotation<A>> predicate) Get the nearest matching annotation or meta-annotation of the specified type, orMergedAnnotation.missing()if none is present.<A extends Annotation>
MergedAnnotation<A>get(String annotationType, Predicate<? super MergedAnnotation<A>> predicate, MergedAnnotationSelector<A> selector) Get a matching annotation or meta-annotation of the specified type, orMergedAnnotation.missing()if none is present.<A extends Annotation>
booleanisDirectlyPresent(Class<A> annotationType) Determine if the specified annotation type is directly present.booleanisDirectlyPresent(String annotationType) Determine if the specified annotation type is directly present.<A extends Annotation>
booleanDetermine if the specified annotation type is either directly present or meta-present.booleanDetermine if the specified annotation type is either directly present or meta-present.static MergedAnnotationsof(Collection<MergedAnnotation<?>> annotations) Create a newMergedAnnotationsinstance from the specified collection of directly present annotations.static MergedAnnotations.Searchsearch(MergedAnnotations.SearchStrategy searchStrategy) Find merged annotations using the suppliedMergedAnnotations.SearchStrategyand a fluent API for configuring and performing the search.stream()Stream all annotations and meta-annotations contained in this collection.<A extends Annotation>
Stream<MergedAnnotation<A>>Stream all annotations and meta-annotations that match the specified type.<A extends Annotation>
Stream<MergedAnnotation<A>>Stream all annotations and meta-annotations that match the specified type.Methods inherited from interface java.lang.Iterable
forEach, iterator, spliterator
-
Method Details
-
isPresent
Determine if the specified annotation type is either directly present or meta-present.Equivalent to calling
get(annotationType).isPresent().- Parameters:
annotationType- the annotation type to check- Returns:
trueif the annotation is present
-
isPresent
Determine if the specified annotation type is either directly present or meta-present.Equivalent to calling
get(annotationType).isPresent().- Parameters:
annotationType- the fully qualified class name of the annotation type to check- Returns:
trueif the annotation is present
-
isDirectlyPresent
Determine if the specified annotation type is directly present.Equivalent to calling
get(annotationType).isDirectlyPresent().- Parameters:
annotationType- the annotation type to check- Returns:
trueif the annotation is directly present
-
isDirectlyPresent
Determine if the specified annotation type is directly present.Equivalent to calling
get(annotationType).isDirectlyPresent().- Parameters:
annotationType- the fully qualified class name of the annotation type to check- Returns:
trueif the annotation is directly present
-
get
Get the nearest matching annotation or meta-annotation of the specified type, orMergedAnnotation.missing()if none is present.- Parameters:
annotationType- the annotation type to get- Returns:
- a
MergedAnnotationinstance
-
get
<A extends Annotation> MergedAnnotation<A> get(Class<A> annotationType, @Nullable Predicate<? super MergedAnnotation<A>> predicate) Get the nearest matching annotation or meta-annotation of the specified type, orMergedAnnotation.missing()if none is present.- Parameters:
annotationType- the annotation type to getpredicate- a predicate that must match, ornullif only type matching is required- Returns:
- a
MergedAnnotationinstance - See Also:
-
get
<A extends Annotation> MergedAnnotation<A> get(Class<A> annotationType, @Nullable Predicate<? super MergedAnnotation<A>> predicate, @Nullable MergedAnnotationSelector<A> selector) Get a matching annotation or meta-annotation of the specified type, orMergedAnnotation.missing()if none is present.- Parameters:
annotationType- the annotation type to getpredicate- a predicate that must match, ornullif only type matching is requiredselector- a selector used to choose the most appropriate annotation within an aggregate, ornullto select the nearest- Returns:
- a
MergedAnnotationinstance - See Also:
-
get
Get the nearest matching annotation or meta-annotation of the specified type, orMergedAnnotation.missing()if none is present.- Parameters:
annotationType- the fully qualified class name of the annotation type to get- Returns:
- a
MergedAnnotationinstance
-
get
<A extends Annotation> MergedAnnotation<A> get(String annotationType, @Nullable Predicate<? super MergedAnnotation<A>> predicate) Get the nearest matching annotation or meta-annotation of the specified type, orMergedAnnotation.missing()if none is present.- Parameters:
annotationType- the fully qualified class name of the annotation type to getpredicate- a predicate that must match, ornullif only type matching is required- Returns:
- a
MergedAnnotationinstance - See Also:
-
get
<A extends Annotation> MergedAnnotation<A> get(String annotationType, @Nullable Predicate<? super MergedAnnotation<A>> predicate, @Nullable MergedAnnotationSelector<A> selector) Get a matching annotation or meta-annotation of the specified type, orMergedAnnotation.missing()if none is present.- Parameters:
annotationType- the fully qualified class name of the annotation type to getpredicate- a predicate that must match, ornullif only type matching is requiredselector- a selector used to choose the most appropriate annotation within an aggregate, ornullto select the nearest- Returns:
- a
MergedAnnotationinstance - See Also:
-
stream
Stream all annotations and meta-annotations that match the specified type.The resulting stream follows the same ordering rules as
stream().- Parameters:
annotationType- the annotation type to match- Returns:
- a stream of matching annotations
-
stream
Stream all annotations and meta-annotations that match the specified type.The resulting stream follows the same ordering rules as
stream().- Parameters:
annotationType- the fully qualified class name of the annotation type to match- Returns:
- a stream of matching annotations
-
stream
Stream<MergedAnnotation<Annotation>> stream()Stream all annotations and meta-annotations contained in this collection.The resulting stream is ordered first by the aggregate index and then by the annotation distance (with the closest annotations first). This ordering means that, for most use-cases, the most suitable annotations appear earliest in the stream.
- Returns:
- a stream of annotations
-
from
Create a newMergedAnnotationsinstance containing all annotations and meta-annotations from the specified element.The resulting instance will not include any inherited annotations. If you want to include those as well you should use
from(AnnotatedElement, SearchStrategy)with an appropriateMergedAnnotations.SearchStrategy.- Parameters:
element- the source element- Returns:
- a
MergedAnnotationsinstance containing the element's annotations - See Also:
-
from
static MergedAnnotations from(AnnotatedElement element, MergedAnnotations.SearchStrategy searchStrategy) Create a newMergedAnnotationsinstance containing all annotations and meta-annotations from the specified element and, depending on theMergedAnnotations.SearchStrategy, related inherited elements.- Parameters:
element- the source elementsearchStrategy- the search strategy to use- Returns:
- a
MergedAnnotationsinstance containing the merged element annotations - See Also:
-
from
static MergedAnnotations from(AnnotatedElement element, MergedAnnotations.SearchStrategy searchStrategy, RepeatableContainers repeatableContainers) Create a newMergedAnnotationsinstance containing all annotations and meta-annotations from the specified element and, depending on theMergedAnnotations.SearchStrategy, related inherited elements.- Parameters:
element- the source elementsearchStrategy- the search strategy to userepeatableContainers- the repeatable containers that may be used by the element annotations or the meta-annotations- Returns:
- a
MergedAnnotationsinstance containing the merged element annotations - See Also:
-
from
static MergedAnnotations from(AnnotatedElement element, MergedAnnotations.SearchStrategy searchStrategy, RepeatableContainers repeatableContainers, AnnotationFilter annotationFilter) Create a newMergedAnnotationsinstance containing all annotations and meta-annotations from the specified element and, depending on theMergedAnnotations.SearchStrategy, related inherited elements.- Parameters:
element- the source elementsearchStrategy- the search strategy to userepeatableContainers- the repeatable containers that may be used by the element annotations or the meta-annotationsannotationFilter- an annotation filter used to restrict the annotations considered- Returns:
- a
MergedAnnotationsinstance containing the merged annotations for the supplied element - See Also:
-
from
Create a newMergedAnnotationsinstance from the specified annotations.- Parameters:
annotations- the annotations to include- Returns:
- a
MergedAnnotationsinstance containing the annotations - See Also:
-
from
Create a newMergedAnnotationsinstance from the specified annotations.- Parameters:
source- the source for the annotations. This source is used only for information and logging. It does not need to actually contain the specified annotations, and it will not be searched.annotations- the annotations to include- Returns:
- a
MergedAnnotationsinstance containing the annotations - See Also:
-
from
static MergedAnnotations from(Object source, Annotation[] annotations, RepeatableContainers repeatableContainers) Create a newMergedAnnotationsinstance from the specified annotations.- Parameters:
source- the source for the annotations. This source is used only for information and logging. It does not need to actually contain the specified annotations, and it will not be searched.annotations- the annotations to includerepeatableContainers- the repeatable containers that may be used by meta-annotations- Returns:
- a
MergedAnnotationsinstance containing the annotations
-
from
static MergedAnnotations from(Object source, Annotation[] annotations, RepeatableContainers repeatableContainers, AnnotationFilter annotationFilter) Create a newMergedAnnotationsinstance from the specified annotations.- Parameters:
source- the source for the annotations. This source is used only for information and logging. It does not need to actually contain the specified annotations, and it will not be searched.annotations- the annotations to includerepeatableContainers- the repeatable containers that may be used by meta-annotationsannotationFilter- an annotation filter used to restrict the annotations considered- Returns:
- a
MergedAnnotationsinstance containing the annotations
-
of
Create a newMergedAnnotationsinstance from the specified collection of directly present annotations. This method allows aMergedAnnotationsinstance to be created from annotations that are not necessarily loaded using reflection. The provided annotations must all bedirectly presentand must have anaggregate indexof0.The resulting
MergedAnnotationsinstance will contain both the specified annotations and any meta-annotations that can be read using reflection.- Parameters:
annotations- the annotations to include- Returns:
- a
MergedAnnotationsinstance containing the annotations - See Also:
-
search
Find merged annotations using the suppliedMergedAnnotations.SearchStrategyand a fluent API for configuring and performing the search.See
MergedAnnotations.Searchfor details.- Parameters:
searchStrategy- the search strategy to use- Returns:
- a
Searchinstance to perform the search - Since:
- 6.0
-