Top Description Inners Methods
org.springframework.core.annotation

public Interface MergedAnnotations

extends Iterable<MergedAnnotation<Annotation>>
Known Direct Implementers
org.springframework.core.annotation.MergedAnnotationsCollection, org.springframework.core.annotation.TypeMappedAnnotations
Imports
java.lang.annotation.Annotation, .Inherited, java.lang.reflect.AnnotatedElement, .Method, java.util.Collection, java.util.function.Predicate, java.util.stream.Stream, org.springframework.lang.Nullable, org.springframework.util.Assert

Provides access to a collection of merged annotations, usually obtained from a source such as a Class or Method.

Each merged annotation represents a view where the attribute values may be "merged" from different source values, typically:

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 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.

Authors
Phillip Webb, Sam Brannen
Since
5.2
See Also
MergedAnnotation, MergedAnnotationCollectors, MergedAnnotationPredicates, MergedAnnotationSelectors

Nested and Inner Type Summary

Modifier and TypeClass and Description
public static class
MergedAnnotations.Search

Fluent API for configuring the search algorithm used in the MergedAnnotations model and performing a search.

public static enum

Method Summary

Modifier and TypeMethod and Description
public static MergedAnnotations

Returns:

a MergedAnnotations instance containing the element's annotations
from
(AnnotatedElement
the source element
element
)

Create a new MergedAnnotations instance containing all annotations and meta-annotations from the specified element.

public static MergedAnnotations

Returns:

a MergedAnnotations instance containing the merged element annotations
from
(AnnotatedElement
the source element
element
,
MergedAnnotations.SearchStrategy
the search strategy to use
searchStrategy
)

Create a new MergedAnnotations instance containing all annotations and meta-annotations from the specified element and, depending on the SearchStrategy, related inherited elements.

public static MergedAnnotations

Returns:

a MergedAnnotations instance containing the merged element annotations
from
(AnnotatedElement
the source element
element
,
MergedAnnotations.SearchStrategy
the search strategy to use
searchStrategy
,
RepeatableContainers
the repeatable containers that may be used by the element annotations or the meta-annotations
repeatableContainers
)

Create a new MergedAnnotations instance containing all annotations and meta-annotations from the specified element and, depending on the SearchStrategy, related inherited elements.

public static MergedAnnotations

Returns:

