接口 MergedAnnotations
- 所有超级接口:
Iterable<MergedAnnotation<Annotation>>
public interface MergedAnnotations extends Iterable<MergedAnnotation<Annotation>>
Provides access to a collection of merged annotations, usually obtained from a source such as aClass
orMethod
.Each merged annotation represents a view where the attribute values may be "merged" from different source values, typically:
- Explicit and Implicit
@AliasFor
declarations on one or more attributes within the annotation - Explicit
@AliasFor
declarations 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 JavaAnnotatedElement
. 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,
MergedAnnotations.SearchStrategy.TYPE_HIERARCHY
will search both superclasses and implemented interfaces.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'sAnnotationUtils
for simple annotation retrieval purposes.- 从以下版本开始:
- 5.2
- 作者:
- Phillip Webb, Sam Brannen
- 另请参阅:
MergedAnnotation
,MergedAnnotationCollectors
,MergedAnnotationPredicates
,MergedAnnotationSelectors
嵌套类概要
嵌套类 修饰符和类型 接口 说明 static class
MergedAnnotations.SearchStrategy
Search strategies supported byfrom(AnnotatedElement, SearchStrategy)
.
方法概要
所有方法 静态方法 实例方法 抽象方法 修饰符和类型 方法 说明 static MergedAnnotations
from(Annotation... annotations)
Create a newMergedAnnotations
instance from the specified annotations.static MergedAnnotations
from(Object source, Annotation... annotations)
Create a newMergedAnnotations
instance from the specified annotations.static MergedAnnotations
from(Object source, Annotation[] annotations, RepeatableContainers repeatableContainers)
Create a newMergedAnnotations
instance from the specified annotations.static MergedAnnotations
from(Object source, Annotation[] annotations, RepeatableContainers repeatableContainers, AnnotationFilter annotationFilter)
Create a newMergedAnnotations
instance from the specified annotations.static MergedAnnotations
from(AnnotatedElement element)
Create a newMergedAnnotations
instance containing all annotations and meta-annotations from the specified element.static MergedAnnotations
from(AnnotatedElement element, MergedAnnotations.SearchStrategy searchStrategy)
Create a newMergedAnnotations
instance containing all annotations and meta-annotations from the specified element and, depending on theMergedAnnotations.SearchStrategy
, related inherited elements.static MergedAnnotations
from(AnnotatedElement element, MergedAnnotations.SearchStrategy searchStrategy, RepeatableContainers repeatableContainers)
Create a newMergedAnnotations
instance containing all annotations and meta-annotations from the specified element and, depending on theMergedAnnotations.SearchStrategy
, related inherited elements.static MergedAnnotations
from(AnnotatedElement element, MergedAnnotations.SearchStrategy searchStrategy, RepeatableContainers repeatableContainers, AnnotationFilter annotationFilter)
Create a newMergedAnnotations
instance containing all annotations and meta-annotations from the specified element and, depending on theMergedAnnotations.SearchStrategy
, related inherited elements.<A extends Annotation>
MergedAnnotation<A>get(Class<A> annotationType)
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(String annotationType)
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 is directly present.boolean
isDirectlyPresent(String annotationType)
Determine if the specified annotation is directly present.<A extends Annotation>
booleanisPresent(Class<A> annotationType)
Determine if the specified annotation is either directly present or meta-present.boolean
isPresent(String annotationType)
Determine if the specified annotation is either directly present or meta-present.static MergedAnnotations
of(Collection<MergedAnnotation<?>> annotations)
Create a newMergedAnnotations
instance from the specified collection of directly present annotations.Stream<MergedAnnotation<Annotation>>
stream()
Stream all annotations and meta-annotations contained in this collection.<A extends Annotation>
Stream<MergedAnnotation<A>>stream(Class<A> annotationType)
Stream all annotations and meta-annotations that match the specified type.<A extends Annotation>
Stream<MergedAnnotation<A>>stream(String annotationType)
Stream all annotations and meta-annotations that match the specified type.从接口继承的方法 java.lang.Iterable
forEach, iterator, spliterator
方法详细资料
isPresent
<A extends Annotation> boolean isPresent(Class<A> annotationType)
Determine if the specified annotation is either directly present or meta-present.Equivalent to calling
get(annotationType).isPresent()
.- 参数:
annotationType
- the annotation type to check- 返回:
true
if the annotation is present
isPresent
boolean isPresent(String annotationType)
Determine if the specified annotation is either directly present or meta-present.Equivalent to calling
get(annotationType).isPresent()
.- 参数:
annotationType
- the fully qualified class name of the annotation type to check- 返回:
true
if the annotation is present
isDirectlyPresent
<A extends Annotation> boolean isDirectlyPresent(Class<A> annotationType)
Determine if the specified annotation is directly present.Equivalent to calling
get(annotationType).isDirectlyPresent()
.- 参数:
annotationType
- the annotation type to check- 返回:
true
if the annotation is directly present
isDirectlyPresent
boolean isDirectlyPresent(String annotationType)
Determine if the specified annotation is directly present.Equivalent to calling
get(annotationType).isDirectlyPresent()
.- 参数:
annotationType
- the fully qualified class name of the annotation type to check- 返回:
true
if the annotation is directly present
get
<A extends Annotation> MergedAnnotation<A> get(Class<A> annotationType)
Get the nearest matching annotation or meta-annotation of the specified type, orMergedAnnotation.missing()
if none is present.- 参数:
annotationType
- the annotation type to get- 返回:
- a
MergedAnnotation
instance
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.- 参数:
annotationType
- the annotation type to getpredicate
- a predicate that must match, ornull
if only type matching is required- 返回:
- a
MergedAnnotation
instance - 另请参阅:
MergedAnnotationPredicates
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.- 参数:
annotationType
- the annotation type to getpredicate
- a predicate that must match, ornull
if only type matching is requiredselector
- a selector used to choose the most appropriate annotation within an aggregate, ornull
to select the nearest- 返回:
- a
MergedAnnotation
instance - 另请参阅:
MergedAnnotationPredicates
,MergedAnnotationSelectors
get
<A extends Annotation> MergedAnnotation<A> get(String annotationType)
Get the nearest matching annotation or meta-annotation of the specified type, orMergedAnnotation.missing()
if none is present.- 参数:
annotationType
- the fully qualified class name of the annotation type to get- 返回:
- a
MergedAnnotation
instance
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.- 参数:
annotationType
- the fully qualified class name of the annotation type to getpredicate
- a predicate that must match, ornull
if only type matching is required- 返回:
- a
MergedAnnotation
instance - 另请参阅:
MergedAnnotationPredicates
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.- 参数:
annotationType
- the fully qualified class name of the annotation type to getpredicate
- a predicate that must match, ornull
if only type matching is requiredselector
- a selector used to choose the most appropriate annotation within an aggregate, ornull
to select the nearest- 返回:
- a
MergedAnnotation
instance - 另请参阅:
MergedAnnotationPredicates
,MergedAnnotationSelectors
stream
<A extends Annotation> Stream<MergedAnnotation<A>> stream(Class<A> annotationType)
Stream all annotations and meta-annotations that match the specified type. The resulting stream follows the same ordering rules asstream()
.- 参数:
annotationType
- the annotation type to match- 返回:
- a stream of matching annotations
stream
<A extends Annotation> Stream<MergedAnnotation<A>> stream(String annotationType)
Stream all annotations and meta-annotations that match the specified type. The resulting stream follows the same ordering rules asstream()
.- 参数:
annotationType
- the fully qualified class name of the annotation type to match- 返回:
- 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.- 返回:
- a stream of annotations
from
static MergedAnnotations from(AnnotatedElement element)
Create a newMergedAnnotations
instance 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 usefrom(AnnotatedElement, SearchStrategy)
with an appropriateMergedAnnotations.SearchStrategy
.- 参数:
element
- the source element- 返回:
- a
MergedAnnotations
instance containing the element's annotations
from
static MergedAnnotations from(AnnotatedElement element, MergedAnnotations.SearchStrategy searchStrategy)
Create a newMergedAnnotations
instance containing all annotations and meta-annotations from the specified element and, depending on theMergedAnnotations.SearchStrategy
, related inherited elements.- 参数:
element
- the source elementsearchStrategy
- the search strategy to use- 返回:
- a
MergedAnnotations
instance containing the merged element annotations
from
static MergedAnnotations from(AnnotatedElement element, MergedAnnotations.SearchStrategy searchStrategy, RepeatableContainers repeatableContainers)
Create a newMergedAnnotations
instance containing all annotations and meta-annotations from the specified element and, depending on theMergedAnnotations.SearchStrategy
, related inherited elements.- 参数:
element
- the source elementsearchStrategy
- the search strategy to userepeatableContainers
- the repeatable containers that may be used by the element annotations or the meta-annotations- 返回:
- a
MergedAnnotations
instance containing the merged element annotations
from
static MergedAnnotations from(AnnotatedElement element, MergedAnnotations.SearchStrategy searchStrategy, RepeatableContainers repeatableContainers, AnnotationFilter annotationFilter)
Create a newMergedAnnotations
instance containing all annotations and meta-annotations from the specified element and, depending on theMergedAnnotations.SearchStrategy
, related inherited elements.- 参数:
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- 返回:
- a
MergedAnnotations
instance containing the merged annotations for the supplied element
from
static MergedAnnotations from(Annotation... annotations)
Create a newMergedAnnotations
instance from the specified annotations.- 参数:
annotations
- the annotations to include- 返回:
- a
MergedAnnotations
instance containing the annotations - 另请参阅:
from(Object, Annotation...)
from
static MergedAnnotations from(Object source, Annotation... annotations)
Create a newMergedAnnotations
instance from the specified annotations.- 参数:
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- 返回:
- a
MergedAnnotations
instance containing the annotations - 另请参阅:
from(Annotation...)
,from(AnnotatedElement)
from
static MergedAnnotations from(Object source, Annotation[] annotations, RepeatableContainers repeatableContainers)
Create a newMergedAnnotations
instance from the specified annotations.- 参数:
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- 返回:
- a
MergedAnnotations
instance containing the annotations
from
static MergedAnnotations from(Object source, Annotation[] annotations, RepeatableContainers repeatableContainers, AnnotationFilter annotationFilter)
Create a newMergedAnnotations
instance from the specified annotations.- 参数:
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- 返回:
- a
MergedAnnotations
instance containing the annotations
of
static MergedAnnotations of(Collection<MergedAnnotation<?>> annotations)
Create a newMergedAnnotations
instance from the specified collection of directly present annotations. This method allows aMergedAnnotations
instance to be created from annotations that are not necessarily loaded using reflection. The provided annotations must all bedirectly present
and must have anaggregate index
of0
.The resulting
MergedAnnotations
instance will contain both the specified annotations, and any meta-annotations that can be read using reflection.- 参数:
annotations
- the annotations to include- 返回:
- a
MergedAnnotations
instance containing the annotations - 另请参阅:
MergedAnnotation.of(ClassLoader, Object, Class, java.util.Map)