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, javassist.bytecode.annotation.Annotation
| Modifier and Type | Class and Description |
|---|---|
| pack-priv static class | |
| pack-priv static class | |
| pack-priv static class | |
| pack-priv static class |
| Modifier and Type | Field and Description |
|---|---|
| public static final String | invisibleTag
The name of the |
| public static final String | visibleTag
The name of the |
| Access | Constructor and Description |
|---|---|
| public | AnnotationsAttribute(ConstPool
constant pool cp, String attribute name ( attrname, byte[] visibleTag or
invisibleTag).the contents of this attribute. It does not
include info)attribute_name_index or
attribute_length.Constructs a |
| public | AnnotationsAttribute(ConstPool
constant pool cp, String attribute name ( attrname)visibleTag or
invisibleTag).Constructs an empty
|
| pack-priv |
| Modifier and Type | Method and Description |
|---|---|
| public void | |
| public AttributeInfo | |
| public Annotation | Returns: null if the specified annotation type is not included.the annotation type. type)Parses the annotations and returns a data structure representing the annotation with the specified type. |
| public Annotation[] | getAnnotations()
Parses the annotations and returns a data structure representing that parsed annotations. |
| pack-priv void | getRefClasses(Map<String, String> classnames)
Overrides javassist.
|
| public int | |
| public boolean | Returns: whether an annotation with the given type has been removedof annotation to remove type)Removes an annotation by type. |
| pack-priv void | renameClass(String
a JVM class name. oldname, String a JVM class name. newname)Overrides javassist.
|
| pack-priv void | |
| public void | setAnnotation(Annotation
the data structure representing
the new annotation. annotation)Changes the annotations. |
| public void | setAnnotations(Annotation[]
the data structure representing the
new annotations. annotations)Changes the annotations represented by this object according to
the given array of |
| public String |
| invisibleTag | back to summary |
|---|---|
| public static final String invisibleTag The name of the | |
| visibleTag | back to summary |
|---|---|
| public static final String visibleTag The name of the | |
| AnnotationsAttribute | back to summary |
|---|---|
| public AnnotationsAttribute(ConstPool cp, String attrname, byte[] info) Constructs a | |
| AnnotationsAttribute | back to summary |
|---|---|
| public AnnotationsAttribute(ConstPool cp, String attrname) Constructs an empty
| |
| AnnotationsAttribute | back to summary |
|---|---|
pack-priv AnnotationsAttribute(ConstPool cp, int n, DataInputStream in) throws IOException
| |
| addAnnotation | back to summary |
|---|---|
| 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.
| |
| copy | back to summary |
|---|---|
| public AttributeInfo copy(ConstPool newCp, Map<String, String> classnames) Overrides javassist. Copies this attribute and returns a new copy.
| |
| getAnnotation | back to summary |
|---|---|
| public Annotation getAnnotation(String type) Parses the annotations and returns a data structure representing
the annotation with the specified type. See also
| |
| getAnnotations | back to summary |
|---|---|
| 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
| |
| getRefClasses | back to summary |
|---|---|
| pack-priv void getRefClasses(Map<String, String> classnames) Overrides javassist.
| |
| numAnnotations | back to summary |
|---|---|
| public int numAnnotations() Returns | |
| removeAnnotation | back to summary |
|---|---|
| public boolean removeAnnotation(String type) Removes an annotation by type.
After removing an annotation, if
| |
| renameClass | back to summary |
|---|---|
| pack-priv void renameClass(String oldname, String newname) Overrides javassist. | |
| renameClass | back to summary |
|---|---|
| pack-priv void renameClass(Map<String, String> classnames) Overrides javassist.
| |
| setAnnotation | back to summary |
|---|---|
| public void setAnnotation(Annotation annotation) Changes the annotations. A call to this method is equivalent to: setAnnotations(new Annotation[] { annotation })
| |
| setAnnotations | back to summary |
|---|---|
| public void setAnnotations(Annotation[] annotations) Changes the annotations represented by this object according to
the given array of
| |
| toString | back to summary |
|---|---|
| public String toString() Overrides java. Returns a string representation of this object.
| |
| Modifier and Type | Field and Description |
|---|---|
| pack-priv Map | |
| pack-priv ConstPool | |
| pack-priv ByteArrayOutputStream | |
| pack-priv ConstPool | |
| pack-priv AnnotationsWriter |
| Access | Constructor and Description |
|---|---|
| pack-priv | |
| pack-priv |
| Modifier and Type | Method and Description |
|---|---|
| pack-priv int | annotation(int pos, int type, int numPairs)
Overrides javassist.
|
| pack-priv int | annotationArray(int pos, int num)
Overrides javassist.
|
| pack-priv int | annotationMemberValue(int pos)
Overrides javassist.
|
| pack-priv int | arrayMemberValue(int pos, int num)
Overrides javassist.
|
| pack-priv void | classMemberValue(int pos, int index)
Overrides javassist.
|
| pack-priv byte[] | |
| pack-priv void | constValueMember(int tag, int index)
Overrides javassist.
|
| pack-priv int | Returns: the index of the copied item into the destination constant pool.the index of the copied entry into the source
constant pool. srcIndex)Copies a constant pool entry into the destination constant pool and returns the index of the copied entry. |
| pack-priv int | Returns: the index of the copied item into the destination constant pool.the index of the copied entry into the source
constant pool. srcIndex)Copies a constant pool entry into the destination constant pool and returns the index of the copied entry. |
| pack-priv void | enumMemberValue(int pos, int typeNameIndex, int constNameIndex)
Overrides javassist.
|
| pack-priv int | memberValuePair(int pos, int nameIndex)
Overrides javassist.
|
| pack-priv void | parameters(int numParam, int pos)
Overrides javassist.
|
| classnames | back to summary |
|---|---|
| pack-priv Map<String, String> classnames | |
| destPool | back to summary |
|---|---|
| pack-priv ConstPool destPool | |
| output | back to summary |
|---|---|
| pack-priv ByteArrayOutputStream output | |
| srcPool | back to summary |
|---|---|
| pack-priv ConstPool srcPool | |
| writer | back to summary |
|---|---|
| pack-priv AnnotationsWriter writer | |
| Copier | back to summary |
|---|---|
| pack-priv Copier(byte[] info, ConstPool src, ConstPool dest, Map<String, String> map) Constructs a copier. This copier renames some class names
into the new names specified by | |
| Copier | back to summary |
|---|---|
| pack-priv Copier(byte[] info, ConstPool src, ConstPool dest, Map<String, String> map, boolean makeWriter) | |
| annotation | back to summary |
|---|---|
| pack-priv int annotation(int pos, int type, int numPairs) throws Exception Overrides javassist.
| |
| annotationArray | back to summary |
|---|---|
| pack-priv int annotationArray(int pos, int num) throws Exception Overrides javassist.
| |
| annotationMemberValue | back to summary |
|---|---|
| pack-priv int annotationMemberValue(int pos) throws Exception Overrides javassist. Doc from javassist.
| |
| arrayMemberValue | back to summary |
|---|---|
| pack-priv int arrayMemberValue(int pos, int num) throws Exception Overrides javassist. Doc from javassist.
| |
| classMemberValue | back to summary |
|---|---|
| pack-priv void classMemberValue(int pos, int index) throws Exception Overrides javassist. Doc from javassist.
| |
| close | back to summary |
|---|---|
| pack-priv byte[] close() throws IOException | |
| constValueMember | back to summary |
|---|---|
| pack-priv void constValueMember(int tag, int index) throws Exception Overrides javassist. Doc from javassist.
| |
| copy | back to summary |
|---|---|
| pack-priv int copy(int srcIndex) Copies a constant pool entry into the destination constant pool and returns the index of the copied entry.
| |
| copyType | back to summary |
|---|---|
| pack-priv int copyType(int srcIndex) Copies a constant pool entry into the destination constant pool
and returns the index of the copied entry. That entry must be
a Utf8Info representing a class name in the L the index of the copied entry into the source
constant pool.
the index of the copied item into the destination
constant pool.
| |
| enumMemberValue | back to summary |
|---|---|
| pack-priv void enumMemberValue(int pos, int typeNameIndex, int constNameIndex) throws Exception Overrides javassist. Doc from javassist.
| |
| memberValuePair | back to summary |
|---|---|
| pack-priv int memberValuePair(int pos, int nameIndex) throws Exception Overrides javassist. Doc from javassist.
| |
| parameters | back to summary |
|---|---|
| pack-priv void parameters(int numParam, int pos) throws Exception Overrides javassist.
| |
| Modifier and Type | Field and Description |
|---|---|
| pack-priv Annotation[] | |
| pack-priv Annotation[][] | |
| pack-priv Annotation | |
| pack-priv MemberValue | |
| pack-priv ConstPool |
| Access | Constructor and Description |
|---|---|
| pack-priv |
| allAnno | back to summary |
|---|---|
| pack-priv Annotation[] allAnno | |
| allParams | back to summary |
|---|---|
| pack-priv Annotation[][] allParams | |
| currentAnno | back to summary |
|---|---|
| pack-priv Annotation currentAnno | |
| currentMember | back to summary |
|---|---|
| pack-priv MemberValue currentMember | |
| pool | back to summary |
|---|---|
| pack-priv ConstPool pool | |
| Parser | back to summary |
|---|---|
| pack-priv Parser(byte[] info, ConstPool cp) Constructs a parser. This parser constructs a parse tree of the annotations.
| |
| annotation | back to summary |
|---|---|
| pack-priv int annotation(int pos, int type, int numPairs) throws Exception Overrides javassist.
| |
| annotationArray | back to summary |
|---|---|
| pack-priv int annotationArray(int pos, int num) throws Exception Overrides javassist.
| |
| annotationMemberValue | back to summary |
|---|---|
| pack-priv int annotationMemberValue(int pos) throws Exception Overrides javassist. Doc from javassist.
| |
| arrayMemberValue | back to summary |
|---|---|
| pack-priv int arrayMemberValue(int pos, int num) throws Exception Overrides javassist. Doc from javassist.
| |
| classMemberValue | back to summary |
|---|---|
| pack-priv void classMemberValue(int pos, int index) throws Exception Overrides javassist. Doc from javassist.
| |
| constValueMember | back to summary |
|---|---|
| pack-priv void constValueMember(int tag, int index) throws Exception Overrides javassist. Doc from javassist.
| |
| enumMemberValue | back to summary |
|---|---|
| pack-priv void enumMemberValue(int pos, int typeNameIndex, int constNameIndex) throws Exception Overrides javassist. Doc from javassist.
| |
| memberValuePair | back to summary |
|---|---|
| pack-priv int memberValuePair(int pos, int nameIndex) throws Exception Overrides javassist. Doc from javassist.
| |
| parameters | back to summary |
|---|---|
| pack-priv void parameters(int numParam, int pos) throws Exception Overrides javassist.
| |
| parseAnnotations | back to summary |
|---|---|
| pack-priv Annotation[] parseAnnotations() throws Exception | |
| parseMemberValue | back to summary |
|---|---|
| pack-priv MemberValue parseMemberValue() throws Exception | |
| parseParameters | back to summary |
|---|---|
| pack-priv Annotation[][] parseParameters() throws Exception | |
| Modifier and Type | Field and Description |
|---|---|
| pack-priv Map | |
| pack-priv ConstPool |
| Access | Constructor and Description |
|---|---|
| pack-priv |
| Modifier and Type | Method and Description |
|---|---|
| pack-priv int | annotation(int pos, int type, int numPairs)
Overrides javassist.
|
| pack-priv void | classMemberValue(int pos, int index)
Overrides javassist.
|
| pack-priv void | enumMemberValue(int pos, int typeNameIndex, int constNameIndex)
Overrides javassist.
|
| private void |
| classnames | back to summary |
|---|---|
| pack-priv Map<String, String> classnames | |
| cpool | back to summary |
|---|---|
| pack-priv ConstPool cpool | |
| Renamer | back to summary |
|---|---|
| pack-priv Renamer(byte[] info, ConstPool cp, Map<String, String> map) Constructs a renamer. It renames some class names
into the new names specified by | |
| annotation | back to summary |
|---|---|
| pack-priv int annotation(int pos, int type, int numPairs) throws Exception Overrides javassist.
| |
| classMemberValue | back to summary |
|---|---|
| pack-priv void classMemberValue(int pos, int index) throws Exception Overrides javassist. Doc from javassist.
| |
| enumMemberValue | back to summary |
|---|---|
| pack-priv void enumMemberValue(int pos, int typeNameIndex, int constNameIndex) throws Exception Overrides javassist. Doc from javassist.
| |
| renameType | back to summary |
|---|---|
| private void renameType(int pos, int index) | |
| Modifier and Type | Field and Description |
|---|---|
| pack-priv byte[] |
| Access | Constructor and Description |
|---|---|
| pack-priv |
| Modifier and Type | Method and Description |
|---|---|
| pack-priv final int | |
| pack-priv int | |
| pack-priv final void | |
| pack-priv final int | |
| pack-priv int | |
| pack-priv int | |
| pack-priv int | |
| pack-priv void | |
| pack-priv void | |
| pack-priv void | |
| pack-priv final int | |
| pack-priv final int | |
| pack-priv int | |
| pack-priv final void | |
| pack-priv void |
| info | back to summary |
|---|---|
| pack-priv byte[] info | |
| Walker | back to summary |
|---|---|
| pack-priv Walker(byte[] attrInfo) | |
| annotation | back to summary |
|---|---|
| pack-priv final int annotation(int pos) throws Exception | |
| annotation | back to summary |
|---|---|
| pack-priv int annotation(int pos, int type, int numPairs) throws Exception | |
| annotationArray | back to summary |
|---|---|
| pack-priv final void annotationArray() throws Exception | |
| annotationArray | back to summary |
|---|---|
| pack-priv final int annotationArray(int pos) throws Exception | |
| annotationArray | back to summary |
|---|---|
| pack-priv int annotationArray(int pos, int num) throws Exception | |
| annotationMemberValue | back to summary |
|---|---|
| pack-priv int annotationMemberValue(int pos) throws Exception
| |
| arrayMemberValue | back to summary |
|---|---|
| pack-priv int arrayMemberValue(int pos, int num) throws Exception
| |
| classMemberValue | back to summary |
|---|---|
| pack-priv void classMemberValue(int pos, int index) throws Exception
| |
| constValueMember | back to summary |
|---|---|
| pack-priv void constValueMember(int tag, int index) throws Exception
| |
| enumMemberValue | back to summary |
|---|---|
| pack-priv void enumMemberValue(int pos, int typeNameIndex, int constNameIndex) throws Exception
| |
| memberValue | back to summary |
|---|---|
| pack-priv final int memberValue(int pos) throws Exception
| |
| memberValuePair | back to summary |
|---|---|
| pack-priv final int memberValuePair(int pos) throws Exception
| |
| memberValuePair | back to summary |
|---|---|
| pack-priv int memberValuePair(int pos, int nameIndex) throws Exception
| |
| parameters | back to summary |
|---|---|
| pack-priv final void parameters() throws Exception | |
| parameters | back to summary |
|---|---|
| pack-priv void parameters(int numParam, int pos) throws Exception | |