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

public Class AnnotationAttributes

extends LinkedHashMap<String, Object>
Class Inheritance
Annotations
@SuppressWarnings:serial
Imports
java.lang.annotation.Annotation, java.lang.reflect.Array, java.util.Iterator, .LinkedHashMap, .Map, org.springframework.lang.Nullable, org.springframework.util.Assert, .StringUtils

LinkedHashMap subclass representing annotation attribute key-value pairs as read by AnnotationUtils, AnnotatedElementUtils, and Spring's reflection- and ASM-based org.springframework.core.type.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.

Authors
Chris Beams, Sam Brannen, Juergen Hoeller
Since
3.1.1
See Also
AnnotationUtils#getAnnotationAttributes, AnnotatedElementUtils

Field Summary

Modifier and TypeField and Description
private final Class<? extends Annotation>
pack-priv final String
private static final String
pack-priv final boolean

Constructor Summary

AccessConstructor and Description
public
AnnotationAttributes()

Create a new, empty AnnotationAttributes instance.

public
AnnotationAttributes(int
initial size of the underlying map
initialCapacity
)

Create a new, empty AnnotationAttributes instance with the given initial capacity to optimize performance.

public
AnnotationAttributes(Map<String, Object>
original source of annotation attribute key-value pairs
map
)

Create a new AnnotationAttributes instance, wrapping the provided map and all its key-value pairs.

public
AnnotationAttributes(AnnotationAttributes
original source of annotation attribute key-value pairs
other
)

Create a new AnnotationAttributes instance, wrapping the provided map and all its key-value pairs.

public
AnnotationAttributes(Class<? extends Annotation>
the type of annotation represented by this AnnotationAttributes instance; never null
annotationType
)

Create a new, empty AnnotationAttributes instance for the specified annotationType.

public
AnnotationAttributes(String
the annotation type name represented by this AnnotationAttributes instance; never null
annotationType
,
ClassLoader
the ClassLoader to try to load the annotation type on, or null to just store the annotation type name
classLoader
)

Create a new, empty AnnotationAttributes instance for the specified annotationType.

pack-priv
AnnotationAttributes(Class<? extends Annotation>
the type of annotation represented by this AnnotationAttributes instance; never null
annotationType
,
boolean
if the attributes are considered already validated
validated
)

Create a possibly already validated new, empty AnnotationAttributes instance for the specified annotationType.

Method Summary

Modifier and TypeMethod and Description
public Class<? extends Annotation>

Returns:

the annotation type, or null if unknown
annotationType
()

Get the type of annotation represented by this AnnotationAttributes.

private void
assertAttributePresence(String attributeName, Object attributeValue)

private void
assertAttributeType(String attributeName, Object attributeValue, Class<?> expectedType)

private void
assertNotException(String attributeName, Object attributeValue)

public static AnnotationAttributes
fromMap(Map<String, Object>
original source of annotation attribute key-value pairs
map
)

Return an AnnotationAttributes instance based on the given map.

public AnnotationAttributes

Returns:

the AnnotationAttributes
getAnnotation
(String
the name of the attribute to get; never null or empty
attributeName
)

Get the AnnotationAttributes stored under the specified attributeName.

public <A extends Annotation> A

Returns:

the annotation
getAnnotation
(String
the name of the attribute to get; never null or empty
attributeName
,
Class<A>
the expected annotation type; never null
annotationType
)

Get the annotation of type annotationType stored under the specified attributeName.

public AnnotationAttributes[]

Returns:

the array of AnnotationAttributes
getAnnotationArray
(String
the name of the attribute to get; never null or empty
attributeName
)

Get the array of AnnotationAttributes stored under the specified attributeName.

public <A extends Annotation> A[]

Returns:

the annotation array
getAnnotationArray
(String
the name of the attribute to get; never null or empty
attributeName
,
Class<A>
the expected annotation type; never null
annotationType
)

Get the array of type annotationType stored under the specified attributeName.

private static Class<? extends Annotation>
getAnnotationType(String annotationType, ClassLoader classLoader)

public boolean

Returns:

the value
getBoolean
(String
the name of the attribute to get; never null or empty
attributeName
)

Get the value stored under the specified attributeName as a boolean.

public <T> Class<? extends T>

Returns:

the value
getClass
(String
the name of the attribute to get; never null or empty
attributeName
)

Get the value stored under the specified attributeName as a class.

public Class<?>[]

Returns:

the value
getClassArray
(String
the name of the attribute to get; never null or empty
attributeName
)

Get the value stored under the specified attributeName as an array of classes.

public <E extends Enum<?>> E

Returns:

the value
getEnum
(String
the name of the attribute to get; never null or empty
attributeName
)

Get the value stored under the specified attributeName as an enum.

public <N extends Number> N

