Class AnnotationWriter

java.lang.Object
org.eclipse.sisu.space.asm.AnnotationVisitor
org.eclipse.sisu.space.asm.AnnotationWriter

final class AnnotationWriter extends AnnotationVisitor
An AnnotationVisitor that generates a corresponding 'annotation' or 'type_annotation' structure, as defined in the Java Virtual Machine Specification (JVMS). AnnotationWriter instances can be chained in a doubly linked list, from which Runtime[In]Visible[Type]Annotations attributes can be generated with the putAnnotations(int, org.eclipse.sisu.space.asm.ByteVector) method. Similarly, arrays of such lists can be used to generate Runtime[In]VisibleParameterAnnotations attributes.
See Also:
  • Field Details

    • symbolTable

      private final SymbolTable symbolTable
      Where the constants used in this AnnotationWriter must be stored.
    • useNamedValues

      private final boolean useNamedValues
      Whether values are named or not. AnnotationWriter instances used for annotation default and annotation arrays use unnamed values (i.e. they generate an 'element_value' structure for each value, instead of an element_name_index followed by an element_value).
    • annotation

      private final ByteVector annotation
      The 'annotation' or 'type_annotation' JVMS structure corresponding to the annotation values visited so far. All the fields of these structures, except the last one - the element_value_pairs array, must be set before this ByteVector is passed to the constructor (num_element_value_pairs can be set to 0, it is reset to the correct value in visitEnd()). The element_value_pairs array is filled incrementally in the various visit() methods.

      Note: as an exception to the above rules, for AnnotationDefault attributes (which contain a single element_value by definition), this ByteVector is initially empty when passed to the constructor, and numElementValuePairsOffset is set to -1.

    • numElementValuePairsOffset

      private final int numElementValuePairsOffset
      The offset in annotation where numElementValuePairs must be stored (or -1 for the case of AnnotationDefault attributes).
    • numElementValuePairs

      private int numElementValuePairs
      The number of element value pairs visited so far.
    • previousAnnotation

      private final AnnotationWriter previousAnnotation
      The previous AnnotationWriter. This field is used to store the list of annotations of a Runtime[In]Visible[Type]Annotations attribute. It is unused for nested or array annotations (annotation values of annotation type), or for AnnotationDefault attributes.
    • nextAnnotation

      private AnnotationWriter nextAnnotation
      The next AnnotationWriter. This field is used to store the list of annotations of a Runtime[In]Visible[Type]Annotations attribute. It is unused for nested or array annotations (annotation values of annotation type), or for AnnotationDefault attributes.
  • Constructor Details

    • AnnotationWriter

      AnnotationWriter(SymbolTable symbolTable, boolean useNamedValues, ByteVector annotation, AnnotationWriter previousAnnotation)
      Constructs a new AnnotationWriter.
      Parameters:
      symbolTable - where the constants used in this AnnotationWriter must be stored.
      useNamedValues - whether values are named or not. AnnotationDefault and annotation arrays use unnamed values.
      annotation - where the 'annotation' or 'type_annotation' JVMS structure corresponding to the visited content must be stored. This ByteVector must already contain all the fields of the structure except the last one (the element_value_pairs array).
      previousAnnotation - the previously visited annotation of the Runtime[In]Visible[Type]Annotations attribute to which this annotation belongs, or null in other cases (e.g. nested or array annotations).
  • Method Details

    • create

      static AnnotationWriter create(SymbolTable symbolTable, String descriptor, AnnotationWriter previousAnnotation)
      Creates a new AnnotationWriter using named values.
      Parameters:
      symbolTable - where the constants used in this AnnotationWriter must be stored.
      descriptor - the class descriptor of the annotation class.
      previousAnnotation - the previously visited annotation of the Runtime[In]Visible[Type]Annotations attribute to which this annotation belongs, or null in other cases (e.g. nested or array annotations).
      Returns:
      a new AnnotationWriter for the given annotation descriptor.
    • create

      static AnnotationWriter create(SymbolTable symbolTable, int typeRef, TypePath typePath, String descriptor, AnnotationWriter previousAnnotation)
      Creates a new AnnotationWriter using named values.
      Parameters:
      symbolTable - where the constants used in this AnnotationWriter must be stored.
      typeRef - a reference to the annotated type. The sort of this type reference must be TypeReference.CLASS_TYPE_PARAMETER, TypeReference.CLASS_TYPE_PARAMETER_BOUND or TypeReference.CLASS_EXTENDS. See TypeReference.
      typePath - the path to the annotated type argument, wildcard bound, array element type, or static inner type within 'typeRef'. May be null if the annotation targets 'typeRef' as a whole.
      descriptor - the class descriptor of the annotation class.
      previousAnnotation - the previously visited annotation of the Runtime[In]Visible[Type]Annotations attribute to which this annotation belongs, or null in other cases (e.g. nested or array annotations).
      Returns:
      a new AnnotationWriter for the given type annotation reference and descriptor.
    • visit

      public void visit(String name, Object value)
      Description copied from class: AnnotationVisitor
      Visits a primitive value of the annotation.
      Overrides:
      visit in class AnnotationVisitor
      Parameters:
      name - the value name.
      value - the actual value, whose type must be Byte, Boolean, Character, Short, Integer , Long, Float, Double, String or Type of Type.OBJECT or Type.ARRAY sort. This value can also be an array of byte, boolean, short, char, int, long, float or double values (this is equivalent to using AnnotationVisitor.visitArray(java.lang.String) and visiting each array element in turn, but is more convenient).
    • visitEnum

      public void visitEnum(String name, String descriptor, String value)
      Description copied from class: AnnotationVisitor
      Visits an enumeration value of the annotation.
      Overrides:
      visitEnum in class AnnotationVisitor
      Parameters:
      name - the value name.
      descriptor - the class descriptor of the enumeration class.
      value - the actual enumeration value.
    • visitAnnotation

      public AnnotationVisitor visitAnnotation(String name, String descriptor)
      Description copied from class: AnnotationVisitor
      Visits a nested annotation value of the annotation.
      Overrides:
      visitAnnotation in class AnnotationVisitor
      Parameters:
      name - the value name.
      descriptor - the class descriptor of the nested annotation class.
      Returns:
      a visitor to visit the actual nested annotation value, or null if this visitor is not interested in visiting this nested annotation. The nested annotation value must be fully visited before calling other methods on this annotation visitor.
    • visitArray

      public AnnotationVisitor visitArray(String name)
      Description copied from class: AnnotationVisitor
      Visits an array value of the annotation. Note that arrays of primitive values (such as byte, boolean, short, char, int, long, float or double) can be passed as value to visit. This is what ClassReader does for non empty arrays of primitive values.
      Overrides:
      visitArray in class AnnotationVisitor
      Parameters:
      name - the value name.
      Returns:
      a visitor to visit the actual array value elements, or null if this visitor is not interested in visiting these values. The 'name' parameters passed to the methods of this visitor are ignored. All the array values must be visited before calling other methods on this annotation visitor.
    • visitEnd

      public void visitEnd()
      Description copied from class: AnnotationVisitor
      Visits the end of the annotation.
      Overrides:
      visitEnd in class AnnotationVisitor
    • computeAnnotationsSize

      int computeAnnotationsSize(String attributeName)
      Returns the size of a Runtime[In]Visible[Type]Annotations attribute containing this annotation and all its predecessors (see previousAnnotation. Also adds the attribute name to the constant pool of the class (if not null).
      Parameters:
      attributeName - one of "Runtime[In]Visible[Type]Annotations", or null.
      Returns:
      the size in bytes of a Runtime[In]Visible[Type]Annotations attribute containing this annotation and all its predecessors. This includes the size of the attribute_name_index and attribute_length fields.
    • computeAnnotationsSize

      static int computeAnnotationsSize(AnnotationWriter lastRuntimeVisibleAnnotation, AnnotationWriter lastRuntimeInvisibleAnnotation, AnnotationWriter lastRuntimeVisibleTypeAnnotation, AnnotationWriter lastRuntimeInvisibleTypeAnnotation)
      Returns the size of the Runtime[In]Visible[Type]Annotations attributes containing the given annotations and all their predecessors (see previousAnnotation. Also adds the attribute names to the constant pool of the class (if not null).
      Parameters:
      lastRuntimeVisibleAnnotation - The last runtime visible annotation of a field, method or class. The previous ones can be accessed with the previousAnnotation field. May be null.
      lastRuntimeInvisibleAnnotation - The last runtime invisible annotation of this a field, method or class. The previous ones can be accessed with the previousAnnotation field. May be null.
      lastRuntimeVisibleTypeAnnotation - The last runtime visible type annotation of this a field, method or class. The previous ones can be accessed with the previousAnnotation field. May be null.
      lastRuntimeInvisibleTypeAnnotation - The last runtime invisible type annotation of a field, method or class field. The previous ones can be accessed with the previousAnnotation field. May be null.
      Returns:
      the size in bytes of a Runtime[In]Visible[Type]Annotations attribute containing the given annotations and all their predecessors. This includes the size of the attribute_name_index and attribute_length fields.
    • putAnnotations

      void putAnnotations(int attributeNameIndex, ByteVector output)
      Puts a Runtime[In]Visible[Type]Annotations attribute containing this annotations and all its predecessors (see previousAnnotation in the given ByteVector. Annotations are put in the same order they have been visited.
      Parameters:
      attributeNameIndex - the constant pool index of the attribute name (one of "Runtime[In]Visible[Type]Annotations").
      output - where the attribute must be put.
    • putAnnotations

      static void putAnnotations(SymbolTable symbolTable, AnnotationWriter lastRuntimeVisibleAnnotation, AnnotationWriter lastRuntimeInvisibleAnnotation, AnnotationWriter lastRuntimeVisibleTypeAnnotation, AnnotationWriter lastRuntimeInvisibleTypeAnnotation, ByteVector output)
      Puts the Runtime[In]Visible[Type]Annotations attributes containing the given annotations and all their predecessors (see previousAnnotation in the given ByteVector. Annotations are put in the same order they have been visited.
      Parameters:
      symbolTable - where the constants used in the AnnotationWriter instances are stored.
      lastRuntimeVisibleAnnotation - The last runtime visible annotation of a field, method or class. The previous ones can be accessed with the previousAnnotation field. May be null.
      lastRuntimeInvisibleAnnotation - The last runtime invisible annotation of this a field, method or class. The previous ones can be accessed with the previousAnnotation field. May be null.
      lastRuntimeVisibleTypeAnnotation - The last runtime visible type annotation of this a field, method or class. The previous ones can be accessed with the previousAnnotation field. May be null.
      lastRuntimeInvisibleTypeAnnotation - The last runtime invisible type annotation of a field, method or class field. The previous ones can be accessed with the previousAnnotation field. May be null.
      output - where the attributes must be put.
    • computeParameterAnnotationsSize

      static int computeParameterAnnotationsSize(String attributeName, AnnotationWriter[] annotationWriters, int annotableParameterCount)
      Returns the size of a Runtime[In]VisibleParameterAnnotations attribute containing all the annotation lists from the given AnnotationWriter sub-array. Also adds the attribute name to the constant pool of the class.
      Parameters:
      attributeName - one of "Runtime[In]VisibleParameterAnnotations".
      annotationWriters - an array of AnnotationWriter lists (designated by their last element).
      annotableParameterCount - the number of elements in annotationWriters to take into account (elements [0..annotableParameterCount[ are taken into account).
      Returns:
      the size in bytes of a Runtime[In]VisibleParameterAnnotations attribute corresponding to the given sub-array of AnnotationWriter lists. This includes the size of the attribute_name_index and attribute_length fields.
    • putParameterAnnotations

      static void putParameterAnnotations(int attributeNameIndex, AnnotationWriter[] annotationWriters, int annotableParameterCount, ByteVector output)
      Puts a Runtime[In]VisibleParameterAnnotations attribute containing all the annotation lists from the given AnnotationWriter sub-array in the given ByteVector.
      Parameters:
      attributeNameIndex - constant pool index of the attribute name (one of Runtime[In]VisibleParameterAnnotations).
      annotationWriters - an array of AnnotationWriter lists (designated by their last element).
      annotableParameterCount - the number of elements in annotationWriters to put (elements [0..annotableParameterCount[ are put).
      output - where the attribute must be put.