Interface AnnotationUtils


public interface AnnotationUtils
Commons Annotation Utilities class
Since:
2.7.6
  • Method Details

    • resolveAnnotationType

      static <A extends Annotation> Class<A> resolveAnnotationType(AnnotatedElement annotatedElement, String annotationClassName)
      Resolve the annotation type by the annotated element and resolved class name
      Type Parameters:
      A - the type of annotation
      Parameters:
      annotatedElement - the annotated element
      annotationClassName - the class name of annotation
      Returns:
      If resolved, return the type of annotation, or null
    • isType

      static boolean isType(AnnotatedElement annotatedElement)
      Is the specified type a generic type
      Parameters:
      annotatedElement - the annotated element
      Returns:
      if annotatedElement is the Class, return true, or false
      See Also:
    • isSameType

      static boolean isSameType(Annotation annotation, Class<? extends Annotation> annotationType)
      Is the type of specified annotation same to the expected type?
      Parameters:
      annotation - the specified Annotation
      annotationType - the expected annotation type
      Returns:
      if same, return true, or false
    • excludedType

      static Predicate<Annotation> excludedType(Class<? extends Annotation> excludedAnnotationType)
      Build an instance of Predicate to excluded annotation type
      Parameters:
      excludedAnnotationType - excluded annotation type
      Returns:
      non-null
    • getAttribute

      static <T> T getAttribute(Annotation annotation, String attributeName) throws IllegalArgumentException
      Get the attribute from the specified annotation
      Type Parameters:
      T - the type of attribute
      Parameters:
      annotation - the specified annotation
      attributeName - the attribute name
      Returns:
      the attribute value
      Throws:
      IllegalArgumentException - If the attribute name can't be found
    • getValue

      static <T> T getValue(Annotation annotation) throws IllegalArgumentException
      Get the "value" attribute from the specified annotation
      Type Parameters:
      T - the type of attribute
      Parameters:
      annotation - the specified annotation
      Returns:
      the value of "value" attribute
      Throws:
      IllegalArgumentException - If the attribute name can't be found
    • getAttribute

      static <T> T getAttribute(Annotation annotation, String... attributeNames) throws IllegalArgumentException
      Get the attribute from the specified annotation
      Type Parameters:
      T - the type of attribute
      Parameters:
      annotation - the specified annotation
      attributeNames - the multiply attribute name arrays
      Returns:
      the attribute value
      Throws:
      IllegalArgumentException - If the attribute name can't be found
    • getAnnotation

      static <A extends Annotation> A getAnnotation(AnnotatedElement annotatedElement, String annotationClassName) throws ClassCastException
      Get the Annotation from the specified the annotated element and annotation class name
      Type Parameters:
      A - The type of Annotation
      Parameters:
      annotatedElement - AnnotatedElement
      annotationClassName - the class name of annotation
      Returns:
      the Annotation if found
      Throws:
      ClassCastException - If the annotation type that client requires can't match actual type
    • getDeclaredAnnotations

      static List<Annotation> getDeclaredAnnotations(AnnotatedElement annotatedElement, Predicate<Annotation>... annotationsToFilter)
      Get annotations that are directly present on this element. This method ignores inherited annotations.
      Parameters:
      annotatedElement - the annotated element
      annotationsToFilter - the annotations to filter
      Returns:
      non-null read-only List
    • getAllDeclaredAnnotations

      static List<Annotation> getAllDeclaredAnnotations(AnnotatedElement annotatedElement, Predicate<Annotation>... annotationsToFilter)
      Get all directly declared annotations of the the annotated element, not including meta annotations.
      Parameters:
      annotatedElement - the annotated element
      annotationsToFilter - the annotations to filter
      Returns:
      non-null read-only List
    • getAllDeclaredAnnotations

      static List<Annotation> getAllDeclaredAnnotations(Class<?> type, Predicate<Annotation>... annotationsToFilter)
      Get all directly declared annotations of the specified type and its' all hierarchical types, not including meta annotations.
      Parameters:
      type - the specified type
      annotationsToFilter - the annotations to filter
      Returns:
      non-null read-only List
    • getMetaAnnotations

      static List<Annotation> getMetaAnnotations(Class<? extends Annotation> annotationType, Predicate<Annotation>... metaAnnotationsToFilter)
      Get the meta-annotated annotations directly, excluding Target, Retention and Documented
      Parameters:
      annotationType - the annotation type
      metaAnnotationsToFilter - the meta annotations to filter
      Returns:
      non-null read-only List
    • getAllMetaAnnotations

      static List<Annotation> getAllMetaAnnotations(Class<? extends Annotation> annotationType, Predicate<Annotation>... annotationsToFilter)
      Get all meta annotations from the specified annotation type
      Parameters:
      annotationType - the annotation type
      annotationsToFilter - the annotations to filter
      Returns:
      non-null read-only List
    • findAnnotation

      static <A extends Annotation> A findAnnotation(AnnotatedElement annotatedElement, String annotationClassName)
      Find the annotation that is annotated on the specified element may be a meta-annotation
      Type Parameters:
      A - the required type of annotation
      Parameters:
      annotatedElement - the annotated element
      annotationClassName - the class name of annotation
      Returns:
      If found, return first matched-type annotation, or null
    • findAnnotation

      static <A extends Annotation> A findAnnotation(AnnotatedElement annotatedElement, Class<A> annotationType)
      Find the annotation that is annotated on the specified element may be a meta-annotation
      Type Parameters:
      A - the required type of annotation
      Parameters:
      annotatedElement - the annotated element
      annotationType - the type of annotation
      Returns:
      If found, return first matched-type annotation, or null
    • findMetaAnnotations

      static <A extends Annotation> List<A> findMetaAnnotations(Class<? extends Annotation> annotationType, Class<A> metaAnnotationType)
      Find the meta annotations from the the annotation type by meta annotation type
      Type Parameters:
      A - the type of required annotation
      Parameters:
      annotationType - the annotation type
      metaAnnotationType - the meta annotation type
      Returns:
      if found, return all matched results, or get an empty list
    • findMetaAnnotations

      static <A extends Annotation> List<A> findMetaAnnotations(AnnotatedElement annotatedElement, Class<A> metaAnnotationType)
      Find the meta annotations from the the the annotated element by meta annotation type
      Type Parameters:
      A - the type of required annotation
      Parameters:
      annotatedElement - the annotated element
      metaAnnotationType - the meta annotation type
      Returns:
      if found, return all matched results, or get an empty list
    • findMetaAnnotation

      static <A extends Annotation> A findMetaAnnotation(AnnotatedElement annotatedElement, String metaAnnotationClassName)
      Find the meta annotation from the annotated element by meta annotation type
      Type Parameters:
      A - the type of required annotation
      Parameters:
      annotatedElement - the annotated element
      metaAnnotationClassName - the class name of meta annotation
      Returns:
      findMetaAnnotation(Class, Class)
    • findMetaAnnotation

      static <A extends Annotation> A findMetaAnnotation(Class<? extends Annotation> annotationType, Class<A> metaAnnotationType)
      Find the meta annotation from the annotation type by meta annotation type
      Type Parameters:
      A - the type of required annotation
      Parameters:
      annotationType - the annotation type
      metaAnnotationType - the meta annotation type
      Returns:
      If found, return the CollectionUtils.first(Collection) matched result, return null. If it requires more result, please consider to use findMetaAnnotations(Class, Class)
      See Also:
    • findMetaAnnotation

      static <A extends Annotation> A findMetaAnnotation(AnnotatedElement annotatedElement, Class<A> metaAnnotationType)
      Find the meta annotation from the annotated element by meta annotation type
      Type Parameters:
      A - the type of required annotation
      Parameters:
      annotatedElement - the annotated element
      metaAnnotationType - the meta annotation type
      Returns:
      If found, return the CollectionUtils.first(Collection) matched result, return null. If it requires more result, please consider to use findMetaAnnotations(AnnotatedElement, Class)
      See Also:
    • isAnnotationPresent

      static boolean isAnnotationPresent(Class<?> type, boolean matchAll, Class<? extends Annotation>... annotationTypes)
      Tests the annotated element is annotated the specified annotations or not
      Parameters:
      type - the annotated type
      matchAll - If true, checking all annotation types are present or not, or match any
      annotationTypes - the specified annotation types
      Returns:
      If the specified annotation types are present, return true, or false
    • isAnnotationPresent

      static boolean isAnnotationPresent(Class<?> type, Class<? extends Annotation> annotationType)
      Tests the annotated element is annotated the specified annotation or not
      Parameters:
      type - the annotated type
      annotationType - the class of annotation
      Returns:
      If the specified annotation type is present, return true, or false
    • isAnnotationPresent

      static boolean isAnnotationPresent(AnnotatedElement annotatedElement, String annotationClassName)
      Tests the annotated element is present any specified annotation types
      Parameters:
      annotatedElement - the annotated element
      annotationClassName - the class name of annotation
      Returns:
      If any specified annotation types are present, return true
    • isAnnotationPresent

      static boolean isAnnotationPresent(AnnotatedElement annotatedElement, Class<? extends Annotation> annotationType)
      Tests the annotated element is present any specified annotation types
      Parameters:
      annotatedElement - the annotated element
      annotationType - the class of annotation
      Returns:
      If any specified annotation types are present, return true
    • isAllAnnotationPresent

      static boolean isAllAnnotationPresent(Class<?> type, Class<? extends Annotation>... annotationTypes)
      Tests the annotated element is annotated all specified annotations or not
      Parameters:
      type - the annotated type
      annotationTypes - the specified annotation types
      Returns:
      If the specified annotation types are present, return true, or false
    • isAnyAnnotationPresent

      static boolean isAnyAnnotationPresent(Class<?> type, Class<? extends Annotation>... annotationTypes)
      Tests the annotated element is present any specified annotation types
      Parameters:
      type - the annotated type
      annotationTypes - the specified annotation types
      Returns:
      If any specified annotation types are present, return true
    • getDefaultValue

      static <T> T getDefaultValue(Annotation annotation, String attributeName)
      Get the default value of attribute on the specified annotation
      Type Parameters:
      T - the type of value
      Parameters:
      annotation - Annotation object
      attributeName - the name of attribute
      Returns:
      null if not found
      Since:
      2.7.9
    • getDefaultValue

      static <T> T getDefaultValue(Class<? extends Annotation> annotationType, String attributeName)
      Get the default value of attribute on the specified annotation
      Type Parameters:
      T - the type of value
      Parameters:
      annotationType - the type of Annotation
      attributeName - the name of attribute
      Returns:
      null if not found
      Since:
      2.7.9
    • filterDefaultValues

      static Map<String,Object> filterDefaultValues(Class<? extends Annotation> annotationType, Map<String,Object> attributes)
      Filter default value of Annotation type
      Parameters:
      annotationType - annotation type from Annotation.annotationType()
      attributes -
      Returns:
    • filterDefaultValues

      static Map<String,Object> filterDefaultValues(Annotation annotation, Map<String,Object> attributes)
      Filter default value of Annotation type
      Parameters:
      annotation -
      attributes -
      Returns:
    • getAttributes

      static Map<String,Object> getAttributes(Annotation annotation, boolean filterDefaultValue)
      Get attributes of annotation
      Parameters:
      annotation -
      Returns: