Class AnnotationHelper

java.lang.Object
com.blackbuild.klum.ast.runtime.internal.AnnotationHelper

public class AnnotationHelper extends Object
Helper class for annotations. Provides various functions concerning the discovery and handling of annotations.
  • Method Details

    • getMostSpecificAnnotation

      public static <T extends Annotation> Optional<T> getMostSpecificAnnotation(AnnotatedElement target, Class<T> annotationType)
      Tries to retrieve the given Annotation from the given target. Checks in the following order:
      1. On the target itself
      2. If the target is a member on the owning class of the target
      3. for each DSL class in hierarchy, check the class and the classes package

      The first found instance of the annotation is returned. If no annotation is found, null is returned.

      Parameters:
      target - the target to retrieve the annotation from
      annotationType - the annotation type to retrieve
      Returns:
      the annotation or Optional.empty() if no annotation is found
    • getMostSpecificAnnotation

      public static <T extends Annotation> Optional<T> getMostSpecificAnnotation(AnnotatedElement target, Class<T> annotationType, Predicate<T> filter)
      Tries to retrieve the given Annotation from the given target. Checks in the following order:
      1. On the target itself
      2. If the target is a member on the owning class of the target
      3. for each DSL class in hierarchy, check the class and the classes package
      The first found instance of the annotation that matches the given filter is returned. If no annotation is found, null is returned.
      Type Parameters:
      T - the annotation type
      Parameters:
      target - the target to retrieve the annotation from
      annotationType - the annotation type to retrieve
      filter - the filter to apply to the annotation
      Returns:
      the annotation or Optional.empty() if no annotation is found
    • getNestedAnnotation

      public static <T extends Annotation> T getNestedAnnotation(AnnotatedElement target, Class<T> annotationType)
      Returns the annotation of the given type that is directly present on the given target. If the annotation is not present, check all annotations of the target that are in turn annotated with the given annotation type.
      Type Parameters:
      T - the annotation type
      Parameters:
      target - the target to retrieve the annotation from
      annotationType - the annotation type to retrieve
      Returns:
      the found annotation or null
    • getMetaAnnotated

      public static Stream<Annotation> getMetaAnnotated(AnnotatedElement target, Class<? extends Annotation> metaAnnotation)
      Returns all annotations which are themselves annotated with a meta annotation on the given target.
      Parameters:
      target - the target to retrieve the annotation from
      metaAnnotation - the meta annotation that must be present on the result annotations
      Returns:
      the found annotation or null
    • getNonDefaultMembers

      public static Map<String,Object> getNonDefaultMembers(Annotation annotation)
    • isMetaAnnotation

      public static boolean isMetaAnnotation(Class<? extends Annotation> type)
      Returns true if the given annotation is a meta annotation, i.e. it's targets include ElementType.ANNOTATION_TYPE or are not set at all.
      Parameters:
      type - the annotation type to check
      Returns:
      true if the annotation is a meta annotation
    • getAnnotation

      public static <T extends Annotation> Optional<T> getAnnotation(Field field, Class<T> annotationType)