org.objectweb.asm.attrs

Class Annotation


public class Annotation
extends Object

Annotation data contains an annotated type and its array of the element-value pairs. Structure is in the following format:
   annotation {
     u2 type_index;
     u2 num_element_value_pairs;
     {
       u2 element_name_index;
       element_value value;
     } element_value_pairs[num_element_value_pairs];
   }
 
The items of the annotation structure are as follows:
The element_value structure is a discriminated union representing the value of a element-value pair. It is used to represent values in all class file attributes that describe annotations ( RuntimeVisibleAnnotations, RuntimeInvisibleAnnotations, RuntimeVisibleParameterAnnotations, and RuntimeInvisibleParameterAnnotations).

The element_value structure has the following format:

   element_value {
     u1 tag;
     union {
       u2   const_value_index;
       {
         u2   type_name_index;
         u2   const_name_index;
       } enum_const_value;
       u2   class_info_index;
       annotation annotation_value;
       {
         u2    num_values;
         element_value values[num_values];
       } array_value;
     } value;
   }
 
The items of the element_value structure are as follows:
     tag  value Element Type
     's'  String
     'e'  enum constant
     'c'  class
     '@'  annotation type
     '['  array
   
annotation
element_value structure
Author:
Eugene Kuleshov
See Also:
JSR 175 : A Metadata Facility for the Java Programming Language

Nested Class Summary

static class
Annotation.EnumConstValue
Container class used to store enum_const_value structure.

Field Summary

List
elementValues
List of Object[]{name, value} pairs.
String
type
A fully qualified class name in internal form (see Type).

Constructor Summary

Annotation()
Annotation(String type)

Method Summary

void
add(String name, Object value)
int
read(ClassReader cr, int off, char[] buf)
Reads annotation data structures.
static int
readAnnotations(List annotations, ClassReader cr, int off, char[] buf)
Utility method to read List of annotations.
static void
readParameterAnnotations(List parameters, ClassReader cr, int off, char[] buf)
Utility method to read List of parameters annotations.
protected static Object
readValue(ClassReader cr, int[] off, char[] buf)
Reads element_value data structures.
static String
stringAnnotations(List annotations)
Returns annotation values in the format described in JSR-175 for Java source code.
static String
stringParameterAnnotations(List parameters)
Returns parameter annotation values in the format described in JSR-175 for Java source code.
String
toString()
Returns value in the format described in JSR-175 for Java source code.
void
write(ByteVector bv, ClassWriter cw)
Writes annotation data structures.
static ByteVector
writeAnnotations(ByteVector bv, List annotations, ClassWriter cw)
Utility method to write List of annotations.
static ByteVector
writeParametersAnnotations(ByteVector bv, List parameters, ClassWriter cw)
Utility method to write List of parameters annotations.
protected static ByteVector
writeValue(ByteVector bv, Object value, ClassWriter cw)
Writes element_value data structures.

Field Details

elementValues

public List elementValues
List of Object[]{name, value} pairs. Where name is String and value is one of Byte, Character, Double, Float, Integer, Long, Short, Boolean, String, Annotation.EnumConstValue, Type, Annotation or Object[].

type

public String type

Constructor Details

Annotation

public Annotation()

Annotation

public Annotation(String type)

Method Details

add

public void add(String name,
                Object value)

read

public int read(ClassReader cr,
                int off,
                char[] buf)
Reads annotation data structures.
Parameters:
cr - the class that contains the attribute to be read.
off - index of the first byte of the data structure.
buf - buffer to be used to call readUTF8, readClass or readConst.
Returns:
offset position in bytecode after reading annotation

readAnnotations

public static int readAnnotations(List annotations,
                                  ClassReader cr,
                                  int off,
                                  char[] buf)
Utility method to read List of annotations. Each element of annotations List will have Annotation instance.
Parameters:
annotations - the List to store parameters annotations.
cr - the class that contains the attribute to be read.
off - index of the first byte of the data structure.
buf - buffer to be used to call readUTF8, readClass or readConst.
Returns:
offset position in bytecode after reading annotations

readParameterAnnotations

public static void readParameterAnnotations(List parameters,
                                            ClassReader cr,
                                            int off,
                                            char[] buf)
Utility method to read List of parameters annotations.
Parameters:
parameters - the List to store parameters annotations. Each element of the parameters List will have List of Annotation instances.
cr - the class that contains the attribute to be read.
off - index of the first byte of the data structure.
buf - buffer to be used to call readUTF8, readClass or readConst.

readValue

protected static Object readValue(ClassReader cr,
                                  int[] off,
                                  char[] buf)
Reads element_value data structures.
Parameters:
cr - the class that contains the attribute to be read.
off - index of the first byte of the data structure.
buf - buffer to be used to call readUTF8, readClass or readConst.
Returns:
offset position in bytecode after reading annotation

stringAnnotations

public static String stringAnnotations(List annotations)
Returns annotation values in the format described in JSR-175 for Java source code.
Parameters:
annotations - a list of annotations.
Returns:
annotation values in the format described in JSR-175 for Java source code.

stringParameterAnnotations

public static String stringParameterAnnotations(List parameters)
Returns parameter annotation values in the format described in JSR-175 for Java source code.
Parameters:
parameters - a list of parameter annotations.
Returns:
parameter annotation values in the format described in JSR-175 for Java source code.

toString

public String toString()
Returns value in the format described in JSR-175 for Java source code.
Returns:
value in the format described in JSR-175 for Java source code.

write

public void write(ByteVector bv,
                  ClassWriter cw)
Writes annotation data structures.
Parameters:
bv - the byte array form to store data structures.
cw - the class to which this attribute must be added. This parameter can be used to add to the constant pool of this class the items that corresponds to this attribute.

writeAnnotations

public static ByteVector writeAnnotations(ByteVector bv,
                                          List annotations,
                                          ClassWriter cw)
Utility method to write List of annotations.
Parameters:
bv - the byte array form to store data structures.
annotations - the List of annotations to write. Elements should be instances of the Annotation class.
cw - the class to which this attribute must be added. This parameter can be used to add to the constant pool of this class the items that corresponds to this attribute.
Returns:
the byte array form with saved annotations.

writeParametersAnnotations

public static ByteVector writeParametersAnnotations(ByteVector bv,
                                                    List parameters,
                                                    ClassWriter cw)
Utility method to write List of parameters annotations.
Parameters:
bv - the byte array form to store data structures.
parameters - the List of parametars to write. Elements should be instances of the List that contains instances of the Annotation class.
cw - the class to which this attribute must be added. This parameter can be used to add to the constant pool of this class the items that corresponds to this attribute.
Returns:
the byte array form with saved annotations.

writeValue

protected static ByteVector writeValue(ByteVector bv,
                                       Object value,
                                       ClassWriter cw)
Writes element_value data structures.
Parameters:
bv - the byte array form to store data structures.
value -
cw - the class to which this attribute must be added. This parameter can be used to add to the constant pool of this class the items that corresponds to this attribute.
Returns:
bv.