Class AnnotationBuilder<P>

  • Type Parameters:
    P - the parent builder type returned by end() (Void when standalone)

    public class AnnotationBuilder<P>
    extends Object
    Fluent builder for a single annotation.
    • Method Detail

      • of

        public static AnnotationBuilder<Void> of​(String type)
        Starts a standalone annotation of the given type.
        Parameters:
        type - the annotation type as an internal name (com/example/Foo), dotted name, or field descriptor (Lcom/example/Foo;)
        Returns:
        a new standalone builder
      • visible

        public AnnotationBuilder<P> visible​(boolean visible)
        Marks this annotation runtime-visible (default) or invisible.
        Parameters:
        visible - true for RuntimeVisibleAnnotations, false for RuntimeInvisibleAnnotations
        Returns:
        this builder
      • isVisible

        public boolean isVisible()
        Returns:
        whether visible
      • end

        public P end()
        Returns:
        the parent builder this annotation was opened from, or null when standalone
      • value

        public AnnotationBuilder<P> value​(String name,
                                          int v)
        Adds an int element.
        Parameters:
        name - the element name
        v - the value
        Returns:
        this builder
      • value

        public AnnotationBuilder<P> value​(String name,
                                          boolean v)
        Adds a boolean element.
        Parameters:
        name - the element name
        v - the value
        Returns:
        this builder
      • value

        public AnnotationBuilder<P> value​(String name,
                                          long v)
        Adds a long element.
        Parameters:
        name - the element name
        v - the value
        Returns:
        this builder
      • value

        public AnnotationBuilder<P> value​(String name,
                                          float v)
        Adds a float element.
        Parameters:
        name - the element name
        v - the value
        Returns:
        this builder
      • value

        public AnnotationBuilder<P> value​(String name,
                                          double v)
        Adds a double element.
        Parameters:
        name - the element name
        v - the value
        Returns:
        this builder
      • byteValue

        public AnnotationBuilder<P> byteValue​(String name,
                                              byte v)
        Adds a byte element.
        Parameters:
        name - the element name
        v - the value
        Returns:
        this builder
      • charValue

        public AnnotationBuilder<P> charValue​(String name,
                                              char v)
        Adds a char element.
        Parameters:
        name - the element name
        v - the value
        Returns:
        this builder
      • shortValue

        public AnnotationBuilder<P> shortValue​(String name,
                                               short v)
        Adds a short element.
        Parameters:
        name - the element name
        v - the value
        Returns:
        this builder
      • stringValue

        public AnnotationBuilder<P> stringValue​(String name,
                                                String v)
        Adds a String element.
        Parameters:
        name - the element name
        v - the value
        Returns:
        this builder
      • classValue

        public AnnotationBuilder<P> classValue​(String name,
                                               String type)
        Adds a class-literal element.
        Parameters:
        name - the element name
        type - a class name (internal or dotted) or a raw type descriptor
        Returns:
        this builder
      • enumValue

        public AnnotationBuilder<P> enumValue​(String name,
                                              String enumType,
                                              String constant)
        Adds an enum-constant element.
        Parameters:
        name - the element name
        enumType - the enum type (internal or dotted name, or field descriptor)
        constant - the enum constant's name
        Returns:
        this builder
      • annotationValue

        public AnnotationBuilder<P> annotationValue​(String name,
                                                    AnnotationBuilder<?> annotation)
        Adds a nested-annotation element.
        Parameters:
        name - the element name
        annotation - the nested annotation's builder, materialized when this one builds
        Returns:
        this builder
      • intArray

        public AnnotationBuilder<P> intArray​(String name,
                                             int... values)
        Adds an int-array element.
        Parameters:
        name - the element name
        values - the array values
        Returns:
        this builder
      • booleanArray

        public AnnotationBuilder<P> booleanArray​(String name,
                                                 boolean... values)
        Adds a boolean-array element.
        Parameters:
        name - the element name
        values - the array values
        Returns:
        this builder
      • byteArray

        public AnnotationBuilder<P> byteArray​(String name,
                                              byte... values)
        Adds a byte-array element.
        Parameters:
        name - the element name
        values - the array values
        Returns:
        this builder
      • charArray

        public AnnotationBuilder<P> charArray​(String name,
                                              char... values)
        Adds a char-array element.
        Parameters:
        name - the element name
        values - the array values
        Returns:
        this builder
      • shortArray

        public AnnotationBuilder<P> shortArray​(String name,
                                               short... values)
        Adds a short-array element.
        Parameters:
        name - the element name
        values - the array values
        Returns:
        this builder
      • longArray

        public AnnotationBuilder<P> longArray​(String name,
                                              long... values)
        Adds a long-array element.
        Parameters:
        name - the element name
        values - the array values
        Returns:
        this builder
      • floatArray

        public AnnotationBuilder<P> floatArray​(String name,
                                               float... values)
        Adds a float-array element.
        Parameters:
        name - the element name
        values - the array values
        Returns:
        this builder
      • doubleArray

        public AnnotationBuilder<P> doubleArray​(String name,
                                                double... values)
        Adds a double-array element.
        Parameters:
        name - the element name
        values - the array values
        Returns:
        this builder
      • stringArray

        public AnnotationBuilder<P> stringArray​(String name,
                                                String... values)
        Adds a String-array element.
        Parameters:
        name - the element name
        values - the array values
        Returns:
        this builder
      • classArray

        public AnnotationBuilder<P> classArray​(String name,
                                               String... types)
        Adds a class-literal-array element.
        Parameters:
        name - the element name
        types - class names (internal or dotted) or raw type descriptors
        Returns:
        this builder
      • enumArray

        public AnnotationBuilder<P> enumArray​(String name,
                                              String enumType,
                                              String... constants)
        Adds an enum-constant-array element with all constants from one enum type.
        Parameters:
        name - the element name
        enumType - the enum type (internal or dotted name, or field descriptor)
        constants - the enum constants' names
        Returns:
        this builder
      • annotationArray

        public AnnotationBuilder<P> annotationArray​(String name,
                                                    AnnotationBuilder<?>... annotations)
        Adds a nested-annotation-array element.
        Parameters:
        name - the element name
        annotations - the nested annotations' builders, materialized when this one builds
        Returns:
        this builder
      • build

        public Annotation build​(ConstPool pool)
        Materializes this specification into an Annotation, interning entries as needed.
        Parameters:
        pool - the constant pool to intern type, name, and value entries into
        Returns:
        the materialized annotation
      • attachTo

        public void attachTo​(MemberEntry member,
                             ConstPool pool)
        Attaches this annotation to a field or method, appending to the existing Runtime[In]VisibleAnnotations attribute of matching visibility when present, or creating one otherwise.
        Parameters:
        member - the field or method to annotate
        pool - the constant pool to intern entries into
      • attachTo

        public void attachTo​(ClassFile classFile,
                             ConstPool pool)
        Attaches this annotation to a class.
        Parameters:
        classFile - the class to annotate
        pool - the constant pool to intern entries into