Top Description Inners Constructors Methods
org.springframework.core.annotation

public abstract Class AnnotatedElementUtils

extends Object
Class Inheritance
Imports
java.lang.annotation.Annotation, java.lang.reflect.AnnotatedElement, java.util.Collections, .Comparator, .LinkedHashSet, .Set, java.util.stream.Collectors, org.springframework.core.BridgeMethodResolver, org.springframework.core.annotation.MergedAnnotation.Adapt, .MergedAnnotations.SearchStrategy, org.springframework.lang.Nullable, org.springframework.util.MultiValueMap

General utility methods for finding annotations, meta-annotations, and repeatable annotations on AnnotatedElements.

AnnotatedElementUtils defines the public API for Spring's meta-annotation programming model with support for annotation attribute overrides. If you do not need support for annotation attribute overrides, consider using AnnotationUtils instead.

Note that the features of this class are not provided by the JDK's introspection facilities themselves.

Annotation Attribute Overrides

Support for meta-annotations with attribute overrides in composed annotations is provided by all variants of the getMergedAnnotationAttributes(), getMergedAnnotation(), getAllMergedAnnotations(), getMergedRepeatableAnnotations(), findMergedAnnotationAttributes(), findMergedAnnotation(), findAllMergedAnnotations(), and findMergedRepeatableAnnotations() methods.

Find vs. Get Semantics

The search algorithms used by methods in this class follow either find or get semantics. Consult the javadocs for each individual method for details on which search algorithm is used.

Get semantics are limited to searching for annotations that are either present on an AnnotatedElement (i.e. declared locally or inherited) or declared within the annotation hierarchy above the AnnotatedElement.

Find semantics are much more exhaustive, providing get semantics plus support for the following:

Support for @Inherited

Methods following get semantics will honor the contract of Java's @Inherited annotation except that locally declared annotations (including custom composed annotations) will be favored over inherited annotations. In contrast, methods following find semantics will completely ignore the presence of @Inherited since the find search algorithm manually traverses type and method hierarchies and thereby implicitly supports annotation inheritance without a need for @Inherited.

Authors
Phillip Webb, Juergen Hoeller, Sam Brannen
Since
4.0
See Also
AliasFor, AnnotationAttributes, AnnotationUtils, BridgeMethodResolver

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public static <A extends Annotation> Set<A>

Returns:

the set of all merged, synthesized Annotations found, or an empty set if none were found
findAllMergedAnnotations
(AnnotatedElement
the annotated element (never null)
element
,
Class<A>
the annotation type to find (never null)
annotationType
)

Find all annotations of the specified annotationType within the annotation hierarchy above the supplied element; 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.

public static Set<Annotation>

Returns:

the set of all merged, synthesized Annotations found, or an empty set if none were found
findAllMergedAnnotations
(AnnotatedElement
the annotated element (never null)
element
,
Set<Class<? extends Annotation>>
the annotation types to find
annotationTypes
)

Find all annotations of the specified annotationTypes within the annotation hierarchy above the supplied element; 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 corresponding annotationType.

private static MergedAnnotations
public static <A extends Annotation> A

Returns:

the merged, synthesized Annotation, or null if not found
findMergedAnnotation
(AnnotatedElement
the annotated element
element
,
Class<A>
the annotation type to find
annotationType
)

Find the first annotation of the specified annotationType within the annotation hierarchy above the supplied element, 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.

public static AnnotationAttributes

Returns:

the merged AnnotationAttributes, or null if not found
findMergedAnnotationAttributes
(AnnotatedElement
the annotated element
element
,
Class<? extends Annotation>
the annotation type to find
annotationType
,
boolean
whether to convert Class references into Strings or to preserve them as Class references
classValuesAsString
,
boolean
whether to convert nested Annotation instances into AnnotationAttributes maps or to preserve them as Annotation instances
nestedAnnotationsAsMap
)

Find the first annotation of the specified annotationType within the annotation hierarchy above the supplied element and merge that annotation's attributes with matching attributes from annotations in lower levels of the annotation hierarchy.

public static AnnotationAttributes

Returns:

the merged AnnotationAttributes, or null if not found
findMergedAnnotationAttributes
(AnnotatedElement
the annotated element
element
,
String
the fully qualified class name of the annotation type to find
annotationName
,
boolean
whether to convert Class references into Strings or to preserve them as Class references
classValuesAsString
,
boolean
whether to convert nested Annotation instances into AnnotationAttributes maps or to preserve them as Annotation instances
nestedAnnotationsAsMap
)

