Class AnnotationAttributes
- java.lang.Object
- java.util.AbstractMap<K,V>
- java.util.HashMap<K,V>
- java.util.LinkedHashMap<String,Object>
- org.springframework.core.annotation.AnnotationAttributes
- All Implemented Interfaces:
Serializable
,Cloneable
,Map<String,Object>
public class AnnotationAttributes extends LinkedHashMap<String,Object>
LinkedHashMap
subclass representing annotation attribute key-value pairs as read byAnnotationUtils
,AnnotatedElementUtils
, and Spring's reflection- and ASM-basedAnnotationMetadata
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.
- Since:
- 3.1.1
- Author:
- Chris Beams, Sam Brannen, Juergen Hoeller
- See Also:
AnnotationUtils.getAnnotationAttributes(java.lang.annotation.Annotation)
,AnnotatedElementUtils
, Serialized Form
Nested Class Summary
Nested classes/interfaces inherited from class java.util.AbstractMap
AbstractMap.SimpleEntry<K extends Object,V extends Object>, AbstractMap.SimpleImmutableEntry<K extends Object,V extends Object>
Constructor Summary
Constructors Constructor Description AnnotationAttributes()
Create a new, emptyAnnotationAttributes
instance.AnnotationAttributes(int initialCapacity)
Create a new, emptyAnnotationAttributes
instance with the given initial capacity to optimize performance.AnnotationAttributes(Class<? extends Annotation> annotationType)
Create a new, emptyAnnotationAttributes
instance for the specifiedannotationType
.AnnotationAttributes(String annotationType, ClassLoader classLoader)
Create a new, emptyAnnotationAttributes
instance for the specifiedannotationType
.AnnotationAttributes(Map<String,Object> map)
Create a newAnnotationAttributes
instance, wrapping the provided map and all its key-value pairs.AnnotationAttributes(AnnotationAttributes other)
Create a newAnnotationAttributes
instance, wrapping the provided map and all its key-value pairs.
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description Class<? extends Annotation>
annotationType()
Get the type of annotation represented by thisAnnotationAttributes
instance.static AnnotationAttributes
fromMap(Map<String,Object> map)
Return anAnnotationAttributes
instance based on the given map.Class<?>[]
getAliasedClassArray(String attributeName, Class<? extends Annotation> annotationType, Object annotationSource)
Deprecated.as of Spring 4.3.2, in favor of built-in alias resolution ingetClassArray(java.lang.String)
itselfString
getAliasedString(String attributeName, Class<? extends Annotation> annotationType, Object annotationSource)
Deprecated.as of Spring 4.3.2, in favor of built-in alias resolution ingetString(java.lang.String)
itselfString[]
getAliasedStringArray(String attributeName, Class<? extends Annotation> annotationType, Object annotationSource)
Deprecated.as of Spring 4.3.2, in favor of built-in alias resolution ingetStringArray(java.lang.String)
itselfAnnotationAttributes
getAnnotation(String attributeName)
Get theAnnotationAttributes
stored under the specifiedattributeName
.<A extends Annotation>
AgetAnnotation(String attributeName, Class<A> annotationType)
Get the annotation of typeannotationType
stored under the specifiedattributeName
.AnnotationAttributes[]
getAnnotationArray(String attributeName)
Get the array ofAnnotationAttributes
stored under the specifiedattributeName
.<A extends Annotation>
A[]getAnnotationArray(String attributeName, Class<A> annotationType)
Get the array of typeannotationType
stored under the specifiedattributeName
.boolean
getBoolean(String attributeName)
Get the value stored under the specifiedattributeName
as a boolean.<T> Class<? extends T>
getClass(String attributeName)
Get the value stored under the specifiedattributeName
as a class.Class<?>[]
getClassArray(String attributeName)
Get the value stored under the specifiedattributeName
as an array of classes.<E extends Enum<?>>
EgetEnum(String attributeName)
Get the value stored under the specifiedattributeName
as an enum.<N extends Number>
NgetNumber(String attributeName)
Get the value stored under the specifiedattributeName
as a number.String
getString(String attributeName)
Get the value stored under the specifiedattributeName
as a string.String[]
getStringArray(String attributeName)
Get the value stored under the specifiedattributeName
as an array of strings.Object
putIfAbsent(String key, Object value)
Store the suppliedvalue
in this map under the specifiedkey
, unless a value is already stored under the key.String
toString()
Methods inherited from class java.util.LinkedHashMap
clear, containsValue, entrySet, forEach, get, getOrDefault, keySet, removeEldestEntry, replaceAll, values
Methods inherited from class java.util.HashMap
clone, compute, computeIfAbsent, computeIfPresent, containsKey, isEmpty, merge, put, putAll, remove, remove, replace, replace, size
Methods inherited from class java.util.AbstractMap
equals, hashCode
Constructor Detail
AnnotationAttributes
public AnnotationAttributes()
Create a new, emptyAnnotationAttributes
instance.
AnnotationAttributes
public AnnotationAttributes(int initialCapacity)
Create a new, emptyAnnotationAttributes
instance with the given initial capacity to optimize performance.- Parameters:
initialCapacity
- initial size of the underlying map
AnnotationAttributes
public AnnotationAttributes(Class<? extends Annotation> annotationType)
Create a new, emptyAnnotationAttributes
instance for the specifiedannotationType
.- Parameters:
annotationType
- the type of annotation represented by thisAnnotationAttributes
instance; nevernull
- Since:
- 4.2
AnnotationAttributes
public AnnotationAttributes(String annotationType, ClassLoader classLoader)
Create a new, emptyAnnotationAttributes
instance for the specifiedannotationType
.- Parameters:
annotationType
- the annotation type name represented by thisAnnotationAttributes
instance; nevernull
classLoader
- the ClassLoader to try to load the annotation type on, ornull
to just store the annotation type name- Since:
- 4.3.2
AnnotationAttributes
public AnnotationAttributes(Map<String,Object> map)
Create a newAnnotationAttributes
instance, wrapping the provided map and all its key-value pairs.- Parameters:
map
- original source of annotation attribute key-value pairs- See Also:
fromMap(Map)
AnnotationAttributes
public AnnotationAttributes(AnnotationAttributes other)
Create a newAnnotationAttributes
instance, wrapping the provided map and all its key-value pairs.- Parameters:
other
- original source of annotation attribute key-value pairs- See Also:
fromMap(Map)
Method Detail
annotationType
public Class<? extends Annotation> annotationType()
Get the type of annotation represented by thisAnnotationAttributes
instance.- Returns:
- the annotation type, or
null
if unknown - Since:
- 4.2
getString
public String getString(String attributeName)
Get the value stored under the specifiedattributeName
as a string.- Parameters:
attributeName
- the name of the attribute to get; nevernull
or empty- Returns:
- the value
- Throws:
IllegalArgumentException
- if the attribute does not exist or if it is not of the expected type
getAliasedString
@Deprecated public String getAliasedString(String attributeName, Class<? extends Annotation> annotationType, Object annotationSource)
Deprecated.as of Spring 4.3.2, in favor of built-in alias resolution ingetString(java.lang.String)
itselfGet the value stored under the specifiedattributeName
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.- Parameters:
attributeName
- the name of the attribute to get; nevernull
or emptyannotationType
- the type of annotation represented by thisAnnotationAttributes
instance; nevernull
annotationSource
- the source of the annotation represented by thisAnnotationAttributes
(e.g., theAnnotatedElement
); ornull
if unknown- Returns:
- the string value
- Throws:
IllegalArgumentException
- if the attribute and its alias do not exist or are not of typeString
AnnotationConfigurationException
- if the attribute and its alias are both present with different non-empty values- Since:
- 4.2
getStringArray
public String[] getStringArray(String attributeName)
Get the value stored under the specifiedattributeName
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.- Parameters:
attributeName
- the name of the attribute to get; nevernull
or empty- Returns:
- the value
- Throws:
IllegalArgumentException
- if the attribute does not exist or if it is not of the expected type
getAliasedStringArray
@Deprecated public String[] getAliasedStringArray(String attributeName, Class<? extends Annotation> annotationType, Object annotationSource)
Deprecated.as of Spring 4.3.2, in favor of built-in alias resolution ingetStringArray(java.lang.String)
itselfGet the value stored under the specifiedattributeName
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.- Parameters:
attributeName
- the name of the attribute to get; nevernull
or emptyannotationType
- the type of annotation represented by thisAnnotationAttributes
instance; nevernull
annotationSource
- the source of the annotation represented by thisAnnotationAttributes
(e.g., theAnnotatedElement
); ornull
if unknown- Returns:
- the array of strings
- Throws:
IllegalArgumentException
- if the attribute and its alias do not exist or are not of typeString[]
AnnotationConfigurationException
- if the attribute and its alias are both present with different non-empty values- Since:
- 4.2
getBoolean
public boolean getBoolean(String attributeName)
Get the value stored under the specifiedattributeName
as a boolean.- Parameters:
attributeName
- the name of the attribute to get; nevernull
or empty- Returns:
- the value
- Throws:
IllegalArgumentException
- if the attribute does not exist or if it is not of the expected type
getNumber
public <N extends Number> N getNumber(String attributeName)
Get the value stored under the specifiedattributeName
as a number.- Parameters:
attributeName
- the name of the attribute to get; nevernull
or empty- Returns:
- the value
- Throws:
IllegalArgumentException
- if the attribute does not exist or if it is not of the expected type
getEnum
public <E extends Enum<?>> E getEnum(String attributeName)
Get the value stored under the specifiedattributeName
as an enum.- Parameters:
attributeName
- the name of the attribute to get; nevernull
or empty- Returns:
- the value
- Throws:
IllegalArgumentException
- if the attribute does not exist or if it is not of the expected type
getClass
public <T> Class<? extends T> getClass(String attributeName)
Get the value stored under the specifiedattributeName
as a class.- Parameters:
attributeName
- the name of the attribute to get; nevernull
or empty- Returns:
- the value
- Throws:
IllegalArgumentException
- if the attribute does not exist or if it is not of the expected type
getClassArray
public Class<?>[] getClassArray(String attributeName)
Get the value stored under the specifiedattributeName
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.- Parameters:
attributeName
- the name of the attribute to get; nevernull
or empty- Returns:
- the value
- Throws:
IllegalArgumentException
- if the attribute does not exist or if it is not of the expected type
getAliasedClassArray
@Deprecated public Class<?>[] getAliasedClassArray(String attributeName, Class<? extends Annotation> annotationType, Object annotationSource)
Deprecated.as of Spring 4.3.2, in favor of built-in alias resolution ingetClassArray(java.lang.String)
itselfGet the value stored under the specifiedattributeName
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.- Parameters:
attributeName
- the name of the attribute to get; nevernull
or emptyannotationType
- the type of annotation represented by thisAnnotationAttributes
instance; nevernull
annotationSource
- the source of the annotation represented by thisAnnotationAttributes
(e.g., theAnnotatedElement
); ornull
if unknown- Returns:
- the array of classes
- Throws:
IllegalArgumentException
- if the attribute and its alias do not exist or are not of typeClass[]
AnnotationConfigurationException
- if the attribute and its alias are both present with different non-empty values- Since:
- 4.2
getAnnotation
public AnnotationAttributes getAnnotation(String attributeName)
Get theAnnotationAttributes
stored under the specifiedattributeName
.Note: if you expect an actual annotation, invoke
getAnnotation(String, Class)
instead.- Parameters:
attributeName
- the name of the attribute to get; nevernull
or empty- Returns:
- the
AnnotationAttributes
- Throws:
IllegalArgumentException
- if the attribute does not exist or if it is not of the expected type
getAnnotation
public <A extends Annotation> A getAnnotation(String attributeName, Class<A> annotationType)
Get the annotation of typeannotationType
stored under the specifiedattributeName
.- Parameters:
attributeName
- the name of the attribute to get; nevernull
or emptyannotationType
- the expected annotation type; nevernull
- Returns:
- the annotation
- Throws:
IllegalArgumentException
- if the attribute does not exist or if it is not of the expected type- Since:
- 4.2
getAnnotationArray
public AnnotationAttributes[] getAnnotationArray(String attributeName)
Get the array ofAnnotationAttributes
stored under the specifiedattributeName
.If the value stored under the specified
attributeName
is an instance ofAnnotationAttributes
, 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.- Parameters:
attributeName
- the name of the attribute to get; nevernull
or empty- Returns:
- the array of
AnnotationAttributes
- Throws:
IllegalArgumentException
- if the attribute does not exist or if it is not of the expected type
getAnnotationArray
public <A extends Annotation> A[] getAnnotationArray(String attributeName, Class<A> annotationType)
Get the array of typeannotationType
stored under the specifiedattributeName
.If the value stored under the specified
attributeName
is anAnnotation
, it will be wrapped in a single-element array before returning it.- Parameters:
attributeName
- the name of the attribute to get; nevernull
or emptyannotationType
- the expected annotation type; nevernull
- Returns:
- the annotation array
- Throws:
IllegalArgumentException
- if the attribute does not exist or if it is not of the expected type- Since:
- 4.2
putIfAbsent
public Object putIfAbsent(String key, Object value)
Store the suppliedvalue
in this map under the specifiedkey
, unless a value is already stored under the key.- Specified by:
putIfAbsent
in interfaceMap<String,Object>
- Overrides:
putIfAbsent
in classHashMap<String,Object>
- Parameters:
key
- the key under which to store the valuevalue
- the value to store- Returns:
- the current value stored in this map, or
null
if no value was previously stored in this map - Since:
- 4.2
- See Also:
LinkedHashMap.get(java.lang.Object)
,HashMap.put(K, V)
fromMap
public static AnnotationAttributes fromMap(Map<String,Object> map)
Return anAnnotationAttributes
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 theAnnotationAttributes(Map)
constructor.- Parameters:
map
- original source of annotation attribute key-value pairs