Class AnnotationUtils

java.lang.Object
io.microsphere.spring.core.annotation.AnnotationUtils

public abstract class AnnotationUtils extends Object
Annotation Utilities
Since:
1.0.0
Author:
Mercy
See Also:
  • Constructor Details

    • AnnotationUtils

      public AnnotationUtils()
  • Method Details

    • isPresent

      public static <A extends Annotation> boolean isPresent(Method method, Class<A> annotationClass)
      Is specified Annotation present on Method's declaring class or parameters or itself.
      Type Parameters:
      A - Annotation type
      Parameters:
      method - Method
      annotationClass - Annotation type
      Returns:
      If present , return true , or false
    • findAnnotations

      @Nonnull public static <A extends Annotation> Map<ElementType,List<A>> findAnnotations(Method method, Class<A> annotationClass)
      Find specified Annotation type maps from Method
      Type Parameters:
      A - Annotation type
      Parameters:
      method - Method
      annotationClass - Annotation type
      Returns:
      Annotation type maps , the ElementType as key , the list of Annotation as value. If Annotation was annotated on Method's parametersElementType.PARAMETER , the associated Annotation list may contain multiple elements.
    • getAttributes

      @Nonnull public static Map<String,Object> getAttributes(Annotation annotation)
      Get the Annotation attributes without default values
      Parameters:
      annotation - specified Annotation
      Returns:
      non-null
      See Also:
    • getAttributes

      @Nonnull public static Map<String,Object> getAttributes(Annotation annotation, boolean ignoreDefaultValue)
      Get the Annotation attributes
      Parameters:
      annotation - specified Annotation
      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 the Annotation attributes
      Parameters:
      annotation - specified Annotation
      ignoreDefaultValue - whether ignore default value or not
      ignoreAttributeNames - 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 the Annotation attributes
      Parameters:
      annotation - specified Annotation
      propertyResolver - PropertyResolver instance, e.g Environment
      ignoreDefaultValue - whether ignore default value or not
      ignoreAttributeNames - 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 - specified Annotation
      propertyResolver - PropertyResolver instance, e.g Environment
      classValuesAsString - whether to turn Class references into Strings (for compatibility with AnnotationMetadata or to preserve them as Class references
      nestedAnnotationsAsMap - whether to turn nested Annotation instances into AnnotationAttributes maps (for compatibility with AnnotationMetadata or to preserve them as Annotation instances
      ignoreDefaultValue - whether ignore default value or not
      ignoreAttributeNames - the attribute names of annotation should be ignored
      Returns:
      non-null
      See Also:
    • getAttribute

      @Nullable public static <T> T getAttribute(Annotation annotation, String attributeName)
      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

      @Nullable public static <T> T getAttribute(Map<String,Object> attributes, String attributeName)
      Get the attribute value
      Type Parameters:
      T - the type of attribute value
      Parameters:
      attributes - the annotation attributes or AnnotationAttributes
      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 or AnnotationAttributes
      attributeName - the name of attribute
      required - 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 or AnnotationAttributes
      attributeName - the name of attribute
      defaultValue - 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 or AnnotationAttributes
      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 the AnnotationAttributes
      Parameters:
      annotation - specified Annotation
      Returns:
      non-null AnnotationAttributes
      See Also:
    • getAnnotationAttributes

      @Nonnull public static org.springframework.core.annotation.AnnotationAttributes getAnnotationAttributes(Annotation annotation, boolean ignoreDefaultValue)
      Get the AnnotationAttributes
      Parameters:
      annotation - specified Annotation
      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 the AnnotationAttributes
      Parameters:
      annotation - specified Annotation
      ignoreDefaultValue - whether ignore default value or not
      ignoreAttributeNames - 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 the AnnotationAttributes
      Parameters:
      annotation - specified Annotation
      propertyResolver - PropertyResolver instance, e.g Environment
      ignoreDefaultValue - whether ignore default value or not
      ignoreAttributeNames - 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 the AnnotationAttributes
      Parameters:
      annotation - specified Annotation
      propertyResolver - PropertyResolver instance, e.g Environment
      classValuesAsString - whether to turn Class references into Strings (for compatibility with AnnotationMetadata or to preserve them as Class references
      nestedAnnotationsAsMap - whether to turn nested Annotation instances into AnnotationAttributes maps (for compatibility with AnnotationMetadata or to preserve them as Annotation instances
      ignoreAttributeNames - the attribute names of annotation should be ignored
      ignoreDefaultValue - 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 the AnnotationAttributes
      Parameters:
      annotatedElement - the annotated element
      annotationType - the tyoe pf annotation
      propertyResolver - PropertyResolver instance, e.g Environment
      ignoreDefaultValue - whether ignore default value or not
      ignoreAttributeNames - the attribute names of annotation should be ignored
      Returns:
      if annotatedElement can't be found in annotatedElement, return null
    • 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 the AnnotationAttributes
      Parameters:
      annotatedElement - the annotated element
      annotationType - the tyoe pf annotation
      propertyResolver - PropertyResolver instance, e.g Environment
      ignoreDefaultValue - whether ignore default value or not
      ignoreAttributeNames - the attribute names of annotation should be ignored
      Returns:
      if annotatedElement can't be found in annotatedElement, return null
    • 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 the AnnotationAttributes, if the argument tryMergedAnnotation is true, the AnnotationAttributes will be got from merged annotation first, if failed, and then to get from normal one
      Parameters:
      annotatedElement - the annotated element
      annotationType - the tyoe pf annotation
      propertyResolver - PropertyResolver instance, e.g Environment
      ignoreDefaultValue - whether ignore default value or not
      tryMergedAnnotation - whether try merged annotation or not
      ignoreAttributeNames - the attribute names of annotation should be ignored
      Returns:
      if annotatedElement can't be found in annotatedElement, return null
    • 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 the AnnotationAttributes, if the argument tryMergedAnnotation is true, the AnnotationAttributes will be got from merged annotation first, if failed, and then to get from normal one
      Parameters:
      annotatedElement - the annotated element
      annotationType - the tyoe pf annotation
      propertyResolver - PropertyResolver instance, e.g Environment
      classValuesAsString - whether to turn Class references into Strings (for compatibility with AnnotationMetadata or to preserve them as Class references
      nestedAnnotationsAsMap - whether to turn nested Annotation instances into AnnotationAttributes maps (for compatibility with AnnotationMetadata or to preserve them as Annotation instances
      ignoreDefaultValue - whether ignore default value or not
      tryMergedAnnotation - whether try merged annotation or not
      ignoreAttributeNames - the attribute names of annotation should be ignored
      Returns:
      if annotatedElement can't be found in annotatedElement, return null
    • getAnnotationAttributes

      public static org.springframework.core.annotation.AnnotationAttributes getAnnotationAttributes(org.springframework.core.type.AnnotationMetadata metadata, Class<? extends Annotation> annotationType)
      Get an instance of AnnotationAttributes from AnnotationMetadata with the specified class name of annotation.
      Parameters:
      metadata - AnnotationMetadata
      annotationType - The class of annotation
      Returns:
      non-null
    • getAnnotationAttributes

      public static org.springframework.core.annotation.AnnotationAttributes getAnnotationAttributes(org.springframework.core.type.AnnotationMetadata metadata, String annotationClassName)
      Get an instance of AnnotationAttributes from AnnotationMetadata with the specified class name of annotation.
      Parameters:
      metadata - AnnotationMetadata
      annotationClassName - The class name of annotation
      Returns:
      non-null
    • tryGetMergedAnnotation

      @Nullable public static Annotation tryGetMergedAnnotation(AnnotatedElement annotatedElement, Class<? extends Annotation> annotationType)
      Try to get the merged annotation
      Parameters:
      annotatedElement - the annotated element
      annotationType - the tyoe pf annotation
      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 merged annotation
      Parameters:
      annotatedElement - the annotated element
      annotationType - the tyoe pf annotation
      classValuesAsString - whether to turn Class references into Strings (for compatibility with AnnotationMetadata or to preserve them as Class references
      nestedAnnotationsAsMap - whether to turn nested Annotation instances into AnnotationAttributes maps (for compatibility with AnnotationMetadata 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 get the annotation attributes after merging and resolving the placeholders
      Parameters:
      annotatedElement - the annotated element
      annotationType - the tyoe pf annotation
      propertyResolver - PropertyResolver instance, e.g Environment
      ignoreDefaultValue - whether ignore default value or not
      ignoreAttributeNames - 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 get the annotation attributes after merging and resolving the placeholders
      Parameters:
      annotatedElement - the annotated element
      annotationType - the tyoe pf annotation
      propertyResolver - PropertyResolver instance, e.g Environment
      classValuesAsString - whether to turn Class references into Strings (for compatibility with AnnotationMetadata or to preserve them as Class references
      nestedAnnotationsAsMap - whether to turn nested Annotation instances into AnnotationAttributes maps (for compatibility with AnnotationMetadata or to preserve them as Annotation instances
      ignoreDefaultValue - whether ignore default value or not
      ignoreAttributeNames - 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)
      Find the class of Annotation from the specified AnnotationAttributes
      Type Parameters:
      A - The class of Annotation
      Parameters:
      annotationAttributes - AnnotationAttributes
      Returns:
      null if not found