org.ow2.asmdex
Class AnnotationWriter

java.lang.Object
  extended by org.ow2.asmdex.AnnotationVisitor
      extended by org.ow2.asmdex.AnnotationWriter
Direct Known Subclasses:
AnnotationWriterArray, AnnotationWriterDefaultAnnotation, AnnotationWriterSubAnnotation

public class AnnotationWriter
extends AnnotationVisitor

The Annotation Writer. Visits an Annotation and stores its content.

A visitor can have children when it needs to visit an Array or sub-Annotations. It has no link with them, but a child knows its father, and once the visit of all its elements are over, the child builds a single element (array, sub-annotation) and adds it to its parent Annotation Item list.

Once the father has visited everything, it registers its list to the Constant Pool (unless its a Default Annotation, which will be registered by the Class, in another constructed AnnotationItem consisting all the DefaultAnnotation of the Class).

There are exceptions though : the Signature Array, String, Enum and Class array must NOT be sorted. So in the case of a visit(), if we're encoding an Array, we don't add the Elements to the current AnnotationItem like we do for the other cases, but add the Elements directly to the Array, which in turn will be added to the AnnotationItem at visitEnd().

Author:
Julien Névo

Field Summary
protected  AnnotationItem annotationItem
          The Annotation Item currently being visited.
protected  java.lang.String annotationType
          Type of the Annotation.
protected  java.lang.String currentName
          Current name of the Annotation.
protected  AnnotationWriter father
          "Father" of the current AnnotationWriter.
protected  boolean mustSortArray
          Indicates if the Array elements must be sorted.
protected  EncodedValueArray unsortedArray
          This is an Array, only used for Signature Array, String Array, Enum and Class Arrays, as these ones are unsorted.
 
Fields inherited from class org.ow2.asmdex.AnnotationVisitor
api, av
 
Constructor Summary
AnnotationWriter(ConstantPool constantPool, AnnotationItem annotationItem)
          Constructor of the Annotation Writer.
 
Method Summary
static AnnotationWriter createAnnotationWriter(java.lang.String desc, boolean visible, ConstantPool constantPool, ClassDefinitionItem classDefinitionItem)
          Creates an Annotation Writer for it to visit a following Annotation.
 AnnotationItem getAnnotationItem()
          Returns the Annotation Item being visited.
 void setMustSortArray(boolean mustSortArray)
          Indicates if the array must be sorted.
 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 desc)
          Visits a nested annotation value of the annotation.
 AnnotationVisitor visitArray(java.lang.String name)
          Visits an array value of the annotation.
 void visitClass(java.lang.String annotationName, java.lang.String className)
          Visits a Class declaration inside an annotation (example : class a()).
 void visitEnd()
          Visits the end of the annotation.
protected  void visitEndRegisterAnnotationItemNotDefaultAnnotation()
          Registers the annotation_item to the Constant Pool.
 void visitEnum(java.lang.String name, java.lang.String desc, java.lang.String value)
          Visits an enumeration value of the annotation.
protected  void visitManageNonDefaultAnnotation(java.lang.String name, java.lang.Object value, boolean isArrayAnnotation)
          Method called only by Visit, in the case our annotation is not a DefaultAnnotation.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

annotationItem

protected AnnotationItem annotationItem
The Annotation Item currently being visited.


currentName

protected java.lang.String currentName
Current name of the Annotation. This is only used for Enumeration and Class Arrays. According to ASM's behavior, Enumerations inside an array have a Null name, as the visitArray in which they are declared already has one. This "current name" is only used if the name of the Enumeration or Class Array name is Null.


annotationType

protected java.lang.String annotationType
Type of the Annotation. Only used for sub-annotations, given by the "father".


father

protected AnnotationWriter father
"Father" of the current AnnotationWriter. This is useful to give him the result of the parsing of the sub-level of the annotation (Array, sub-annotation...).


unsortedArray

