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 specifiedAnnotationtype maps fromMethodstatic <A extends Annotation>
Class<A>findAnnotationType(org.springframework.core.annotation.AnnotationAttributes annotationAttributes) static org.springframework.core.annotation.AnnotationAttributesgetAnnotationAttributes(Annotation annotation) Get theAnnotationAttributesstatic org.springframework.core.annotation.AnnotationAttributesgetAnnotationAttributes(Annotation annotation, boolean ignoreDefaultValue) Get theAnnotationAttributesstatic org.springframework.core.annotation.AnnotationAttributesgetAnnotationAttributes(Annotation annotation, boolean ignoreDefaultValue, String... ignoreAttributeNames) Get theAnnotationAttributesstatic org.springframework.core.annotation.AnnotationAttributesgetAnnotationAttributes(Annotation annotation, org.springframework.core.env.PropertyResolver propertyResolver, boolean classValuesAsString, boolean nestedAnnotationsAsMap, boolean ignoreDefaultValue, String... ignoreAttributeNames) Get theAnnotationAttributesstatic org.springframework.core.annotation.AnnotationAttributesgetAnnotationAttributes(Annotation annotation, org.springframework.core.env.PropertyResolver propertyResolver, boolean ignoreDefaultValue, String... ignoreAttributeNames) Get theAnnotationAttributesstatic org.springframework.core.annotation.AnnotationAttributesgetAnnotationAttributes(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 argumenttryMergedAnnotationistrue, theAnnotationAttributeswill be got frommerged annotationfirst, if failed, and then to get fromnormal onestatic org.springframework.core.annotation.AnnotationAttributesgetAnnotationAttributes(AnnotatedElement annotatedElement, Class<? extends Annotation> annotationType, org.springframework.core.env.PropertyResolver propertyResolver, boolean classValuesAsString, boolean nestedAnnotationsAsMap, boolean ignoreDefaultValue, String... ignoreAttributeNames) Get theAnnotationAttributesstatic org.springframework.core.annotation.AnnotationAttributesgetAnnotationAttributes(AnnotatedElement annotatedElement, Class<? extends Annotation> annotationType, org.springframework.core.env.PropertyResolver propertyResolver, boolean ignoreDefaultValue, boolean tryMergedAnnotation, String... ignoreAttributeNames) Get theAnnotationAttributes, if the argumenttryMergedAnnotationistrue, theAnnotationAttributeswill be got frommerged annotationfirst, if failed, and then to get fromnormal onestatic org.springframework.core.annotation.AnnotationAttributesgetAnnotationAttributes(AnnotatedElement annotatedElement, Class<? extends Annotation> annotationType, org.springframework.core.env.PropertyResolver propertyResolver, boolean ignoreDefaultValue, String... ignoreAttributeNames) Get theAnnotationAttributesstatic org.springframework.core.annotation.AnnotationAttributesgetAnnotationAttributes(org.springframework.core.type.AnnotationMetadata metadata, Class<? extends Annotation> annotationType) Get an instance ofAnnotationAttributesfromAnnotationMetadatawith the specified class name ofannotation.static org.springframework.core.annotation.AnnotationAttributesgetAnnotationAttributes(org.springframework.core.type.AnnotationMetadata metadata, String annotationClassName) Get an instance ofAnnotationAttributesfromAnnotationMetadatawith the specified class name ofannotation.static <T> TgetAttribute(Annotation annotation, String attributeName) Get the attribute valuestatic <T> TgetAttribute(Map<String, Object> attributes, String attributeName) Get the attribute valuestatic <T> TgetAttribute(Map<String, Object> attributes, String attributeName, boolean required) Get the attribute value the willstatic <T> TgetAttribute(Map<String, Object> attributes, String attributeName, T defaultValue) Get the attribute value with default valuegetAttributes(Annotation annotation) Get theAnnotationattributes without default valuesgetAttributes(Annotation annotation, boolean ignoreDefaultValue) Get theAnnotationattributesgetAttributes(Annotation annotation, boolean ignoreDefaultValue, String... ignoreAttributeNames) Get theAnnotationattributesgetAttributes(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 theAnnotationattributesstatic <T> TgetRequiredAttribute(Map<String, Object> attributes, String attributeName) Get the required attribute valuestatic <A extends Annotation>
booleanIs specifiedAnnotationpresent onMethod's declaring class or parameters or itself.static AnnotationtryGetMergedAnnotation(AnnotatedElement annotatedElement, Class<? extends Annotation> annotationType) Try to get the mergedannotationstatic AnnotationtryGetMergedAnnotation(AnnotatedElement annotatedElement, Class<? extends Annotation> annotationType, boolean classValuesAsString, boolean nestedAnnotationsAsMap) Try to get the mergedannotationstatic org.springframework.core.annotation.AnnotationAttributestryGetMergedAnnotationAttributes(AnnotatedElement annotatedElement, Class<? extends Annotation> annotationType, org.springframework.core.env.PropertyResolver propertyResolver, boolean classValuesAsString, boolean nestedAnnotationsAsMap, boolean ignoreDefaultValue, String... ignoreAttributeNames) Try to getthe annotation attributesafter merging and resolving the placeholdersstatic org.springframework.core.annotation.AnnotationAttributestryGetMergedAnnotationAttributes(AnnotatedElement annotatedElement, Class<? extends Annotation> annotationType, org.springframework.core.env.PropertyResolver propertyResolver, boolean ignoreDefaultValue, String... ignoreAttributeNames) Try to getthe annotation attributesafter merging and resolving the placeholders
-
Constructor Details
-
AnnotationUtils
public AnnotationUtils()
-
-
Method Details
-
isPresent
Is specifiedAnnotationpresent onMethod's declaring class or parameters or itself.- Type Parameters:
A-Annotationtype- Parameters:
method-MethodannotationClass-Annotationtype- Returns:
- If present , return
true, orfalse
-
findAnnotations
@Nonnull public static <A extends Annotation> Map<ElementType,List<A>> findAnnotations(Method method, Class<A> annotationClass) Find specifiedAnnotationtype maps fromMethod- Type Parameters:
A-Annotationtype- Parameters:
method-MethodannotationClass-Annotationtype- Returns:
Annotationtype maps , theElementTypeas key , the list ofAnnotationas value. IfAnnotationwas annotated onMethod's parametersElementType.PARAMETER, the associatedAnnotationlist may contain multiple elements.
-
getAttributes
Get theAnnotationattributes without default values- Parameters:
annotation- specifiedAnnotation- Returns:
- non-null
- See Also:
-
getAttributes
@Nonnull public static Map<String,Object> getAttributes(Annotation annotation, boolean ignoreDefaultValue) Get theAnnotationattributes- Parameters:
annotation- specifiedAnnotationignoreDefaultValue- 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 theAnnotationattributes- Parameters:
annotation- specifiedAnnotationignoreDefaultValue- 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 theAnnotationattributes- Parameters:
annotation- specifiedAnnotationpropertyResolver-PropertyResolverinstance, e.gEnvironmentignoreDefaultValue- 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- specifiedAnnotationpropertyResolver-PropertyResolverinstance, e.gEnvironmentclassValuesAsString- whether to turn Class references into Strings (for compatibility withAnnotationMetadataor to preserve them as Class referencesnestedAnnotationsAsMap- whether to turn nested Annotation instances intoAnnotationAttributesmaps (for compatibility withAnnotationMetadataor 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-annotationattributeName- 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 attributesorAnnotationAttributesattributeName- 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 attributesorAnnotationAttributesattributeName- 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 attributesorAnnotationAttributesattributeName- 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 attributesorAnnotationAttributesattributeName- 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- specifiedAnnotationignoreDefaultValue- 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- specifiedAnnotationignoreDefaultValue- 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- specifiedAnnotationpropertyResolver-PropertyResolverinstance, e.gEnvironmentignoreDefaultValue- 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- specifiedAnnotationpropertyResolver-PropertyResolverinstance, e.gEnvironmentclassValuesAsString- whether to turn Class references into Strings (for compatibility withAnnotationMetadataor to preserve them as Class referencesnestedAnnotationsAsMap- whether to turn nested Annotation instances intoAnnotationAttributesmaps (for compatibility withAnnotationMetadataor 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 elementannotationType- thetyoepfannotationpropertyResolver-PropertyResolverinstance, e.gEnvironmentignoreDefaultValue- whether ignore default value or notignoreAttributeNames- the attribute names of annotation should be ignored- Returns:
- if
annotatedElementcan'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 elementannotationType- thetyoepfannotationpropertyResolver-PropertyResolverinstance, e.gEnvironmentignoreDefaultValue- whether ignore default value or notignoreAttributeNames- the attribute names of annotation should be ignored- Returns:
- if
annotatedElementcan'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 argumenttryMergedAnnotationistrue, theAnnotationAttributeswill be got frommerged annotationfirst, if failed, and then to get fromnormal one- Parameters:
annotatedElement-the annotated elementannotationType- thetyoepfannotationpropertyResolver-PropertyResolverinstance, e.gEnvironmentignoreDefaultValue- whether ignore default value or nottryMergedAnnotation- whether try merged annotation or notignoreAttributeNames- the attribute names of annotation should be ignored- Returns:
- if
annotatedElementcan'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 argumenttryMergedAnnotationistrue, theAnnotationAttributeswill be got frommerged annotationfirst, if failed, and then to get fromnormal one- Parameters:
annotatedElement-the annotated elementannotationType- thetyoepfannotationpropertyResolver-PropertyResolverinstance, e.gEnvironmentclassValuesAsString- whether to turn Class references into Strings (for compatibility withAnnotationMetadataor to preserve them as Class referencesnestedAnnotationsAsMap- whether to turn nested Annotation instances intoAnnotationAttributesmaps (for compatibility withAnnotationMetadataor 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
annotatedElementcan'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 ofAnnotationAttributesfromAnnotationMetadatawith the specified class name ofannotation.- Parameters:
metadata-AnnotationMetadataannotationType- Theclassofannotation- Returns:
- non-null
-
getAnnotationAttributes
public static org.springframework.core.annotation.AnnotationAttributes getAnnotationAttributes(org.springframework.core.type.AnnotationMetadata metadata, String annotationClassName) Get an instance ofAnnotationAttributesfromAnnotationMetadatawith the specified class name ofannotation.- Parameters:
metadata-AnnotationMetadataannotationClassName- 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 elementannotationType- thetyoepfannotation- Returns:
nullIf 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 elementannotationType- thetyoepfannotationclassValuesAsString- whether to turn Class references into Strings (for compatibility withAnnotationMetadataor to preserve them as Class referencesnestedAnnotationsAsMap- whether to turn nested Annotation instances intoAnnotationAttributesmaps (for compatibility withAnnotationMetadataor to preserve them as Annotation instances- Returns:
nullIf 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 attributesafter merging and resolving the placeholders- Parameters:
annotatedElement-the annotated elementannotationType- thetyoepfannotationpropertyResolver-PropertyResolverinstance, e.gEnvironmentignoreDefaultValue- 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 attributesafter merging and resolving the placeholders- Parameters:
annotatedElement-the annotated elementannotationType- thetyoepfannotationpropertyResolver-PropertyResolverinstance, e.gEnvironmentclassValuesAsString- whether to turn Class references into Strings (for compatibility withAnnotationMetadataor to preserve them as Class referencesnestedAnnotationsAsMap- whether to turn nested Annotation instances intoAnnotationAttributesmaps (for compatibility withAnnotationMetadataor 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- TheclassofAnnotation- Parameters:
annotationAttributes-AnnotationAttributes- Returns:
nullif not found
-