Class AnnotationUtils

java.lang.Object
org.axonframework.common.annotation.AnnotationUtils

public final class AnnotationUtils extends Object
Utility class for locating annotation and attribute values on elements.
Since:
3.0
Author:
Allard Buijze
  • Field Details

  • Method Details

    • isAnnotationPresent

      public static boolean isAnnotationPresent(AnnotatedElement element, Class<? extends Annotation> annotationType)
      Indicates whether an annotation of given annotationType is present on the given element, considering that the given annotationType may be present as a meta annotation on any other annotation on that element.
      Parameters:
      element - The element to inspect
      annotationType - The type of annotation to find
      Returns:
      true if such annotation is present.
    • isAnnotationPresent

      public static boolean isAnnotationPresent(AnnotatedElement element, String annotationType)
      Indicates whether an annotation with given annotationType is present on the given element, considering that the given annotationType may be present as a meta annotation on any other annotation on that element.
      Parameters:
      element - The element to inspect
      annotationType - The name of the annotation to find
      Returns:
      true if such annotation is present.
    • findAnnotationAttributes

      public static Optional<Map<String,Object>> findAnnotationAttributes(AnnotatedElement element, Class<? extends Annotation> annotationType)
      Find the attributes of an annotation of type annotationType on the given element. The returned optional has a value present if the annotation has been found, either directly on the element, or as a meta-annotation.

      The map of attributes contains all the attributes found on the annotation, as well as attributes of any annotations on which the targeted annotation was placed (directly, or indirectly).

      Note that the value property of annotations is reported as the simple class name (lowercase first character) of the annotation. This allows specific attribute overrides for annotations that have multiple meta-annotation with the value property.

      Parameters:
      element - The element for find the annotation on
      annotationType - The type of the annotation to find
      Returns:
      an optional that resolved to a map with attribute names and value, if the annotation is found
    • findAnnotationAttributes

      public static Optional<Map<String,Object>> findAnnotationAttributes(AnnotatedElement element, Class<? extends Annotation> annotationType, boolean overrideOnly)
      Find the attributes of an annotation of type annotationType on the given element. The returned optional has a value present if the annotation has been found, either directly on the element, or as a meta-annotation.

      The map of attributes contains all the attributes found on the annotation. The overrideOnly parameter defines whether all attributes of any annotation on which the targeted annotation was placed (directly, or indirectly) should be included. For OVERRIDE_ONLY, only attribute overrides will be added on top of that, whereas for ADD_ALL all attributes on any meta-annotated level will be included in the result.

      Note that the value property of annotations is reported as the simple class name (lowercase first character) of the annotation. This allows specific attribute overrides for annotations that have multiple meta-annotation with the value property.

      Parameters:
      element - the element for find the annotation on
      annotationType - the type of the annotation to find
      overrideOnly - boolean defining whether or not to only take attribute overrides from meta-annotations into account for the result or to include all attributes from every level
      Returns:
      an optional that resolved to a map with attribute names and value, if the annotation is found
    • findAnnotationAttributes

      public static Optional<Map<String,Object>> findAnnotationAttributes(AnnotatedElement element, String annotationName)
      Find the attributes of an annotation with given annotationName on the given element. The returned optional has a value present if the annotation has been found, either directly on the element, or as a meta-annotation.

      The map of attributes contains all the attributes found on the annotation, as well as attributes of any annotations on which the targeted annotation was placed (directly, or indirectly).

      Parameters:
      element - The element for find the annotation on
      annotationName - The name of the annotation to find
      Returns:
      an optional that resolved to a map with attribute names and value, if the annotation is found
    • findAnnotationAttributes

      public static Optional<Map<String,Object>> findAnnotationAttributes(AnnotatedElement element, String annotationName, boolean overrideOnly)
      Find the attributes of an annotation with given annotationName on the given element. The returned optional has a value present if the annotation has been found, either directly on the element, or as a meta-annotation.

      The map of attributes contains all the attributes found on the annotation. The overrideOnly parameter defines whether all attributes of any annotation on which the targeted annotation was placed (directly, or indirectly) should be included. For OVERRIDE_ONLY, only attribute overrides will be added on top of that, whereas for ADD_ALL all attributes on any meta-annotated level will be included in the result.

      Note that the value property of annotations is reported as the simple class name (lowercase first character) of the annotation. This allows specific attribute overrides for annotations that have multiple meta-annotation with the value property.

      Parameters:
      element - the element for find the annotation on
      annotationName - the name of the annotation to find
      overrideOnly - boolean defining whether or not to only take attribute overrides from meta-annotations into account for the result or to include all attributes from every level
      Returns:
      an optional that resolved to a map with attribute names and value, if the annotation is found
    • findAnnotationAttribute

      public static <T> Optional<T> findAnnotationAttribute(AnnotatedElement element, Class<? extends Annotation> annotationType, String attributeName)
      Find the attributeName of an annotation of type annotationType on the given element. The returned optional has a value present if the annotation has been found, either directly on the element, or as a meta-annotation, if the named attribute exist.
      Parameters:
      element - the element to find the annotation on
      annotationType - the type of the annotation to find
      attributeName - the name of the attribute to find
      Returns:
      an optional that resolved to the attribute value, if the annotation is found and if the attribute exists
    • isAnnotatedWith

      public static boolean isAnnotatedWith(Class<? extends Annotation> target, Class<? extends Annotation> subject, Set<Class<? extends Annotation>> annotatedWithSubject, Set<Class<? extends Annotation>> visited)
      Validate whether the given target annotation Class is meta-annotated with the given subject. If this is the case for the target itself or any meta-annotation on any level of the target, true will be returned.

      Any Annotation classes which are directly annotated or meta-annotated with the given subject will be stored in the annotatedWithSubject Set. The visited Set is used to ignore annotations which have already been validated.

      Parameters:
      target - the annotation Class to validate if it is annotated with the given subject
      subject - the annotation Class to check whether it is present on the given target, directly or through meta-annotations
      annotatedWithSubject - a Set to store all class' in which are annotated with the subject, either directly or through meta-annotations
      visited - a Set containing all annotation class' which have been visited in the process to overcome an endless validation loop
      Returns:
      true if the target or any meta-annotations of the target are annotated with the subject, false otherwise