protected EncodedValueArray unsortedArray
This is an Array, only used for Signature Array, String Array, Enum and Class Arrays, as these ones are unsorted. So instead of adding elements to the Annotation Item like we do in any other cases, we fill this Array, which will be added to the current Annotation Item at visitEnd(). It is only created when needed, that is if the method setMustSortArray is called, with a false value.


mustSortArray

protected boolean mustSortArray
Indicates if the Array elements must be sorted. Always true, except for Signature array and String array, Enum and Class Arrays. It must NOT be set directly ; use the setMustSortArray method.

Constructor Detail

AnnotationWriter

public AnnotationWriter(ConstantPool constantPool,
                        AnnotationItem annotationItem)
Constructor of the Annotation Writer.

Parameters:
constantPool - the Constant Pool of the Application.
annotationItem - the Annotation Item to visit.
Method Detail

getAnnotationItem

public AnnotationItem getAnnotationItem()
Returns the Annotation Item being visited.

Returns:
the Annotation Item being visited.

setMustSortArray

public void setMustSortArray(boolean mustSortArray)
Indicates if the array must be sorted. False only for Signature array and String array.

Parameters:
mustSortArray - true to sort the Array.

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. This value can also be an array of byte, boolean, short, char, int, long, float or double values (this is equivalent to using visitArray and visiting each array element in turn, but is more convenient).

visitManageNonDefaultAnnotation

protected void visitManageNonDefaultAnnotation(java.lang.String name,
                                               java.lang.Object value,
                                               boolean isArrayAnnotation)
Method called only by Visit, in the case our annotation is not a DefaultAnnotation.

Parameters:
name - the value name.
value - the actual value.
isArrayAnnotation - indicates if the current Annotation is an Array Annotation.

visitEnd

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

Overrides:
visitEnd in class AnnotationVisitor

visitEndRegisterAnnotationItemNotDefaultAnnotation

protected void visitEndRegisterAnnotationItemNotDefaultAnnotation()
Registers the annotation_item to the Constant Pool. Must only be called from a VisitEnd, and the Annotation must NOT be a DefaultAnnotation, because it won't be used as-is, but will be converted into a new AnnotationItem consisting of all the DefaultAnnotation of the Class.


visitEnum

public void visitEnum(java.lang.String name,
                      java.lang.String desc,
                      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.
desc - the class descriptor of the enumeration class.
value - the actual enumeration value.

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 ClassVisitor 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.

visitAnnotation

public AnnotationVisitor visitAnnotation(java.lang.String name,
                                         java.lang.String desc)
Description copied from class: AnnotationVisitor
Visits a nested annotation value of the annotation.

Overrides:
visitAnnotation in class AnnotationVisitor
Parameters:
name - the value name.
desc - 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.

visitClass

public void visitClass(java.lang.String annotationName,
                       java.lang.String className)
Description copied from class: AnnotationVisitor
Visits a Class declaration inside an annotation (example : class a()).

Overrides:
visitClass in class AnnotationVisitor
Parameters:
annotationName - the name of the annotation. Null if the visit of the Class is inside an Array.
className - the name of the class.

createAnnotationWriter

public static AnnotationWriter createAnnotationWriter(java.lang.String desc,
                                                      boolean visible,
                                                      ConstantPool constantPool,
                                                      ClassDefinitionItem classDefinitionItem)
Creates an Annotation Writer for it to visit a following Annotation. Also registers the descriptor to the Constant pool. The Annotation Item created inside the Annotation Writer is NOT registered to anything, so the calling Method may want to do it.

Parameters:
desc - the class descriptor of the annotation class.
visible - true if the annotation is visible at runtime.
constantPool - the Constant Pool of the Application.
classDefinitionItem - the Class the Annotation is linked to. This is ONLY useful for Default Annotation, and must be Null otherwise.
Returns:
an Annotation Writer for it to visit a following Annotation.