Find the first annotation of the specified annotationName within the annotation hierarchy above the supplied element and merge that annotation's attributes with matching attributes from annotations in lower levels of the annotation hierarchy.

public static <A extends Annotation> Set<A>

Returns:

the set of all merged repeatable Annotations found, or an empty set if none were found
findMergedRepeatableAnnotations
(AnnotatedElement
the annotated element (never null)
element
,
Class<A>
the annotation type to find (never null)
annotationType
)

Find all repeatable annotations of the specified annotationType within the annotation hierarchy above the supplied element; 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.

public static <A extends Annotation> Set<A>

Returns:

the set of all merged repeatable Annotations found, or an empty set if none were found
findMergedRepeatableAnnotations
(AnnotatedElement
the annotated element (never null)
element
,
Class<A>
the annotation type to find (never null)
annotationType
,
Class<? extends Annotation>
the type of the container that holds the annotations; may be null if the container type should be looked up via java.lang.annotation.Repeatable
containerType
)

Find all repeatable annotations of the specified annotationType within the annotation hierarchy above the supplied element; 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.

private static MergedAnnotations
findRepeatableAnnotations(AnnotatedElement element, Class<? extends Annotation> containerType, Class<? extends Annotation> annotationType)

public static AnnotatedElement
forAnnotations(Annotation...
the annotations to expose through the AnnotatedElement
annotations
)

Build an adapted AnnotatedElement for the given annotations, typically for use with other methods on AnnotatedElementUtils.

public static MultiValueMap<String, Object>

Returns:

a MultiValueMap keyed by attribute name, containing the annotation attributes from all annotations found, or null if not found
getAllAnnotationAttributes
(AnnotatedElement
the annotated element
element
,
String
the fully qualified class name of the annotation type to find
annotationName
)

Get the annotation attributes of all annotations of the specified annotationName in the annotation hierarchy above the supplied AnnotatedElement and store the results in a MultiValueMap.

public static MultiValueMap<String, Object>

Returns:

a MultiValueMap keyed by attribute name, containing the annotation attributes from all annotations found, or null if not found
getAllAnnotationAttributes
(AnnotatedElement
the annotated element
element
,
String
the fully qualified class name of the annotation type to find
annotationName
,
final boolean
whether to convert Class references into Strings or to preserve them as Class references
classValuesAsString
,
final boolean
whether to convert nested Annotation instances into AnnotationAttributes maps or to preserve them as Annotation instances
nestedAnnotationsAsMap
)

Get the annotation attributes of all annotations of the specified annotationName in the annotation hierarchy above the supplied AnnotatedElement and store the results in a MultiValueMap.

public static <A extends Annotation> Set<A>

Returns:

the set of all merged, synthesized Annotations found, or an empty set if none were found
getAllMergedAnnotations
(AnnotatedElement
the annotated element (never null)
element
,
Class<A>
the annotation type to find (never null)
annotationType
)

Get all annotations of the specified annotationType within the annotation hierarchy above the supplied element; 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.

public static Set<Annotation>

Returns:

the set of all merged, synthesized Annotations found, or an empty set if none were found
getAllMergedAnnotations
(AnnotatedElement
the annotated element (never null)
element
,
Set<Class<? extends Annotation>>
the annotation types to find
annotationTypes
)

Get all annotations of the specified annotationTypes within the annotation hierarchy above the supplied element; 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 corresponding annotationType.

private static AnnotationAttributes
getAnnotationAttributes(MergedAnnotation<?> annotation, boolean classValuesAsString, boolean nestedAnnotationsAsMap)

private static MergedAnnotations
public static <A extends Annotation> A

Returns:

the merged, synthesized Annotation, or null if not found
getMergedAnnotation
(AnnotatedElement
the annotated element
element
,
Class<A>
the annotation type to find
annotationType
)

Get the first annotation of the specified annotationType within the annotation hierarchy above the supplied element, 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.

public static AnnotationAttributes

Returns:

the merged AnnotationAttributes, or null if not found
getMergedAnnotationAttributes
(AnnotatedElement
the annotated element
element
,
Class<? extends Annotation>
the annotation type to find
annotationType
)

Get the first annotation of the specified annotationType within the annotation hierarchy above the supplied element and merge that annotation's attributes with matching attributes from annotations in lower levels of the annotation hierarchy.

public static AnnotationAttributes

Returns:

the merged AnnotationAttributes, or null if not found
getMergedAnnotationAttributes
(AnnotatedElement
the annotated element
element
,
String
the fully qualified class name of the annotation type to find
annotationName
)

