public class AnnotationAttributes
extends java.util.LinkedHashMap<java.lang.String,java.lang.Object>
LinkedHashMap
subclass representing annotation attribute
key-value pairs as read by AnnotationUtils
,
AnnotatedElementUtils
, and Spring's reflection- and ASM-based
AnnotationMetadata
implementations.
Provides 'pseudo-reification' to avoid noisy Map generics in the calling
code as well as convenience methods for looking up annotation attributes
in a type-safe fashion, including support for attribute aliases configured
via @AliasFor
.
AnnotationUtils.getAnnotationAttributes(java.lang.annotation.Annotation)
,
AnnotatedElementUtils
,
AliasFor
,
Serialized FormModifier and Type | Field and Description |
---|---|
private java.lang.Class<? extends java.lang.annotation.Annotation> |
annotationType |
private java.lang.String |
displayName |
Constructor and Description |
---|
AnnotationAttributes()
Create a new, empty
AnnotationAttributes instance. |
AnnotationAttributes(java.lang.Class<? extends java.lang.annotation.Annotation> annotationType)
Create a new, empty
AnnotationAttributes instance for the
specified annotationType . |
AnnotationAttributes(int initialCapacity)
Create a new, empty
AnnotationAttributes instance with the
given initial capacity to optimize performance. |
AnnotationAttributes(java.util.Map<java.lang.String,java.lang.Object> map)
Create a new
AnnotationAttributes instance, wrapping the
provided map and all its key-value pairs. |
Modifier and Type | Method and Description |
---|---|
java.lang.Class<? extends java.lang.annotation.Annotation> |
annotationType()
Get the type of annotation represented by this
AnnotationAttributes instance. |
private void |
assertAttributePresence(java.lang.String attributeName,
java.util.List<java.lang.String> aliases,
java.lang.Object attributeValue) |
private void |
assertAttributePresence(java.lang.String attributeName,
java.lang.Object attributeValue) |
private void |
assertAttributeType(java.lang.String attributeName,
java.lang.Object attributeValue,
java.lang.Class<?> expectedType) |
private void |
assertNotException(java.lang.String attributeName,
java.lang.Object attributeValue) |
static AnnotationAttributes |
fromMap(java.util.Map<java.lang.String,java.lang.Object> map)
Return an
AnnotationAttributes instance based on the given map. |
java.lang.Class<?>[] |
getAliasedClassArray(java.lang.String attributeName,
java.lang.Class<? extends java.lang.annotation.Annotation> annotationType,
java.lang.Object annotationSource)
Get the value stored under the specified
attributeName as an
array of classes, taking into account alias semantics defined via
@AliasFor . |
java.lang.String |
getAliasedString(java.lang.String attributeName,
java.lang.Class<? extends java.lang.annotation.Annotation> annotationType,
java.lang.Object annotationSource)
Get the value stored under the specified
attributeName as a
string, taking into account alias semantics defined via
@AliasFor . |
java.lang.String[] |
getAliasedStringArray(java.lang.String attributeName,
java.lang.Class<? extends java.lang.annotation.Annotation> annotationType,
java.lang.Object annotationSource)
Get the value stored under the specified
attributeName as an
array of strings, taking into account alias semantics defined via
@AliasFor . |
AnnotationAttributes |
getAnnotation(java.lang.String attributeName)
Get the
AnnotationAttributes stored under the specified
attributeName . |
<A extends java.lang.annotation.Annotation> |
getAnnotation(java.lang.String attributeName,
java.lang.Class<A> annotationType)
Get the annotation of type
annotationType stored under the
specified attributeName . |
AnnotationAttributes[] |
getAnnotationArray(java.lang.String attributeName)
Get the array of
AnnotationAttributes stored under the specified
attributeName . |
<A extends java.lang.annotation.Annotation> |
getAnnotationArray(java.lang.String attributeName,
java.lang.Class<A> annotationType)
Get the array of type
annotationType stored under the specified
attributeName . |
private <T> T |
getAttribute(java.lang.String attributeName,
java.lang.Class<T> expectedType)
Get the value stored under the specified
attributeName ,
ensuring that the value is of the expectedType . |
boolean |
getBoolean(java.lang.String attributeName)
Get the value stored under the specified
attributeName as a
boolean. |
<T> java.lang.Class<? extends T> |
getClass(java.lang.String attributeName)
Get the value stored under the specified
attributeName as a
class. |
java.lang.Class<?>[] |
getClassArray(java.lang.String attributeName)
Get the value stored under the specified
attributeName as an
array of classes. |
<E extends java.lang.Enum<?>> |
getEnum(java.lang.String attributeName)
Get the value stored under the specified
attributeName as an
enum. |
<N extends java.lang.Number> |
getNumber(java.lang.String attributeName)
Get the value stored under the specified
attributeName as a
number. |
private <T> T |
getRequiredAttribute(java.lang.String attributeName,
java.lang.Class<T> expectedType)
Get the value stored under the specified
attributeName ,
ensuring that the value is of the expectedType . |
private <T> T |
getRequiredAttributeWithAlias(java.lang.String attributeName,
java.lang.Class<? extends java.lang.annotation.Annotation> annotationType,
java.lang.Object annotationSource,
java.lang.Class<T> expectedType)
Get the value stored under the specified
attributeName as an
object of the expectedType , taking into account alias semantics
defined via @AliasFor . |
java.lang.String |
getString(java.lang.String attributeName)
Get the value stored under the specified
attributeName as a
string. |
java.lang.String[] |
getStringArray(java.lang.String attributeName)
Get the value stored under the specified
attributeName as an
array of strings. |
java.lang.Object |
putIfAbsent(java.lang.String key,
java.lang.Object value)
Store the supplied
value in this map under the specified
key , unless a value is already stored under the key. |
java.lang.String |
toString() |
private java.lang.String |
valueToString(java.lang.Object value) |
clear, containsValue, entrySet, forEach, get, getOrDefault, keySet, removeEldestEntry, replaceAll, values
clone, compute, computeIfAbsent, computeIfPresent, containsKey, isEmpty, merge, put, putAll, remove, remove, replace, replace, size
private final java.lang.Class<? extends java.lang.annotation.Annotation> annotationType
private final java.lang.String displayName
public AnnotationAttributes()
AnnotationAttributes
instance.public AnnotationAttributes(java.lang.Class<? extends java.lang.annotation.Annotation> annotationType)
AnnotationAttributes
instance for the
specified annotationType
.annotationType
- the type of annotation represented by this
AnnotationAttributes
instance; never null
public AnnotationAttributes(int initialCapacity)
AnnotationAttributes
instance with the
given initial capacity to optimize performance.initialCapacity
- initial size of the underlying mappublic AnnotationAttributes(java.util.Map<java.lang.String,java.lang.Object> map)
AnnotationAttributes
instance, wrapping the
provided map and all its key-value pairs.map
- original source of annotation attribute key-value
pairsfromMap(Map)
public java.lang.Class<? extends java.lang.annotation.Annotation> annotationType()
AnnotationAttributes
instance.null
if unknownpublic java.lang.String getString(java.lang.String attributeName)
attributeName
as a
string.attributeName
- the name of the attribute to get; never
null
or emptyjava.lang.IllegalArgumentException
- if the attribute does not exist or
if it is not of the expected typepublic java.lang.String getAliasedString(java.lang.String attributeName, java.lang.Class<? extends java.lang.annotation.Annotation> annotationType, java.lang.Object annotationSource)
attributeName
as a
string, taking into account alias semantics defined via
@AliasFor
.
If there is no value stored under the specified attributeName
but the attribute has an alias declared via @AliasFor
, the
value of the alias will be returned.
attributeName
- the name of the attribute to get; never
null
or emptyannotationType
- the type of annotation represented by this
AnnotationAttributes
instance; never null
annotationSource
- the source of the annotation represented by
this AnnotationAttributes
(e.g., the AnnotatedElement
);
or null
if unknownjava.lang.IllegalArgumentException
- if the attribute and its alias do
not exist or are not of type String
AnnotationConfigurationException
- if the attribute and its
alias are both present with different non-empty valuesObjectUtils.isEmpty(Object)
public java.lang.String[] getStringArray(java.lang.String attributeName)
attributeName
as an
array of strings.
If the value stored under the specified attributeName
is
a string, it will be wrapped in a single-element array before
returning it.
attributeName
- the name of the attribute to get; never
null
or emptyjava.lang.IllegalArgumentException
- if the attribute does not exist or
if it is not of the expected typepublic java.lang.String[] getAliasedStringArray(java.lang.String attributeName, java.lang.Class<? extends java.lang.annotation.Annotation> annotationType, java.lang.Object annotationSource)
attributeName
as an
array of strings, taking into account alias semantics defined via
@AliasFor
.
If there is no value stored under the specified attributeName
but the attribute has an alias declared via @AliasFor
, the
value of the alias will be returned.
attributeName
- the name of the attribute to get; never
null
or emptyannotationType
- the type of annotation represented by this
AnnotationAttributes
instance; never null
annotationSource
- the source of the annotation represented by
this AnnotationAttributes
(e.g., the AnnotatedElement
);
or null
if unknownjava.lang.IllegalArgumentException
- if the attribute and its alias do
not exist or are not of type String[]
AnnotationConfigurationException
- if the attribute and its
alias are both present with different non-empty valuespublic boolean getBoolean(java.lang.String attributeName)
attributeName
as a
boolean.attributeName
- the name of the attribute to get; never
null
or emptyjava.lang.IllegalArgumentException
- if the attribute does not exist or
if it is not of the expected typepublic <N extends java.lang.Number> N getNumber(java.lang.String attributeName)
attributeName
as a
number.attributeName
- the name of the attribute to get; never
null
or emptyjava.lang.IllegalArgumentException
- if the attribute does not exist or
if it is not of the expected typepublic <E extends java.lang.Enum<?>> E getEnum(java.lang.String attributeName)
attributeName
as an
enum.attributeName
- the name of the attribute to get; never
null
or emptyjava.lang.IllegalArgumentException
- if the attribute does not exist or
if it is not of the expected typepublic <T> java.lang.Class<? extends T> getClass(java.lang.String attributeName)
attributeName
as a
class.attributeName
- the name of the attribute to get; never
null
or emptyjava.lang.IllegalArgumentException
- if the attribute does not exist or
if it is not of the expected typepublic java.lang.Class<?>[] getClassArray(java.lang.String attributeName)
attributeName
as an
array of classes.
If the value stored under the specified attributeName
is
a class, it will be wrapped in a single-element array before
returning it.
attributeName
- the name of the attribute to get; never
null
or emptyjava.lang.IllegalArgumentException
- if the attribute does not exist or
if it is not of the expected typepublic java.lang.Class<?>[] getAliasedClassArray(java.lang.String attributeName, java.lang.Class<? extends java.lang.annotation.Annotation> annotationType, java.lang.Object annotationSource)
attributeName
as an
array of classes, taking into account alias semantics defined via
@AliasFor
.
If there is no value stored under the specified attributeName
but the attribute has an alias declared via @AliasFor
, the
value of the alias will be returned.
attributeName
- the name of the attribute to get; never
null
or emptyannotationType
- the type of annotation represented by this
AnnotationAttributes
instance; never null
annotationSource
- the source of the annotation represented by
this AnnotationAttributes
(e.g., the AnnotatedElement
);
or null
if unknownjava.lang.IllegalArgumentException
- if the attribute and its alias do
not exist or are not of type Class[]
AnnotationConfigurationException
- if the attribute and its
alias are both present with different non-empty valuespublic AnnotationAttributes getAnnotation(java.lang.String attributeName)
AnnotationAttributes
stored under the specified
attributeName
.
Note: if you expect an actual annotation, invoke
getAnnotation(String, Class)
instead.
attributeName
- the name of the attribute to get; never
null
or emptyAnnotationAttributes
java.lang.IllegalArgumentException
- if the attribute does not exist or
if it is not of the expected typepublic <A extends java.lang.annotation.Annotation> A getAnnotation(java.lang.String attributeName, java.lang.Class<A> annotationType)
annotationType
stored under the
specified attributeName
.attributeName
- the name of the attribute to get; never
null
or emptyannotationType
- the expected annotation type; never null
java.lang.IllegalArgumentException
- if the attribute does not exist or
if it is not of the expected typepublic AnnotationAttributes[] getAnnotationArray(java.lang.String attributeName)
AnnotationAttributes
stored under the specified
attributeName
.
If the value stored under the specified attributeName
is
an instance of AnnotationAttributes
, it will be wrapped in
a single-element array before returning it.
Note: if you expect an actual array of annotations, invoke
getAnnotationArray(String, Class)
instead.
attributeName
- the name of the attribute to get; never
null
or emptyAnnotationAttributes
java.lang.IllegalArgumentException
- if the attribute does not exist or
if it is not of the expected typepublic <A extends java.lang.annotation.Annotation> A[] getAnnotationArray(java.lang.String attributeName, java.lang.Class<A> annotationType)
annotationType
stored under the specified
attributeName
.
If the value stored under the specified attributeName
is
an Annotation
, it will be wrapped in a single-element array
before returning it.
attributeName
- the name of the attribute to get; never
null
or emptyannotationType
- the expected annotation type; never null
java.lang.IllegalArgumentException
- if the attribute does not exist or
if it is not of the expected typeprivate <T> T getRequiredAttribute(java.lang.String attributeName, java.lang.Class<T> expectedType)
attributeName
,
ensuring that the value is of the expectedType
.
If the expectedType
is an array and the value stored
under the specified attributeName
is a single element of the
component type of the expected array type, the single element will be
wrapped in a single-element array of the appropriate type before
returning it.
attributeName
- the name of the attribute to get; never
null
or emptyexpectedType
- the expected type; never null
java.lang.IllegalArgumentException
- if the attribute does not exist or
if it is not of the expected typeprivate <T> T getRequiredAttributeWithAlias(java.lang.String attributeName, java.lang.Class<? extends java.lang.annotation.Annotation> annotationType, java.lang.Object annotationSource, java.lang.Class<T> expectedType)
attributeName
as an
object of the expectedType
, taking into account alias semantics
defined via @AliasFor
.
If there is no value stored under the specified attributeName
but the attribute has an alias declared via @AliasFor
, the
value of the alias will be returned.
attributeName
- the name of the attribute to get; never
null
or emptyannotationType
- the type of annotation represented by this
AnnotationAttributes
instance; never null
annotationSource
- the source of the annotation represented by
this AnnotationAttributes
(e.g., the AnnotatedElement
);
or null
if unknownexpectedType
- the expected type; never null
java.lang.IllegalArgumentException
- if the attribute and its alias do
not exist or are not of the expectedType
AnnotationConfigurationException
- if the attribute and its
alias are both present with different non-empty valuesObjectUtils.isEmpty(Object)
private <T> T getAttribute(java.lang.String attributeName, java.lang.Class<T> expectedType)
attributeName
,
ensuring that the value is of the expectedType
.attributeName
- the name of the attribute to get; never
null
or emptyexpectedType
- the expected type; never null
java.lang.IllegalArgumentException
- if the attribute is not of the
expected typegetRequiredAttribute(String, Class)
private void assertAttributePresence(java.lang.String attributeName, java.lang.Object attributeValue)
private void assertAttributePresence(java.lang.String attributeName, java.util.List<java.lang.String> aliases, java.lang.Object attributeValue)
private void assertNotException(java.lang.String attributeName, java.lang.Object attributeValue)
private void assertAttributeType(java.lang.String attributeName, java.lang.Object attributeValue, java.lang.Class<?> expectedType)
public java.lang.Object putIfAbsent(java.lang.String key, java.lang.Object value)
value
in this map under the specified
key
, unless a value is already stored under the key.putIfAbsent
in interface java.util.Map<java.lang.String,java.lang.Object>
putIfAbsent
in class java.util.HashMap<java.lang.String,java.lang.Object>
key
- the key under which to store the valuevalue
- the value to storenull
if no
value was previously stored in this mapLinkedHashMap.get(java.lang.Object)
,
HashMap.put(K, V)
public java.lang.String toString()
toString
in class java.util.AbstractMap<java.lang.String,java.lang.Object>
private java.lang.String valueToString(java.lang.Object value)
public static AnnotationAttributes fromMap(java.util.Map<java.lang.String,java.lang.Object> map)
AnnotationAttributes
instance based on the given map.
If the map is already an AnnotationAttributes
instance, it
will be cast and returned immediately without creating a new instance.
Otherwise a new instance will be created by passing the supplied map
to the AnnotationAttributes(Map)
constructor.
map
- original source of annotation attribute key-value pairs