Class AnnotationUtils
java.lang.Object
io.microsphere.spring.core.annotation.AnnotationUtils
Annotation
Utilities- Since:
- 1.0.0
- Author:
- Mercy
- See Also:
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic <A extends Annotation>
Map<ElementType,List<A>> findAnnotations
(Method method, Class<A> annotationClass) Find specifiedAnnotation
type maps fromMethod
static <A extends Annotation>
Class<A>findAnnotationType
(org.springframework.core.annotation.AnnotationAttributes annotationAttributes) static org.springframework.core.annotation.AnnotationAttributes
getAnnotationAttributes
(Annotation annotation) Get theAnnotationAttributes
static org.springframework.core.annotation.AnnotationAttributes
getAnnotationAttributes
(Annotation annotation, boolean ignoreDefaultValue) Get theAnnotationAttributes
static org.springframework.core.annotation.AnnotationAttributes
getAnnotationAttributes
(Annotation annotation, boolean ignoreDefaultValue, String... ignoreAttributeNames) Get theAnnotationAttributes
static org.springframework.core.annotation.AnnotationAttributes
getAnnotationAttributes
(Annotation annotation, org.springframework.core.env.PropertyResolver propertyResolver, boolean classValuesAsString, boolean nestedAnnotationsAsMap, boolean ignoreDefaultValue, String... ignoreAttributeNames) Get theAnnotationAttributes
static org.springframework.core.annotation.AnnotationAttributes
getAnnotationAttributes
(Annotation annotation, org.springframework.core.env.PropertyResolver propertyResolver, boolean ignoreDefaultValue, String... ignoreAttributeNames) Get theAnnotationAttributes
static org.springframework.core.annotation.AnnotationAttributes
getAnnotationAttributes
(AnnotatedElement annotatedElement, Class<? extends Annotation> annotationType, org.springframework.core.env.PropertyResolver propertyResolver, boolean classValuesAsString, boolean nestedAnnotationsAsMap, boolean ignoreDefaultValue, boolean tryMergedAnnotation, String... ignoreAttributeNames) Get theAnnotationAttributes
, if the argumenttryMergedAnnotation
istrue
, theAnnotationAttributes
will be got frommerged annotation
first, if failed, and then to get fromnormal one
static org.springframework.core.annotation.AnnotationAttributes
getAnnotationAttributes
(AnnotatedElement annotatedElement, Class<? extends Annotation> annotationType, org.springframework.core.env.PropertyResolver propertyResolver, boolean classValuesAsString, boolean nestedAnnotationsAsMap, boolean ignoreDefaultValue, String... ignoreAttributeNames) Get theAnnotationAttributes
static org.springframework.core.annotation.AnnotationAttributes
getAnnotationAttributes
(AnnotatedElement annotatedElement, Class<? extends Annotation> annotationType, org.springframework.core.env.PropertyResolver propertyResolver, boolean ignoreDefaultValue, boolean tryMergedAnnotation, String... ignoreAttributeNames) Get theAnnotationAttributes
, if the argumenttryMergedAnnotation
istrue
, theAnnotationAttributes
will be got frommerged annotation
first, if failed, and then to get fromnormal one
static org.springframework.core.annotation.AnnotationAttributes
getAnnotationAttributes
(AnnotatedElement annotatedElement, Class<? extends Annotation> annotationType, org.springframework.core.env.PropertyResolver propertyResolver, boolean ignoreDefaultValue, String... ignoreAttributeNames) Get theAnnotationAttributes
static org.springframework.core.annotation.AnnotationAttributes
getAnnotationAttributes
(org.springframework.core.type.AnnotationMetadata metadata, Class<? extends Annotation> annotationType) Get an instance ofAnnotationAttributes
fromAnnotationMetadata
with the specified class name ofannotation
.static org.springframework.core.annotation.AnnotationAttributes
getAnnotationAttributes
(org.springframework.core.type.AnnotationMetadata metadata, String annotationClassName) Get an instance ofAnnotationAttributes
fromAnnotationMetadata
with the specified class name ofannotation
.static <T> T
getAttribute
(Annotation annotation, String attributeName) Get the attribute valuestatic <T> T
getAttribute
(Map<String, Object> attributes, String attributeName) Get the attribute valuestatic <T> T
getAttribute
(Map<String, Object> attributes, String attributeName, boolean required) Get the attribute value the willstatic <T> T
getAttribute
(Map<String, Object> attributes, String attributeName, T defaultValue) Get the attribute value with default valuegetAttributes
(Annotation annotation) Get theAnnotation
attributes without default valuesgetAttributes
(Annotation annotation, boolean ignoreDefaultValue) Get theAnnotation
attributesgetAttributes
(Annotation annotation, boolean ignoreDefaultValue, String... ignoreAttributeNames) Get theAnnotation
attributesgetAttributes
(Annotation annotation, org.springframework.core.env.PropertyResolver propertyResolver, boolean classValuesAsString, boolean nestedAnnotationsAsMap, boolean ignoreDefaultValue, String... ignoreAttributeNames) getAttributes
(Annotation annotation, org.springframework.core.env.PropertyResolver propertyResolver, boolean ignoreDefaultValue, String... ignoreAttributeNames) Get theAnnotation
attributesstatic <T> T
getRequiredAttribute
(Map<String, Object> attributes, String attributeName) Get the required attribute valuestatic <A extends Annotation>
booleanIs specifiedAnnotation
present onMethod
's declaring class or parameters or itself.static Annotation
tryGetMergedAnnotation
(AnnotatedElement annotatedElement, Class<? extends Annotation> annotationType) Try to get the mergedannotation
static Annotation
tryGetMergedAnnotation
(AnnotatedElement annotatedElement, Class<? extends Annotation> annotationType, boolean classValuesAsString, boolean nestedAnnotationsAsMap) Try to get the mergedannotation
static org.springframework.core.annotation.AnnotationAttributes
tryGetMergedAnnotationAttributes
(AnnotatedElement annotatedElement, Class<? extends Annotation> annotationType, org.springframework.core.env.PropertyResolver propertyResolver, boolean classValuesAsString, boolean nestedAnnotationsAsMap, boolean ignoreDefaultValue, String... ignoreAttributeNames) Try to getthe annotation attributes
after merging and resolving the placeholdersstatic org.springframework.core.annotation.AnnotationAttributes
tryGetMergedAnnotationAttributes
(AnnotatedElement annotatedElement, Class<? extends Annotation> annotationType, org.springframework.core.env.PropertyResolver propertyResolver, boolean ignoreDefaultValue, String... ignoreAttributeNames) Try to getthe annotation attributes
after merging and resolving the placeholders
-
Constructor Details
-
AnnotationUtils
public AnnotationUtils()
-
-
Method Details
-
isPresent
Is specifiedAnnotation
present onMethod
's declaring class or parameters or itself.- Type Parameters:
A
-Annotation
type- Parameters:
method
-Method
annotationClass
-Annotation
type- Returns:
- If present , return
true
, orfalse
-
findAnnotations
@Nonnull public static <A extends Annotation> Map<ElementType,List<A>> findAnnotations(Method method, Class<A> annotationClass) Find specifiedAnnotation
type maps fromMethod
- Type Parameters:
A
-Annotation
type- Parameters:
method
-Method
annotationClass
-Annotation
type- Returns:
Annotation
type maps , theElementType
as key , the list ofAnnotation
as value. IfAnnotation
was annotated onMethod
's parametersElementType.PARAMETER
, the associatedAnnotation
list may contain multiple elements.
-
getAttributes
Get theAnnotation
attributes without default values- Parameters:
annotation
- specifiedAnnotation
- Returns:
- non-null
- See Also:
-
getAttributes
@Nonnull public static Map<String,Object> getAttributes(Annotation annotation, boolean ignoreDefaultValue) Get theAnnotation
attributes- Parameters:
annotation
- specifiedAnnotation
ignoreDefaultValue
- whether ignore default value or not- Returns:
- non-null
- See Also:
-
getAttributes
@Nonnull public static Map<String,Object> getAttributes(Annotation annotation, boolean ignoreDefaultValue, String... ignoreAttributeNames) Get theAnnotation
attributes- Parameters:
annotation
- specifiedAnnotation
ignoreDefaultValue
- whether ignore default value or notignoreAttributeNames
- the attribute names of annotation should be ignored- Returns:
- non-null
- See Also:
-
getAttributes
@Nonnull public static Map<String,Object> getAttributes(Annotation annotation, org.springframework.core.env.PropertyResolver propertyResolver, boolean ignoreDefaultValue, String... ignoreAttributeNames) Get theAnnotation
attributes- Parameters:
annotation
- specifiedAnnotation
propertyResolver
-PropertyResolver
instance, e.gEnvironment
ignoreDefaultValue
- whether ignore default value or notignoreAttributeNames
- the attribute names of annotation should be ignored- Returns:
- non-null
- See Also:
-
getAttributes
@Nonnull public static Map<String,Object> getAttributes(Annotation annotation, org.springframework.core.env.PropertyResolver propertyResolver, boolean classValuesAsString, boolean nestedAnnotationsAsMap, boolean ignoreDefaultValue, String... ignoreAttributeNames) - Parameters:
annotation
- specifiedAnnotation
propertyResolver
-PropertyResolver
instance, e.gEnvironment
classValuesAsString
- whether to turn Class references into Strings (for compatibility withAnnotationMetadata
or to preserve them as Class referencesnestedAnnotationsAsMap
- whether to turn nested Annotation instances intoAnnotationAttributes
maps (for compatibility withAnnotationMetadata
or to preserve them as Annotation instancesignoreDefaultValue
- whether ignore default value or notignoreAttributeNames
- the attribute names of annotation should be ignored- Returns:
- non-null
- See Also:
-
getAttribute
Get the attribute value- Type Parameters:
T
- the type of attribute value- Parameters:
annotation
-annotation
attributeName
- the name of attribute- Returns:
- the attribute value if found
-
getAttribute
Get the attribute value- Type Parameters:
T
- the type of attribute value- Parameters:
attributes
-the annotation attributes
orAnnotationAttributes
attributeName
- the name of attribute- Returns:
- the attribute value if found
-
getAttribute
@Nullable public static <T> T getAttribute(Map<String, Object> attributes, String attributeName, boolean required) Get the attribute value the will- Type Parameters:
T
- the type of attribute value- Parameters:
attributes
-the annotation attributes
orAnnotationAttributes
attributeName
- the name of attributerequired
- the required attribute or not- Returns:
- the attribute value if found
- Throws:
IllegalStateException
- if attribute value can't be found
-
getAttribute
@Nullable public static <T> T getAttribute(Map<String, Object> attributes, String attributeName, T defaultValue) Get the attribute value with default value- Type Parameters:
T
- the type of attribute value- Parameters:
attributes
-the annotation attributes
orAnnotationAttributes
attributeName
- the name of attributedefaultValue
- the default value of attribute- Returns:
- the attribute value if found
-
getRequiredAttribute
@Nonnull public static <T> T getRequiredAttribute(Map<String, Object> attributes, String attributeName) Get the required attribute value- Type Parameters:
T
- the type of attribute value- Parameters:
attributes
-the annotation attributes
orAnnotationAttributes
attributeName
- the name of attribute- Returns:
- the attribute value if found
- Throws:
IllegalStateException
- if attribute value can't be found
-
getAnnotationAttributes
@Nonnull public static org.springframework.core.annotation.AnnotationAttributes getAnnotationAttributes(Annotation annotation) Get theAnnotationAttributes
- Parameters:
annotation
- specifiedAnnotation
- Returns:
- non-null
AnnotationAttributes
- See Also:
-
getAnnotationAttributes
@Nonnull public static org.springframework.core.annotation.AnnotationAttributes getAnnotationAttributes(Annotation annotation, boolean ignoreDefaultValue) Get theAnnotationAttributes
- Parameters:
annotation
- specifiedAnnotation
ignoreDefaultValue
- whether ignore default value or not- Returns:
- non-null
AnnotationAttributes
- See Also:
-
getAnnotationAttributes
@Nonnull public static org.springframework.core.annotation.AnnotationAttributes getAnnotationAttributes(Annotation annotation, boolean ignoreDefaultValue, String... ignoreAttributeNames) Get theAnnotationAttributes
- Parameters:
annotation
- specifiedAnnotation
ignoreDefaultValue
- whether ignore default value or notignoreAttributeNames
- the attribute names of annotation should be ignored- Returns:
- non-null
AnnotationAttributes
- See Also:
-
getAnnotationAttributes
@Nonnull public static org.springframework.core.annotation.AnnotationAttributes getAnnotationAttributes(Annotation annotation, org.springframework.core.env.PropertyResolver propertyResolver, boolean ignoreDefaultValue, String... ignoreAttributeNames) Get theAnnotationAttributes
- Parameters:
annotation
- specifiedAnnotation
propertyResolver
-PropertyResolver
instance, e.gEnvironment
ignoreDefaultValue
- whether ignore default value or notignoreAttributeNames
- the attribute names of annotation should be ignored- Returns:
- non-null
AnnotationAttributes
- See Also:
-
getAnnotationAttributes
@Nonnull public static org.springframework.core.annotation.AnnotationAttributes getAnnotationAttributes(Annotation annotation, org.springframework.core.env.PropertyResolver propertyResolver, boolean classValuesAsString, boolean nestedAnnotationsAsMap, boolean ignoreDefaultValue, String... ignoreAttributeNames) Get theAnnotationAttributes
- Parameters:
annotation
- specifiedAnnotation
propertyResolver
-PropertyResolver
instance, e.gEnvironment
classValuesAsString
- whether to turn Class references into Strings (for compatibility withAnnotationMetadata
or to preserve them as Class referencesnestedAnnotationsAsMap
- whether to turn nested Annotation instances intoAnnotationAttributes
maps (for compatibility withAnnotationMetadata
or to preserve them as Annotation instancesignoreAttributeNames
- the attribute names of annotation should be ignoredignoreDefaultValue
- whether ignore default value or not- Returns:
- non-null
- See Also:
-
getAnnotationAttributes
@Nullable public static org.springframework.core.annotation.AnnotationAttributes getAnnotationAttributes(AnnotatedElement annotatedElement, Class<? extends Annotation> annotationType, org.springframework.core.env.PropertyResolver propertyResolver, boolean ignoreDefaultValue, String... ignoreAttributeNames) Get theAnnotationAttributes
- Parameters:
annotatedElement
-the annotated element
annotationType
- thetyoe
pfannotation
propertyResolver
-PropertyResolver
instance, e.gEnvironment
ignoreDefaultValue
- whether ignore default value or notignoreAttributeNames
- the attribute names of annotation should be ignored- Returns:
- if
annotatedElement
can't be found inannotatedElement
, returnnull
-
getAnnotationAttributes
@Nullable public static org.springframework.core.annotation.AnnotationAttributes getAnnotationAttributes(AnnotatedElement annotatedElement, Class<? extends Annotation> annotationType, org.springframework.core.env.PropertyResolver propertyResolver, boolean classValuesAsString, boolean nestedAnnotationsAsMap, boolean ignoreDefaultValue, String... ignoreAttributeNames) Get theAnnotationAttributes
- Parameters:
annotatedElement
-the annotated element
annotationType
- thetyoe
pfannotation
propertyResolver
-PropertyResolver
instance, e.gEnvironment
ignoreDefaultValue
- whether ignore default value or notignoreAttributeNames
- the attribute names of annotation should be ignored- Returns:
- if
annotatedElement
can't be found inannotatedElement
, returnnull
-
getAnnotationAttributes
@Nullable public static org.springframework.core.annotation.AnnotationAttributes getAnnotationAttributes(AnnotatedElement annotatedElement, Class<? extends Annotation> annotationType, org.springframework.core.env.PropertyResolver propertyResolver, boolean ignoreDefaultValue, boolean tryMergedAnnotation, String... ignoreAttributeNames) Get theAnnotationAttributes
, if the argumenttryMergedAnnotation
istrue
, theAnnotationAttributes
will be got frommerged annotation
first, if failed, and then to get fromnormal one
- Parameters:
annotatedElement
-the annotated element
annotationType
- thetyoe
pfannotation
propertyResolver
-PropertyResolver
instance, e.gEnvironment
ignoreDefaultValue
- whether ignore default value or nottryMergedAnnotation
- whether try merged annotation or notignoreAttributeNames
- the attribute names of annotation should be ignored- Returns:
- if
annotatedElement
can't be found inannotatedElement
, returnnull
-
getAnnotationAttributes
public static org.springframework.core.annotation.AnnotationAttributes getAnnotationAttributes(AnnotatedElement annotatedElement, Class<? extends Annotation> annotationType, org.springframework.core.env.PropertyResolver propertyResolver, boolean classValuesAsString, boolean nestedAnnotationsAsMap, boolean ignoreDefaultValue, boolean tryMergedAnnotation, String... ignoreAttributeNames) Get theAnnotationAttributes
, if the argumenttryMergedAnnotation
istrue
, theAnnotationAttributes
will be got frommerged annotation
first, if failed, and then to get fromnormal one
- Parameters:
annotatedElement
-the annotated element
annotationType
- thetyoe
pfannotation
propertyResolver
-PropertyResolver
instance, e.gEnvironment
classValuesAsString
- whether to turn Class references into Strings (for compatibility withAnnotationMetadata
or to preserve them as Class referencesnestedAnnotationsAsMap
- whether to turn nested Annotation instances intoAnnotationAttributes
maps (for compatibility withAnnotationMetadata
or to preserve them as Annotation instancesignoreDefaultValue
- whether ignore default value or nottryMergedAnnotation
- whether try merged annotation or notignoreAttributeNames
- the attribute names of annotation should be ignored- Returns:
- if
annotatedElement
can't be found inannotatedElement
, returnnull
-
getAnnotationAttributes
public static org.springframework.core.annotation.AnnotationAttributes getAnnotationAttributes(org.springframework.core.type.AnnotationMetadata metadata, Class<? extends Annotation> annotationType) Get an instance ofAnnotationAttributes
fromAnnotationMetadata
with the specified class name ofannotation
.- Parameters:
metadata
-AnnotationMetadata
annotationType
- Theclass
ofannotation
- Returns:
- non-null
-
getAnnotationAttributes
public static org.springframework.core.annotation.AnnotationAttributes getAnnotationAttributes(org.springframework.core.type.AnnotationMetadata metadata, String annotationClassName) Get an instance ofAnnotationAttributes
fromAnnotationMetadata
with the specified class name ofannotation
.- Parameters:
metadata
-AnnotationMetadata
annotationClassName
- The class name ofannotation
- Returns:
- non-null
-
tryGetMergedAnnotation
@Nullable public static Annotation tryGetMergedAnnotation(AnnotatedElement annotatedElement, Class<? extends Annotation> annotationType) Try to get the mergedannotation
- Parameters:
annotatedElement
-the annotated element
annotationType
- thetyoe
pfannotation
- Returns:
null
If not found
-
tryGetMergedAnnotation
@Nullable public static Annotation tryGetMergedAnnotation(AnnotatedElement annotatedElement, Class<? extends Annotation> annotationType, boolean classValuesAsString, boolean nestedAnnotationsAsMap) Try to get the mergedannotation
- Parameters:
annotatedElement
-the annotated element
annotationType
- thetyoe
pfannotation
classValuesAsString
- whether to turn Class references into Strings (for compatibility withAnnotationMetadata
or to preserve them as Class referencesnestedAnnotationsAsMap
- whether to turn nested Annotation instances intoAnnotationAttributes
maps (for compatibility withAnnotationMetadata
or to preserve them as Annotation instances- Returns:
null
If not found
-
tryGetMergedAnnotationAttributes
public static org.springframework.core.annotation.AnnotationAttributes tryGetMergedAnnotationAttributes(AnnotatedElement annotatedElement, Class<? extends Annotation> annotationType, org.springframework.core.env.PropertyResolver propertyResolver, boolean ignoreDefaultValue, String... ignoreAttributeNames) Try to getthe annotation attributes
after merging and resolving the placeholders- Parameters:
annotatedElement
-the annotated element
annotationType
- thetyoe
pfannotation
propertyResolver
-PropertyResolver
instance, e.gEnvironment
ignoreDefaultValue
- whether ignore default value or notignoreAttributeNames
- the attribute names of annotation should be ignored- Returns:
- If the specified annotation type is not found, return
null
-
tryGetMergedAnnotationAttributes
@Nullable public static org.springframework.core.annotation.AnnotationAttributes tryGetMergedAnnotationAttributes(AnnotatedElement annotatedElement, Class<? extends Annotation> annotationType, org.springframework.core.env.PropertyResolver propertyResolver, boolean classValuesAsString, boolean nestedAnnotationsAsMap, boolean ignoreDefaultValue, String... ignoreAttributeNames) Try to getthe annotation attributes
after merging and resolving the placeholders- Parameters:
annotatedElement
-the annotated element
annotationType
- thetyoe
pfannotation
propertyResolver
-PropertyResolver
instance, e.gEnvironment
classValuesAsString
- whether to turn Class references into Strings (for compatibility withAnnotationMetadata
or to preserve them as Class referencesnestedAnnotationsAsMap
- whether to turn nested Annotation instances intoAnnotationAttributes
maps (for compatibility withAnnotationMetadata
or to preserve them as Annotation instancesignoreDefaultValue
- whether ignore default value or notignoreAttributeNames
- the attribute names of annotation should be ignored- Returns:
- If the specified annotation type is not found, return
null
-
findAnnotationType
@Nullable public static <A extends Annotation> Class<A> findAnnotationType(org.springframework.core.annotation.AnnotationAttributes annotationAttributes) - Type Parameters:
A
- Theclass
ofAnnotation
- Parameters:
annotationAttributes
-AnnotationAttributes
- Returns:
null
if not found
-