Get the first annotation of the specified annotationName within the annotation hierarchy above the supplied element and merge that annotation's attributes with matching attributes from annotations in lower levels of the annotation hierarchy.

public static AnnotationAttributes

Returns:

the merged AnnotationAttributes, or null if not found
getMergedAnnotationAttributes
(AnnotatedElement
the annotated element
element
,
String
the fully qualified class name of the annotation type to find
annotationName
,
boolean
whether to convert Class references into Strings or to preserve them as Class references
classValuesAsString
,
boolean
whether to convert nested Annotation instances into AnnotationAttributes maps or to preserve them as Annotation instances
nestedAnnotationsAsMap
)

Get the first annotation of the specified annotationName within the annotation hierarchy above the supplied element and merge that annotation's attributes with matching attributes from annotations in lower levels of the annotation hierarchy.

public static <A extends Annotation> Set<A>

Returns:

the set of all merged repeatable Annotations found, or an empty set if none were found
getMergedRepeatableAnnotations
(AnnotatedElement
the annotated element (never null)
element
,
Class<A>
the annotation type to find (never null)
annotationType
)

Get all repeatable annotations of the specified annotationType within the annotation hierarchy above the supplied element; 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.

public static <A extends Annotation> Set<A>

Returns:

the set of all merged repeatable Annotations found, or an empty set if none were found
getMergedRepeatableAnnotations
(AnnotatedElement
the annotated element (never null)
element
,
Class<A>
the annotation type to find (never null)
annotationType
,
Class<? extends Annotation>
the type of the container that holds the annotations; may be null if the container type should be looked up via java.lang.annotation.Repeatable
containerType
)

Get all repeatable annotations of the specified annotationType within the annotation hierarchy above the supplied element; 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.

public static Set<String>

Returns:

the names of all meta-annotations present on the annotation, or an empty set if not found
getMetaAnnotationTypes
(AnnotatedElement
the annotated element
element
,
Class<? extends Annotation>
the annotation type on which to find meta-annotations
annotationType
)

Get the fully qualified class names of all meta-annotation types present on the annotation (of the specified annotationType) on the supplied AnnotatedElement.

public static Set<String>

Returns:

the names of all meta-annotations present on the annotation, or an empty set if none found
getMetaAnnotationTypes
(AnnotatedElement
the annotated element
element
,
String
the fully qualified class name of the annotation type on which to find meta-annotations
annotationName
)

Get the fully qualified class names of all meta-annotation types present on the annotation (of the specified annotationName) on the supplied AnnotatedElement.

private static Set<String>
private static MergedAnnotations
getRepeatableAnnotations(AnnotatedElement element, Class<? extends Annotation> containerType, Class<? extends Annotation> annotationType)

public static boolean

Returns:

true if a matching annotation is present
hasAnnotation
(AnnotatedElement
the annotated element
element
,
Class<? extends Annotation>
the annotation type to find
annotationType
)

Determine if an annotation of the specified annotationType is available on the supplied AnnotatedElement or within the annotation hierarchy above the specified element.

public static boolean

Returns:

true if a matching meta-annotation is present
hasMetaAnnotationTypes
(AnnotatedElement
the annotated element
element
,
Class<? extends Annotation>
the meta-annotation type to find
annotationType
)

Determine if the supplied AnnotatedElement is annotated with a composed annotation that is meta-annotated with an annotation of the specified annotationType.

public static boolean

Returns:

true if a matching meta-annotation is present
hasMetaAnnotationTypes
(AnnotatedElement
the annotated element
element
,
String
the fully qualified class name of the meta-annotation type to find
annotationName
)

Determine if the supplied AnnotatedElement is annotated with a composed annotation that is meta-annotated with an annotation of the specified annotationName.

private static <A extends Annotation> Comparator<MergedAnnotation<A>>
public static boolean

Returns:

true if a matching annotation is present
isAnnotated
(AnnotatedElement
the annotated element
element
,
Class<? extends Annotation>
the annotation type to find
annotationType
)

Determine if an annotation of the specified annotationType is present on the supplied AnnotatedElement or within the annotation hierarchy above the specified element.

public static boolean

Returns:

true if a matching annotation is present
isAnnotated
(AnnotatedElement
the annotated element
element
,
String
the fully qualified class name of the annotation type to find
annotationName
)

Determine if an annotation of the specified annotationName is present on the supplied AnnotatedElement or within the annotation hierarchy above the specified element.

private static MultiValueMap<String, Object>
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait