Class AnnotationsWriter

java.lang.Object
javassist.bytecode.annotation.AnnotationsWriter
Direct Known Subclasses:
TypeAnnotationsWriter

public class AnnotationsWriter extends Object
A convenience class for constructing a ..Annotations_attribute. See the source code of the AnnotationsAttribute.Copier class.

The following code snippet is an example of use of this class:

 ConstPool pool = ...;
 output = new ByteArrayOutputStream();
 writer = new AnnotationsWriter(output, pool);

 writer.numAnnotations(1);
 writer.annotation("Author", 2);
 writer.memberValuePair("name");      // element_value_pair
 writer.constValueIndex("chiba");
 writer.memberValuePair("address");   // element_value_pair
 writer.constValueIndex("tokyo");

 writer.close();
 byte[] attribute_info = output.toByteArray();
 AnnotationsAttribute anno
     = new AnnotationsAttribute(pool, AnnotationsAttribute.visibleTag,
                                attribute_info);
 

The code snippet above generates the annotation attribute corresponding to this annotation:

  @Author(name = "chiba", address = "tokyo")
 
See Also:
  • Constructor Summary

    Constructors
    Constructor
    Description
    Constructs with the given output stream.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    annotation(int typeIndex, int numMemberValuePairs)
    Writes annotation.
    void
    annotation(String type, int numMemberValuePairs)
    Writes annotation.
    void
    Writes tag and annotation_value in element_value.
    void
    arrayValue(int numValues)
    Writes tag and array_value in element_value.
    void
    classInfoIndex(int index)
    Writes tag and class_info_index in element_value.
    void
    Writes tag and class_info_index in element_value.
    void
    Closes the output stream.
    void
    constValueIndex(boolean value)
    Writes tag and const_value_index in element_value.
    void
    constValueIndex(byte value)
    Writes tag and const_value_index in element_value.
    void
    constValueIndex(char value)
    Writes tag and const_value_index in element_value.
    void
    constValueIndex(double value)
    Writes tag and const_value_index in element_value.
    void
    constValueIndex(float value)
    Writes tag and const_value_index in element_value.
    void
    constValueIndex(int value)
    Writes tag and const_value_index in element_value.
    void
    constValueIndex(int tag, int index)
    Writes tag and const_value_index in element_value.
    void
    constValueIndex(long value)
    Writes tag and const_value_index in element_value.
    void
    constValueIndex(short value)
    Writes tag and const_value_index in element_value.
    void
    Writes tag and const_value_index in element_value.
    void
    enumConstValue(int typeNameIndex, int constNameIndex)
    Writes tag and enum_const_value in element_value.
    void
    enumConstValue(String typeName, String constName)
    Writes tag and enum_const_value in element_value.
    Obtains the constant pool given to the constructor.
    void
    memberValuePair(int memberNameIndex)
    Writes an element of a element_value_pairs array in annotation.
    void
    memberValuePair(String memberName)
    Writes an element of a element_value_pairs array in annotation.
    void
    numAnnotations(int num)
    Writes num_annotations in Runtime(In)VisibleAnnotations_attribute.
    void
    numParameters(int num)
    Writes num_parameters in Runtime(In)VisibleParameterAnnotations_attribute.

    Methods inherited from class java.lang.Object

    equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • AnnotationsWriter

      public AnnotationsWriter(OutputStream os, ConstPool cp)
      Constructs with the given output stream.
      Parameters:
      os - the output stream.
      cp - the constant pool.
  • Method Details

    • getConstPool

      public ConstPool getConstPool()
      Obtains the constant pool given to the constructor.
    • close

      public void close() throws IOException
      Closes the output stream.
      Throws:
      IOException
    • numParameters

      public void numParameters(int num) throws IOException
      Writes num_parameters in Runtime(In)VisibleParameterAnnotations_attribute. This method must be followed by num calls to numAnnotations().
      Throws:
      IOException
    • numAnnotations

      public void numAnnotations(int num) throws IOException
      Writes num_annotations in Runtime(In)VisibleAnnotations_attribute. This method must be followed by num calls to annotation().
      Throws:
      IOException
    • annotation

      public void annotation(String type, int numMemberValuePairs) throws IOException
      Writes annotation. This method must be followed by numMemberValuePairs calls to memberValuePair().
      Parameters:
      type - the annotation interface name.
      numMemberValuePairs - num_element_value_pairs in annotation.
      Throws:
      IOException
    • annotation

      public void annotation(int typeIndex, int numMemberValuePairs) throws IOException
      Writes annotation. This method must be followed by numMemberValuePairs calls to memberValuePair().
      Parameters:
      typeIndex - type_index in annotation.
      numMemberValuePairs - num_element_value_pairs in annotation.
      Throws:
      IOException
    • memberValuePair

      public void memberValuePair(String memberName) throws IOException
      Writes an element of a element_value_pairs array in annotation. This method must be followed by a call to constValueIndex(), enumConstValue(), etc.
      Parameters:
      memberName - the element name.
      Throws:
      IOException
    • memberValuePair

      public void memberValuePair(int memberNameIndex) throws IOException
      Writes an element of a element_value_pairs array in annotation. This method must be followed by a call to constValueIndex(), enumConstValue(), etc.
      Parameters:
      memberNameIndex - element_name_index in element_value_pairs array.
      Throws:
      IOException
    • constValueIndex

      public void constValueIndex(boolean value) throws IOException
      Writes tag and const_value_index in element_value.
      Parameters:
      value - the constant value.
      Throws:
      IOException
    • constValueIndex

      public void constValueIndex(byte value) throws IOException
      Writes tag and const_value_index in element_value.
      Parameters:
      value - the constant value.
      Throws:
      IOException
    • constValueIndex

      public void constValueIndex(char value) throws IOException
      Writes tag and const_value_index in element_value.
      Parameters:
      value - the constant value.
      Throws:
      IOException
    • constValueIndex

      public void constValueIndex(short value) throws IOException
      Writes tag and const_value_index in element_value.
      Parameters:
      value - the constant value.
      Throws:
      IOException
    • constValueIndex

      public void constValueIndex(int value) throws IOException
      Writes tag and const_value_index in element_value.
      Parameters:
      value - the constant value.
      Throws:
      IOException
    • constValueIndex

      public void constValueIndex(long value) throws IOException
      Writes tag and const_value_index in element_value.
      Parameters:
      value - the constant value.
      Throws:
      IOException
    • constValueIndex

      public void constValueIndex(float value) throws IOException
      Writes tag and const_value_index in element_value.
      Parameters:
      value - the constant value.
      Throws:
      IOException
    • constValueIndex

      public void constValueIndex(double value) throws IOException
      Writes tag and const_value_index in element_value.
      Parameters:
      value - the constant value.
      Throws:
      IOException
    • constValueIndex

      public void constValueIndex(String value) throws IOException
      Writes tag and const_value_index in element_value.
      Parameters:
      value - the constant value.
      Throws:
      IOException
    • constValueIndex

      public void constValueIndex(int tag, int index) throws IOException
      Writes tag and const_value_index in element_value.
      Parameters:
      tag - tag in element_value.
      index - const_value_index in element_value.
      Throws:
      IOException
    • enumConstValue

      public void enumConstValue(String typeName, String constName) throws IOException
      Writes tag and enum_const_value in element_value.
      Parameters:
      typeName - the type name of the enum constant.
      constName - the simple name of the enum constant.
      Throws:
      IOException
    • enumConstValue

      public void enumConstValue(int typeNameIndex, int constNameIndex) throws IOException
      Writes tag and enum_const_value in element_value.
      Parameters:
      typeNameIndex - type_name_index in element_value.
      constNameIndex - const_name_index in element_value.
      Throws:
      IOException
    • classInfoIndex

      public void classInfoIndex(String name) throws IOException
      Writes tag and class_info_index in element_value.
      Parameters:
      name - the class name.
      Throws:
      IOException
    • classInfoIndex

      public void classInfoIndex(int index) throws IOException
      Writes tag and class_info_index in element_value.
      Parameters:
      index - class_info_index
      Throws:
      IOException
    • annotationValue

      public void annotationValue() throws IOException
      Writes tag and annotation_value in element_value. This method must be followed by a call to annotation().
      Throws:
      IOException
    • arrayValue

      public void arrayValue(int numValues) throws IOException
      Writes tag and array_value in element_value. This method must be followed by numValues calls to constValueIndex(), enumConstValue(), etc.
      Parameters:
      numValues - num_values in array_value.
      Throws:
      IOException