Returns:

the value
getNumber
(String
the name of the attribute to get; never null or empty
attributeName
)

Get the value stored under the specified attributeName as a number.

private <T> T

Returns:

the value
getRequiredAttribute
(String
the name of the attribute to get; never null or empty
attributeName
,
Class<T>
the expected type; never null
expectedType
)

Get the value stored under the specified attributeName, ensuring that the value is of the expectedType.

public String

Returns:

the value
getString
(String
the name of the attribute to get; never null or empty
attributeName
)

Get the value stored under the specified attributeName as a string.

public String[]

Returns:

the value
getStringArray
(String
the name of the attribute to get; never null or empty
attributeName
)

Get the value stored under the specified attributeName as an array of strings.

public String
toString()

Overrides java.util.AbstractMap.toString.

Returns a string representation of this map.

private String
Inherited from java.util.LinkedHashMap:
clearcontainsValueentrySetforEachgetgetOrDefaultkeySetnewLinkedHashMapputFirstputLastremoveEldestEntryreplaceAllreversedsequencedEntrySetsequencedKeySetsequencedValuesvalues

Field Detail

annotationTypeback to summary
private final Class<? extends Annotation> annotationType
Annotations
@Nullable
displayNameback to summary
pack-priv final String displayName
UNKNOWNback to summary
private static final String UNKNOWN
validatedback to summary
pack-priv final boolean validated

Constructor Detail

AnnotationAttributesback to summary
public AnnotationAttributes()

Create a new, empty AnnotationAttributes instance.

AnnotationAttributesback to summary
public AnnotationAttributes(int initialCapacity)

Create a new, empty AnnotationAttributes instance with the given initial capacity to optimize performance.

Parameters
initialCapacity:int

initial size of the underlying map

AnnotationAttributesback to summary
public AnnotationAttributes(Map<String, Object> map)

Create a new AnnotationAttributes instance, wrapping the provided map and all its key-value pairs.

Parameters
map:Map<String, Object>

original source of annotation attribute key-value pairs

See Also
fromMap(Map)
AnnotationAttributesback to summary
public AnnotationAttributes(AnnotationAttributes other)

Create a new AnnotationAttributes instance, wrapping the provided map and all its key-value pairs.

Parameters
other:AnnotationAttributes

original source of annotation attribute key-value pairs

See Also
fromMap(Map)
AnnotationAttributesback to summary
public AnnotationAttributes(Class<? extends Annotation> annotationType)

Create a new, empty AnnotationAttributes instance for the specified annotationType.

Parameters
annotationType:Class<? extends Annotation>

the type of annotation represented by this AnnotationAttributes instance; never null

Since
4.2
AnnotationAttributesback to summary
public AnnotationAttributes(String annotationType, ClassLoader classLoader)

Create a new, empty AnnotationAttributes instance for the specified annotationType.

Parameters
annotationType:String

the annotation type name represented by this AnnotationAttributes instance; never null

classLoader:ClassLoader

the ClassLoader to try to load the annotation type on, or null to just store the annotation type name

Since
4.3.2
AnnotationAttributesback to summary
pack-priv AnnotationAttributes(Class<? extends Annotation> annotationType, boolean validated)

Create a possibly already validated new, empty AnnotationAttributes instance for the specified annotationType.

Parameters
annotationType:Class<? extends Annotation>

the type of annotation represented by this AnnotationAttributes instance; never null

validated:boolean

if the attributes are considered already validated

Since
5.2

Method Detail

annotationTypeback to summary
public Class<? extends Annotation> annotationType()

Get the type of annotation represented by this AnnotationAttributes.

Returns:Class<? extends Annotation>

the annotation type, or null if unknown

Annotations
@Nullable
Since
4.2
assertAttributePresenceback to summary
private void assertAttributePresence(String attributeName, Object attributeValue)
assertAttributeTypeback to summary
private void assertAttributeType(String attributeName, Object attributeValue, Class<?> expectedType)
assertNotExceptionback to summary
private void assertNotException(String attributeName, Object attributeValue)
fromMapback to summary
public static AnnotationAttributes fromMap(Map<String, Object> map)

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

Parameters
map:Map<String, Object>

original source of annotation attribute key-value pairs

Annotations
@Nullable
getAnnotationback to summary
public AnnotationAttributes getAnnotation(String attributeName)

Get the AnnotationAttributes stored under the specified attributeName.

Note

if you expect an actual annotation, invoke getAnnotation(String, Class) instead.

Parameters
attributeName:String

the name of the attribute to get; never null or empty

Returns:AnnotationAttributes

the AnnotationAttributes

Exceptions
IllegalArgumentException:
if the attribute does not exist or if it is not of the expected type
getAnnotationback to summary
public <A extends Annotation> A getAnnotation(String attributeName, Class<A> annotationType)

