Class AnnotationTargetUtils

java.lang.Object
io.micronaut.sourcegen.bytecode.core.AnnotationTargetUtils

@Internal public final class AnnotationTargetUtils extends Object
Resolves Target metadata for annotations represented by the Sourcegen model.

Annotation-processing class elements are checked before class loading because an annotation type may be one of the source files currently being compiled.

Since:
2.2
  • Method Details

    • targetsOf

      public static Optional<Set<ElementType>> targetsOf(AnnotationDef annotation, @Nullable ClassLoader classLoader)
      Parameters:
      annotation - The annotation
      classLoader - The fallback class loader
      Returns:
      Declared targets, or empty when the annotation has no usable target declaration
    • declaredTargetsOf

      public static Optional<Set<ElementType>> declaredTargetsOf(ObjectDef objectDef)
      Parameters:
      objectDef - An annotation definition
      Returns:
      Declared targets, or empty when none can be read
    • toElementTypes

      public static Set<ElementType> toElementTypes(@Nullable Object value)
      Converts the model's representation of a Target.value() member.
      Parameters:
      value - The member value
      Returns:
      Element types
    • declarationAnnotations

      public static List<AnnotationDef> declarationAnnotations(List<AnnotationDef> annotations, ElementType elementType, @Nullable ClassLoader classLoader)
      The annotations of a declaration that belong among its declaration annotations, which is every one of them but those a compiler writes as type annotations instead. An annotation whose Target names ElementType.TYPE_USE but not the declaration itself annotates the type of the declaration and nothing else, the way org.jspecify.annotations.Nullable does; one that names both is written in both places, and one that names neither is left where it was put rather than dropped.
      Parameters:
      annotations - The annotations of the declaration
      elementType - The kind of declaration they are written on
      classLoader - The fallback class loader
      Returns:
      The annotations that belong among the declaration annotations
    • typeUseAnnotations

      public static List<AnnotationDef> typeUseAnnotations(List<AnnotationDef> annotations, @Nullable ClassLoader classLoader)
      Parameters:
      annotations - The annotations of a declaration
      classLoader - The fallback class loader
      Returns:
      Those of them that annotate a type use
    • annotatedType

      public static TypeDef annotatedType(TypeDef typeDef, List<AnnotationDef> annotations, @Nullable ClassLoader classLoader)
      The type of a declaration, carrying those of the declaration's annotations that annotate a type use. A compiler writes such an annotation against the type of the declaration and not against the declaration itself, so that a reader finds it on the annotated type.
      Parameters:
      typeDef - The declared type
      annotations - The annotations of the declaration
      classLoader - The fallback class loader
      Returns:
      The type, annotated when any of them belongs on it
    • retentionOf

      public static RetentionPolicy retentionOf(AnnotationDef annotation, @Nullable ClassLoader classLoader)
      The retention of an annotation, resolved the way the ASM backend resolves it.
      Parameters:
      annotation - The annotation
      classLoader - The fallback class loader
      Returns:
      Its retention
    • declaredRetentionOf

      public static Optional<RetentionPolicy> declaredRetentionOf(ObjectDef objectDef)
      Parameters:
      objectDef - An annotation definition
      Returns:
      The retention it declares, or empty when it declares none
    • isArrayMember

      public static boolean isArrayMember(AnnotationDef annotation, String member, @Nullable ClassLoader classLoader)
      Whether a member of an annotation is declared with an array type.

      A single value is a legal source form for an array member - @Target(TYPE) means @Target({TYPE}) - but a class file has no such shorthand, so a writer has to know the declared type to wrap the value. The declaration is read from the definition of an annotation type generated in this same round, from the source element of one still being compiled, and otherwise from the loaded class.

      Parameters:
      annotation - The annotation
      member - The name of the member
      classLoader - The fallback class loader
      Returns:
      Whether the member is declared as an array