javassist.bytecode

Class AnnotationsAttribute

public class AnnotationsAttribute extends AttributeInfo

A class representing RuntimeVisibleAnnotations_attribute and RuntimeInvisibleAnnotations_attribute.

To obtain an AnnotationAttribute object, invoke getAttribute(AnnotationsAttribute.visibleTag) in ClassFile, MethodInfo, or FieldInfo. The obtained attribute is a runtime visible annotations attribute. If the parameter is AnnotationAttribute.invisibleTag, then the obtained attribute is a runtime invisible one.

For example,

This code snippet retrieves an annotation of the type Author from the MethodInfo object specified by minfo. Then, it prints the value of name in Author.

If the annotation type Author is annotated by a meta annotation:

Then Author is visible at runtime. Therefore, the third statement of the code snippet above must be changed into:

The attribute tag must be visibleTag instead of invisibleTag.

If the member value of an annotation is not specified, the default value is used as that member value. If so, getMemberValue() in Annotation returns null since the default value is not included in the AnnotationsAttribute. It is included in the AnnotationDefaultAttribute of the method declared in the annotation type.

If you want to record a new AnnotationAttribute object, execute the following snippet:

The last statement is necessary if the class file was produced by Javassist or JDK 1.4. Otherwise, it is not necessary.

See Also: AnnotationDefaultAttribute Annotation

Field Summary
static StringinvisibleTag
The name of the RuntimeInvisibleAnnotations attribute.
static StringvisibleTag
The name of the RuntimeVisibleAnnotations attribute.
Constructor Summary
AnnotationsAttribute(ConstPool cp, String attrname, byte[] info)
Constructs a Runtime(In)VisisbleAnnotations_attribute.
AnnotationsAttribute(ConstPool cp, String attrname)
Constructs an empty Runtime(In)VisisbleAnnotations_attribute.
Method Summary
voidaddAnnotation(Annotation annotation)
Adds an annotation.
AttributeInfocopy(ConstPool newCp, Map classnames)
Copies this attribute and returns a new copy.
AnnotationgetAnnotation(String type)
Parses the annotations and returns a data structure representing the annotation with the specified type.
Annotation[]getAnnotations()
Parses the annotations and returns a data structure representing that parsed annotations.
intnumAnnotations()
Returns num_annotations.
voidsetAnnotation(Annotation annotation)
Changes the annotations.
voidsetAnnotations(Annotation[] annotations)
Changes the annotations represented by this object according to the given array of Annotation objects.
StringtoString()
Returns a string representation of this object.

Field Detail

invisibleTag

public static final String invisibleTag
The name of the RuntimeInvisibleAnnotations attribute.

visibleTag

public static final String visibleTag
The name of the RuntimeVisibleAnnotations attribute.

Constructor Detail

AnnotationsAttribute

public AnnotationsAttribute(ConstPool cp, String attrname, byte[] info)
Constructs a Runtime(In)VisisbleAnnotations_attribute.

Parameters: cp constant pool attrname attribute name (visibleTag or invisibleTag). info the contents of this attribute. It does not include attribute_name_index or attribute_length.

AnnotationsAttribute

public AnnotationsAttribute(ConstPool cp, String attrname)
Constructs an empty Runtime(In)VisisbleAnnotations_attribute. A new annotation can be later added to the created attribute by setAnnotations().

Parameters: cp constant pool attrname attribute name (visibleTag or invisibleTag).

See Also: (Annotation[])

Method Detail

addAnnotation

public void addAnnotation(Annotation annotation)
Adds an annotation. If there is an annotation with the same type, it is removed before the new annotation is added.

Parameters: annotation the added annotation.

copy

public AttributeInfo copy(ConstPool newCp, Map classnames)
Copies this attribute and returns a new copy.

getAnnotation

public Annotation getAnnotation(String type)
Parses the annotations and returns a data structure representing the annotation with the specified type. See also getAnnotations() as to the returned data structure.

Parameters: type the annotation type.

Returns: null if the specified annotation type is not included.

See Also: getAnnotations

getAnnotations

public Annotation[] getAnnotations()
Parses the annotations and returns a data structure representing that parsed annotations. Note that changes of the node values of the returned tree are not reflected on the annotations represented by this object unless the tree is copied back to this object by setAnnotations().

See Also: (Annotation[])

numAnnotations

public int numAnnotations()
Returns num_annotations.

setAnnotation

public void setAnnotation(Annotation annotation)
Changes the annotations. A call to this method is equivalent to:

Parameters: annotation the data structure representing the new annotation.

setAnnotations

public void setAnnotations(Annotation[] annotations)
Changes the annotations represented by this object according to the given array of Annotation objects.

Parameters: annotations the data structure representing the new annotations.

toString

public String toString()
Returns a string representation of this object.
Javassist, a Java-bytecode translator toolkit.
Copyright (C) 1999-2006 Shigeru Chiba. All Rights Reserved.