a MergedAnnotations instance containing the merged annotations for the supplied element
from
(AnnotatedElement
the source element
element
,
MergedAnnotations.SearchStrategy
the search strategy to use
searchStrategy
,
RepeatableContainers
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 MergedAnnotations instance containing all annotations and meta-annotations from the specified element and, depending on the SearchStrategy, related inherited elements.

private static MergedAnnotations
from(AnnotatedElement element, MergedAnnotations.SearchStrategy searchStrategy, Predicate<Class<?>> searchEnclosingClass, RepeatableContainers repeatableContainers, AnnotationFilter annotationFilter)

public static MergedAnnotations

Returns:

a MergedAnnotations instance containing the annotations
from
(Annotation...
the annotations to include
annotations
)

Create a new MergedAnnotations instance from the specified annotations.

public static MergedAnnotations

Returns:

a MergedAnnotations instance containing the annotations
from
(Object
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.
source
,
Annotation...
the annotations to include
annotations
)

Create a new MergedAnnotations instance from the specified annotations.

public static MergedAnnotations

Returns:

a MergedAnnotations instance containing the annotations
from
(Object
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.
source
,
Annotation[]
the annotations to include
annotations
,
RepeatableContainers
the repeatable containers that may be used by meta-annotations
repeatableContainers
)

Create a new MergedAnnotations instance from the specified annotations.

public static MergedAnnotations

Returns:

a MergedAnnotations instance containing the annotations
from
(Object
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.
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 MergedAnnotations instance from the specified annotations.

public <A extends Annotation> MergedAnnotation<A>

Returns:

a MergedAnnotation instance
get
(Class<A>
the annotation type to get
annotationType
)

Get the nearest matching annotation or meta-annotation of the specified type, or MergedAnnotation#missing() if none is present.

public <A extends Annotation> MergedAnnotation<A>

Returns:

a MergedAnnotation instance
get
(Class<A>
the annotation type to get
annotationType
,
Predicate<? super MergedAnnotation<A>>
a predicate that must match, or null if only type matching is required
predicate
)

Get the nearest matching annotation or meta-annotation of the specified type, or MergedAnnotation#missing() if none is present.

public <A extends Annotation> MergedAnnotation<A>

Returns:

a MergedAnnotation instance
get
(Class<A>
the annotation type to get
annotationType
,
Predicate<? super MergedAnnotation<A>>
a predicate that must match, or null if only type matching is required
predicate
,
MergedAnnotationSelector<A>
a selector used to choose the most appropriate annotation within an aggregate, or null to select the nearest
selector
)

Get a matching annotation or meta-annotation of the specified type, or MergedAnnotation#missing() if none is present.

public <A extends Annotation> MergedAnnotation<A>

Returns:

a MergedAnnotation instance
get
(String
the fully qualified class name of the annotation type to get
annotationType
)

Get the nearest matching annotation or meta-annotation of the specified type, or MergedAnnotation#missing() if none is present.

public <A extends Annotation> MergedAnnotation<A>

Returns:

a MergedAnnotation instance
get
(String
the fully qualified class name of the annotation type to get
annotationType
,
Predicate<? super MergedAnnotation<A>>
a predicate that must match, or null if only type matching is required
predicate
)

Get the nearest matching annotation or meta-annotation of the specified type, or MergedAnnotation#missing() if none is present.

public <A extends Annotation> MergedAnnotation<A>

Returns:

a MergedAnnotation instance
get
(String
the fully qualified class name of the annotation type to get
annotationType
,
Predicate<? super MergedAnnotation<A>>
a predicate that must match, or null if only type matching is required
predicate
,
MergedAnnotationSelector<A>
a selector used to choose the most appropriate annotation within an aggregate, or null to select the nearest
selector
)

Get a matching annotation or meta-annotation of the specified type, or MergedAnnotation#missing() if none is present.

public <A extends Annotation> boolean

Returns:

true if the annotation is directly present
isDirectlyPresent
(Class<A>
the annotation type to check
annotationType
)

Determine if the specified annotation type is directly present.

public boolean

Returns:

true if the annotation is directly present
isDirectlyPresent
(String
the 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

Returns:

true if the annotation is present
isPresent
(Class<A>
the annotation type to check
annotationType
)

Determine if the specified annotation type is either directly present or meta-present.

public boolean

Returns:

true if the annotation is present
isPresent
(String
the fully qualified class name of the annotation type to check
annotationType
)

Determine if the specified annotation type is either directly present or meta-present.

public static MergedAnnotations

Returns:

a MergedAnnotations instance containing the annotations
of
(Collection<MergedAnnotation<?>>
the annotations to include
annotations
)

Create a new MergedAnnotations instance from the specified collection of directly present annotations.

public static MergedAnnotations.Search

Returns:

a Search instance to perform the search
search
(MergedAnnotations.SearchStrategy
the search strategy to use
searchStrategy
)

Find merged annotations using the supplied SearchStrategy and a fluent API for configuring and performing the search.

public <A extends Annotation> Stream<MergedAnnotation<A>>

Returns:

a stream of matching annotations
stream
(Class<A>
the annotation type to match
annotationType
)

Stream all annotations and meta-annotations that match the specified type.

public <A extends Annotation> Stream<MergedAnnotation<A>>

Returns:

a stream of matching annotations
stream
(String
the fully qualified class name of the annotation type to match
annotationType
)

Stream all annotations and meta-annotations that match the specified type.

public Stream<MergedAnnotation<Annotation>>

Returns:

a stream of annotations
stream
()

Stream all annotations and meta-annotations contained in this collection.

Inherited from java.lang.Iterable:
forEachiteratorspliterator