public class AnnotationsAttribute extends AttributeInfo
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,
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 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:
@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 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:
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.
AnnotationDefaultAttribute
,
Annotation
Modifier and Type | Field and Description |
---|---|
static java.lang.String |
invisibleTag
The name of the
RuntimeInvisibleAnnotations attribute. |
static java.lang.String |
visibleTag
The name of the
RuntimeVisibleAnnotations attribute. |
Constructor and Description |
---|
AnnotationsAttribute(ConstPool cp,
java.lang.String attrname)
Constructs an empty
Runtime(In)VisibleAnnotations_attribute . |
AnnotationsAttribute(ConstPool cp,
java.lang.String attrname,
byte[] info)
Constructs a
Runtime(In)VisibleAnnotations_attribute . |
Modifier and Type | Method and Description |
---|---|
void |
addAnnotation(Annotation annotation)
Adds an annotation.
|
AttributeInfo |
copy(ConstPool newCp,
java.util.Map 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()
Returns
num_annotations . |
void |
setAnnotation(Annotation annotation)
Changes the annotations.
|
void |
setAnnotations(Annotation[] annotations)
Changes the annotations represented by this object according to
the given array of
Annotation objects. |
java.lang.String |
toString()
Returns a string representation of this object.
|
get, getConstPool, getName, length, set
public static final java.lang.String visibleTag
RuntimeVisibleAnnotations
attribute.public static final java.lang.String invisibleTag
RuntimeInvisibleAnnotations
attribute.public AnnotationsAttribute(ConstPool cp, java.lang.String attrname, byte[] info)
Runtime(In)VisibleAnnotations_attribute
.cp
- constant poolattrname
- attribute name (visibleTag
or
invisibleTag
).info
- the contents of this attribute. It does not
include attribute_name_index
or
attribute_length
.public AnnotationsAttribute(ConstPool cp, java.lang.String attrname)
Runtime(In)VisibleAnnotations_attribute
.
A new annotation can be later added to the created attribute
by setAnnotations()
.cp
- constant poolattrname
- attribute name (visibleTag
or
invisibleTag
).setAnnotations(Annotation[])
public int numAnnotations()
num_annotations
.public AttributeInfo copy(ConstPool newCp, java.util.Map classnames)
copy
in class AttributeInfo
newCp
- the constant pool table used by the new copy.classnames
- pairs of replaced and substituted
class names.public Annotation getAnnotation(java.lang.String type)
getAnnotations()
as to the returned data structure.type
- the annotation type.getAnnotations()
public void addAnnotation(Annotation annotation)
annotation
- the added annotation.public Annotation[] getAnnotations()
setAnnotations()
.setAnnotations(Annotation[])
public void setAnnotations(Annotation[] annotations)
Annotation
objects.annotations
- the data structure representing the
new annotations.public void setAnnotation(Annotation annotation)
setAnnotations(new Annotation[] { annotation })
annotation
- the data structure representing
the new annotation.public java.lang.String toString()
toString
in class java.lang.Object
Javassist, a Java-bytecode translator toolkit.
Copyright (C) 1999- Shigeru Chiba. All Rights Reserved.