Package org.objectweb.asm
Class AnnotationWriter
- java.lang.Object
-
- org.objectweb.asm.AnnotationVisitor
-
- org.objectweb.asm.AnnotationWriter
-
final class AnnotationWriter extends AnnotationVisitor
AnAnnotationVisitor
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 theputAnnotations(int, org.objectweb.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 Summary
Fields Modifier and Type Field Description private ByteVector
annotation
The 'annotation' or 'type_annotation' JVMS structure corresponding to the annotation values visited so far.private AnnotationWriter
nextAnnotation
The next AnnotationWriter.private int
numElementValuePairs
The number of element value pairs visited so far.private int
numElementValuePairsOffset
The offset inannotation
wherenumElementValuePairs
must be stored (or -1 for the case of AnnotationDefault attributes).private AnnotationWriter
previousAnnotation
The previous AnnotationWriter.private SymbolTable
symbolTable
Where the constants used in this AnnotationWriter must be stored.private boolean
useNamedValues
Whether values are named or not.-
Fields inherited from class org.objectweb.asm.AnnotationVisitor
api, av
-
-
Constructor Summary
Constructors Constructor Description AnnotationWriter(SymbolTable symbolTable, boolean useNamedValues, ByteVector annotation, AnnotationWriter previousAnnotation)
Constructs a newAnnotationWriter
.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description (package private) int
computeAnnotationsSize(java.lang.String attributeName)
Returns the size of a Runtime[In]Visible[Type]Annotations attribute containing this annotation and all its predecessors (seepreviousAnnotation
.(package private) 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 (seepreviousAnnotation
.(package private) 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.(package private) static AnnotationWriter
create(SymbolTable symbolTable, int typeRef, TypePath typePath, java.lang.String descriptor, AnnotationWriter previousAnnotation)
Creates a newAnnotationWriter
using named values.(package private) static AnnotationWriter
create(SymbolTable symbolTable, java.lang.String descriptor, AnnotationWriter previousAnnotation)
Creates a newAnnotationWriter
using named values.(package private) void
putAnnotations(int attributeNameIndex, ByteVector output)
Puts a Runtime[In]Visible[Type]Annotations attribute containing this annotations and all its predecessors (seepreviousAnnotation
in the given ByteVector.(package private) 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 (seepreviousAnnotation
in the given ByteVector.(package private) 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.void
visit(java.lang.String name, java.lang.Object value)
Visits a primitive value of the annotation.AnnotationVisitor
visitAnnotation(java.lang.String name, java.lang.String descriptor)
Visits a nested annotation value of the annotation.AnnotationVisitor
visitArray(java.lang.String name)
Visits an array value of the annotation.void
visitEnd()
Visits the end of the annotation.void
visitEnum(java.lang.String name, java.lang.String descriptor, java.lang.String value)
Visits an enumeration value of the annotation.-
Methods inherited from class org.objectweb.asm.AnnotationVisitor
getDelegate
-
-
-
-
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 invisitEnd()
). 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 inannotation
wherenumElementValuePairs
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 newAnnotationWriter
.- 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 Detail
-
create
static AnnotationWriter create(SymbolTable symbolTable, java.lang.String descriptor, AnnotationWriter previousAnnotation)
Creates a newAnnotationWriter
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, java.lang.String descriptor, AnnotationWriter previousAnnotation)
Creates a newAnnotationWriter
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 beTypeReference.CLASS_TYPE_PARAMETER
,TypeReference.CLASS_TYPE_PARAMETER_BOUND
orTypeReference.CLASS_EXTENDS
. SeeTypeReference
.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(java.lang.String name, java.lang.Object value)
Description copied from class:AnnotationVisitor
Visits a primitive value of the annotation.- Overrides:
visit
in classAnnotationVisitor
- Parameters:
name
- the value name.value
- the actual value, whose type must beByte
,Boolean
,Character
,Short
,Integer
,Long
,Float
,Double
,String
orType
ofType.OBJECT
orType.ARRAY
sort. This value can also be an array of byte, boolean, short, char, int, long, float or double values (this is equivalent to usingAnnotationVisitor.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 classAnnotationVisitor
- 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 classAnnotationVisitor
- 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 values (such as byte, boolean, short, char, int, long, float or double) can be passed as value tovisit
. This is whatClassReader
does for non empty arrays of primitive values.- Overrides:
visitArray
in classAnnotationVisitor
- 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 classAnnotationVisitor
-
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 (seepreviousAnnotation
. 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 (seepreviousAnnotation
. 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 thepreviousAnnotation
field. May be null.lastRuntimeInvisibleAnnotation
- The last runtime invisible annotation of this a field, method or class. The previous ones can be accessed with thepreviousAnnotation
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 thepreviousAnnotation
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 thepreviousAnnotation
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 (seepreviousAnnotation
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 (seepreviousAnnotation
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 thepreviousAnnotation
field. May be null.lastRuntimeInvisibleAnnotation
- The last runtime invisible annotation of this a field, method or class. The previous ones can be accessed with thepreviousAnnotation
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 thepreviousAnnotation
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 thepreviousAnnotation
field. May be null.output
- where the attributes 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.
-
-