Get the annotation of type annotationType stored under the specified attributeName.

Parameters
attributeName:String

the name of the attribute to get; never null or empty

annotationType:Class<A>

the expected annotation type; never null

Returns:A

the annotation

Exceptions
IllegalArgumentException:
if the attribute does not exist or if it is not of the expected type
Since
4.2
getAnnotationArrayback to summary
public AnnotationAttributes[] getAnnotationArray(String attributeName)

Get the array of 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.

Parameters
attributeName:String

the name of the attribute to get; never null or empty

Returns:AnnotationAttributes[]

the array of AnnotationAttributes

Exceptions
IllegalArgumentException:
if the attribute does not exist or if it is not of the expected type
getAnnotationArrayback to summary
public <A extends Annotation> A[] getAnnotationArray(String attributeName, Class<A> annotationType)

Get the array of type 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.

Parameters
attributeName:String

the name of the attribute to get; never null or empty

annotationType:Class<A>

the expected annotation type; never null

Returns:A[]

the annotation array

Annotations
@SuppressWarnings:unchecked
Exceptions
IllegalArgumentException:
if the attribute does not exist or if it is not of the expected type
Since
4.2
getAnnotationTypeback to summary
private static Class<? extends Annotation> getAnnotationType(String annotationType, ClassLoader classLoader)
Annotations
@SuppressWarnings:unchecked
@Nullable
getBooleanback to summary
public boolean getBoolean(String attributeName)

Get the value stored under the specified attributeName as a boolean.

Parameters
attributeName:String

the name of the attribute to get; never null or empty

Returns:boolean

the value

Exceptions
IllegalArgumentException:
if the attribute does not exist or if it is not of the expected type
getClassback to summary
public <T> Class<? extends T> getClass(String attributeName)

Get the value stored under the specified attributeName as a class.

Parameters
attributeName:String

the name of the attribute to get; never null or empty

Returns:Class<? extends T>

the value

Annotations
@SuppressWarnings:unchecked
Exceptions
IllegalArgumentException:
if the attribute does not exist or if it is not of the expected type
getClassArrayback to summary
public Class<?>[] getClassArray(String attributeName)

Get the value stored under the specified 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.

Parameters
attributeName:String

the name of the attribute to get; never null or empty

Returns:Class<?>[]

the value

Exceptions
IllegalArgumentException:
if the attribute does not exist or if it is not of the expected type
getEnumback to summary
public <E extends Enum<?>> E getEnum(String attributeName)

Get the value stored under the specified attributeName as an enum.

Parameters
attributeName:String

the name of the attribute to get; never null or empty

Returns:E

the value

Annotations
@SuppressWarnings:unchecked
Exceptions
IllegalArgumentException:
if the attribute does not exist or if it is not of the expected type
getNumberback to summary
public <N extends Number> N getNumber(String attributeName)

Get the value stored under the specified attributeName as a number.

Parameters
attributeName:String

the name of the attribute to get; never null or empty

Returns:N

the value

Annotations
@SuppressWarnings:unchecked
Exceptions
IllegalArgumentException:
if the attribute does not exist or if it is not of the expected type
getRequiredAttributeback to summary
private <T> T getRequiredAttribute(String attributeName, Class<T> expectedType)

Get the value stored under the specified 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.

Parameters
attributeName:String

the name of the attribute to get; never null or empty

expectedType:Class<T>

the expected type; never null

Returns:T

the value

Annotations
@SuppressWarnings:unchecked
Exceptions
IllegalArgumentException:
if the attribute does not exist or if it is not of the expected type
getStringback to summary
public String getString(String attributeName)

Get the value stored under the specified attributeName as a string.

Parameters
attributeName:String

the name of the attribute to get; never null or empty

Returns:String

the value

Exceptions
IllegalArgumentException:
if the attribute does not exist or if it is not of the expected type
getStringArrayback to summary
public String[] getStringArray(String attributeName)

Get the value stored under the specified 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.

Parameters
attributeName:String

the name of the attribute to get; never null or empty

Returns:String[]

the value

Exceptions
IllegalArgumentException:
if the attribute does not exist or if it is not of the expected type
toStringback to summary
public String toString()

Overrides java.util.AbstractMap.toString.

Doc from java.util.AbstractMap.toString.

Returns a string representation of this map. The string representation consists of a list of key-value mappings in the order returned by the map's entrySet view's iterator, enclosed in braces ("{}"). Adjacent mappings are separated by the characters ", " (comma and space). Each key-value mapping is rendered as the key followed by an equals sign ("=") followed by the associated value. Keys and values are converted to strings as by String#valueOf(Object).

Returns:String

a string representation of this map

Annotations
@Override
valueToStringback to summary
private String valueToString(Object value)