Package org.mvel2.asm

Class 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.mvel2.asm.ByteVector) method. Similarly, arrays of such lists can be used to generate Runtime[In]VisibleParameterAnnotations attributes.
    See Also:
    JVMS 4.7.16, JVMS 4.7.20
    • Field Detail

      • 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 Detail

      • 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).
      • AnnotationWriter

        AnnotationWriter​(SymbolTable symbolTable,
                         ByteVector annotation,
                         AnnotationWriter previousAnnotation)
        Constructs a new AnnotationWriter using named values.
        Parameters:
        symbolTable - where the constants used in this AnnotationWriter must be stored.
        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 Detail

      • visit

        public void visit​(java.lang.String name,
                          java.lang.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​(java.lang.String name,
                              java.lang.String descriptor,
                              java.lang.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​(java.lang.String name,
                                                 java.lang.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​(java.lang.String name)
        Description copied from class: AnnotationVisitor
        Visits an array value of the annotation. Note that arrays of primitive types (such as byte, boolean, short, char, int, long, float or double) can be passed as value to visit. This is what ClassReader does.
        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.
      • computeAnnotationsSize

        int computeAnnotationsSize​(java.lang.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.
      • 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.
      • computeParameterAnnotationsSize

        static int computeParameterAnnotationsSize​(java.lang.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.