Class AnnotationsAttribute
- java.lang.Object
-
- javassist.bytecode.AttributeInfo
-
- javassist.bytecode.AnnotationsAttribute
-
public class AnnotationsAttribute extends AttributeInfo
A class representingRuntimeVisibleAnnotations_attribute
andRuntimeInvisibleAnnotations_attribute
.To obtain an AnnotationAttribute object, invoke
getAttribute(AnnotationsAttribute.visibleTag)
inClassFile
,MethodInfo
, orFieldInfo
. The obtained attribute is a runtime visible annotations attribute. If the parameter isAnnotationAttribute.invisibleTag
, then the obtained attribute is a runtime invisible one.For example,
import javassist.bytecode.annotation.Annotation; : CtMethod m = ... ; MethodInfo minfo = m.getMethodInfo(); AnnotationsAttribute attr = (AnnotationsAttribute) minfo.getAttribute(AnnotationsAttribute.invisibleTag); Annotation an = attr.getAnnotation("Author"); String s = ((StringMemberValue)an.getMemberValue("name")).getValue(); System.out.println("@Author(name=" + s + ")");
This code snippet retrieves an annotation of the type
Author
from theMethodInfo
object specified byminfo
. Then, it prints the value ofname
inAuthor
.If the annotation type
Author
is annotated by a meta annotation:@Retention(RetentionPolicy.RUNTIME)
Then
Author
is visible at runtime. Therefore, the third statement of the code snippet above must be changed into:AnnotationsAttribute attr = (AnnotationsAttribute) minfo.getAttribute(AnnotationsAttribute.visibleTag);
The attribute tag must be
visibleTag
instead ofinvisibleTag
.If the member value of an annotation is not specified, the default value is used as that member value. If so,
getMemberValue()
inAnnotation
returnsnull
since the default value is not included in theAnnotationsAttribute
. It is included in theAnnotationDefaultAttribute
of the method declared in the annotation type.If you want to record a new AnnotationAttribute object, execute the following snippet:
ClassFile cf = ... ; ConstPool cp = cf.getConstPool(); AnnotationsAttribute attr = new AnnotationsAttribute(cp, AnnotationsAttribute.visibleTag); Annotation a = new Annotation("Author", cp); a.addMemberValue("name", new StringMemberValue("Chiba", cp)); attr.setAnnotation(a); cf.addAttribute(attr); cf.setVersionToJava5();
The last statement is necessary if the class file was produced by
javac
of JDK 1.4 or earlier. Otherwise, it is not necessary.- See Also:
AnnotationDefaultAttribute
,Annotation
-
-
Field Summary
Fields Modifier and Type Field Description static java.lang.String
invisibleTag
The name of theRuntimeInvisibleAnnotations
attribute.static java.lang.String
visibleTag
The name of theRuntimeVisibleAnnotations
attribute.
-
Constructor Summary
Constructors Constructor Description AnnotationsAttribute(ConstPool cp, java.lang.String attrname)
Constructs an emptyRuntime(In)VisibleAnnotations_attribute
.AnnotationsAttribute(ConstPool cp, java.lang.String attrname, byte[] info)
Constructs aRuntime(In)VisibleAnnotations_attribute
.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
addAnnotation(Annotation annotation)
Adds an annotation.AttributeInfo
copy(ConstPool newCp, java.util.Map<java.lang.String,java.lang.String> classnames)
Copies this attribute and returns a new copy.Annotation
getAnnotation(java.lang.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.int
numAnnotations()
Returnsnum_annotations
.boolean
removeAnnotation(java.lang.String type)
Removes an annotation by type.void
setAnnotation(Annotation annotation)
Changes the annotations.void
setAnnotations(Annotation[] annotations)
Changes the annotations represented by this object according to the given array ofAnnotation
objects.java.lang.String
toString()
Returns a string representation of this object.-
Methods inherited from class javassist.bytecode.AttributeInfo
get, getConstPool, getName, length, set
-
-
-
-
Field Detail
-
visibleTag
public static final java.lang.String visibleTag
The name of theRuntimeVisibleAnnotations
attribute.- See Also:
- Constant Field Values
-
invisibleTag
public static final java.lang.String invisibleTag
The name of theRuntimeInvisibleAnnotations
attribute.- See Also:
- Constant Field Values
-
-
Constructor Detail
-
AnnotationsAttribute
public AnnotationsAttribute(ConstPool cp, java.lang.String attrname, byte[] info)
Constructs aRuntime(In)VisibleAnnotations_attribute
.- Parameters:
cp
- constant poolattrname
- attribute name (visibleTag
orinvisibleTag
).info
- the contents of this attribute. It does not includeattribute_name_index
orattribute_length
.
-
AnnotationsAttribute
public AnnotationsAttribute(ConstPool cp, java.lang.String attrname)
Constructs an emptyRuntime(In)VisibleAnnotations_attribute
. A new annotation can be later added to the created attribute bysetAnnotations()
.- Parameters:
cp
- constant poolattrname
- attribute name (visibleTag
orinvisibleTag
).- See Also:
setAnnotations(Annotation[])
-
-
Method Detail
-
numAnnotations
public int numAnnotations()
Returnsnum_annotations
.
-
copy
public AttributeInfo copy(ConstPool newCp, java.util.Map<java.lang.String,java.lang.String> classnames)
Copies this attribute and returns a new copy.- Overrides:
copy
in classAttributeInfo
- Parameters:
newCp
- the constant pool table used by the new copy.classnames
- pairs of replaced and substituted class names.
-
getAnnotation
public Annotation getAnnotation(java.lang.String type)
Parses the annotations and returns a data structure representing the annotation with the specified type. See alsogetAnnotations()
as to the returned data structure.- Parameters:
type
- the annotation type.- Returns:
- null if the specified annotation type is not included.
- See Also:
getAnnotations()
-
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.
-
removeAnnotation
public boolean removeAnnotation(java.lang.String type)
Removes an annotation by type. After removing an annotation, ifnumAnnotations()
returns 0, this annotations attribute has to be removed.- Parameters:
type
- of annotation to remove- Returns:
- whether an annotation with the given type has been removed
- Since:
- 3.21
-
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 bysetAnnotations()
.- See Also:
setAnnotations(Annotation[])
-
setAnnotations
public void setAnnotations(Annotation[] annotations)
Changes the annotations represented by this object according to the given array ofAnnotation
objects.- Parameters:
annotations
- the data structure representing the new annotations.
-
setAnnotation
public void setAnnotation(Annotation annotation)
Changes the annotations. A call to this method is equivalent to:setAnnotations(new Annotation[] { annotation })
- Parameters:
annotation
- the data structure representing the new annotation.
-
toString
public java.lang.String toString()
Returns a string representation of this object.- Overrides:
toString
in classjava.lang.Object
-
-