Class or Method.
Each merged annotation represents a view where the attribute values may be "merged" from different source values, typically:
@AliasFor declarations on one or
more attributes within the annotation@AliasFor declarations for a meta-annotationFor 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 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.
MergedAnnotation, MergedAnnotationCollectors, MergedAnnotationPredicates, MergedAnnotationSelectors
| Modifier and Type | Class and Description |
|---|---|
| public static class | MergedAnnotations.Search
Fluent API for configuring the search algorithm used in the
|
| public static enum | MergedAnnotations.SearchStrategy
Search strategies supported by |
| Modifier and Type | Method and Description |
|---|---|
| public static MergedAnnotations | Returns: aMergedAnnotations instance containing the element's
annotationsthe source element element)Create a new |
| public static MergedAnnotations | Returns: aMergedAnnotations instance containing the merged
element annotationsthe source element element, MergedAnnotations.the search strategy to use searchStrategyCreate a new |
| public static MergedAnnotations | Returns: aMergedAnnotations instance containing the merged
element annotationsthe source element element, MergedAnnotations.the search strategy to use searchStrategy,the repeatable containers that may be used by
the element annotations or the meta-annotations repeatableContainers)Create a new |
| public static MergedAnnotations | Returns: aMergedAnnotations instance containing the merged
annotations for the supplied elementthe source element element, MergedAnnotations.the search strategy to use searchStrategy,the repeatable containers that may be used by
the element annotations or the meta-annotations repeatableContainers, AnnotationFilter an annotation filter used to restrict the
annotations considered annotationFilter)Create a new |
| private static MergedAnnotations | from(AnnotatedElement element, MergedAnnotations.
|
| public static MergedAnnotations | Returns: aMergedAnnotations instance containing the annotationsthe annotations to include annotations)Create a new |
| public static MergedAnnotations | Returns: aMergedAnnotations instance containing the annotationsthe 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. source, Annotation... the annotations to include annotations)Create a new |
| public static MergedAnnotations | Returns: aMergedAnnotations instance containing the annotationsthe 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. source, Annotation[] the annotations to include annotations, RepeatableContainers the repeatable containers that may be used by
meta-annotations repeatableContainers)Create a new |
| public static MergedAnnotations | Returns: aMergedAnnotations instance containing the annotationsthe 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. source, Annotation[] the annotations to include annotations, RepeatableContainers the repeatable containers that may be used by
meta-annotations repeatableContainers, AnnotationFilter an annotation filter used to restrict the
annotations considered annotationFilter)Create a new |
| public <A extends Annotation> MergedAnnotation | Returns: aMergedAnnotation instancethe annotation type to get annotationType)Get the nearest matching
annotation or meta-annotation of the specified type, or
|
| public <A extends Annotation> MergedAnnotation | Returns: aMergedAnnotation instancethe annotation type to get annotationType, Predicate<? super MergedAnnotation<A>> a predicate that must match, or predicate)null if only
type matching is requiredGet the nearest matching
annotation or meta-annotation of the specified type, or
|
| public <A extends Annotation> MergedAnnotation | Returns: aMergedAnnotation instancethe annotation type to get annotationType, Predicate<? super MergedAnnotation<A>> a predicate that must match, or predicate, MergedAnnotationSelector<A> null if only
type matching is requireda selector used to choose the most appropriate annotation
within an aggregate, or selector)null to select the
nearestGet a matching annotation or meta-annotation of the specified type, or
|
| public <A extends Annotation> MergedAnnotation | Returns: aMergedAnnotation instancethe fully qualified class name of the annotation type
to get annotationType)Get the nearest matching
annotation or meta-annotation of the specified type, or
|
| public <A extends Annotation> MergedAnnotation | Returns: aMergedAnnotation instancethe fully qualified class name of the annotation type
to get annotationType, Predicate<? super MergedAnnotation<A>> a predicate that must match, or predicate)null if only
type matching is requiredGet the nearest matching
annotation or meta-annotation of the specified type, or
|
| public <A extends Annotation> MergedAnnotation | Returns: aMergedAnnotation instancethe fully qualified class name of the annotation type
to get annotationType, Predicate<? super MergedAnnotation<A>> a predicate that must match, or predicate, MergedAnnotationSelector<A> null if only
type matching is requireda selector used to choose the most appropriate annotation
within an aggregate, or selector)null to select the
nearestGet a matching annotation or meta-annotation of the specified type, or
|
| public <A extends Annotation> boolean | Returns: true if the annotation is directly presentthe annotation type to check annotationType)Determine if the specified annotation type is directly present. |
| public boolean | Returns: true if the annotation is directly presentthe fully qualified class name of the annotation type
to check annotationType)Determine if the specified annotation type is directly present. |
| public <A extends Annotation> boolean | |
| public boolean | |
| public static MergedAnnotations | Returns: aMergedAnnotations instance containing the annotationsthe annotations to include annotations)Create a new |
| public static MergedAnnotations. | Returns: aSearch instance to perform the searchthe search strategy to use searchStrategyFind merged annotations using the supplied |
| public <A extends Annotation> Stream | |
| public <A extends Annotation> Stream | |
| public Stream | Returns: a stream of annotationsStream all annotations and meta-annotations contained in this collection. |