1
/* *******************************************************************
2
* Copyright (c) 2004 IBM Corporation
5
* This program and the accompanying materials are made available
6
* under the terms of the Eclipse Public License v1.0
7
* which accompanies this distribution and is available at
8
* http://www.eclipse.org/legal/epl-v10.html
11
* Andy Clement initial implementation
12
* ******************************************************************/
13
package org.aspectj.apache.bcel.classfile.annotation;
15
import java.io.DataInputStream;
16
import java.io.DataOutputStream;
17
import java.io.IOException;
18
import java.util.ArrayList;
19
import java.util.Iterator;
20
import java.util.List;
22
import org.aspectj.apache.bcel.Constants;
23
import org.aspectj.apache.bcel.classfile.ConstantPool;
24
import org.aspectj.apache.bcel.classfile.ConstantUtf8;
25
import org.aspectj.apache.bcel.classfile.Utility;
28
* An annotation is an immutable object (AnnotationGen is the mutable variant) - it basically contains a list
29
* of name-value pairs.
31
public class Annotation {
32
private int typeIndex;
33
private List /* ElementNameValuePair */ evs = new ArrayList();
34
private ConstantPool cpool;
35
private boolean isRuntimeVisible;
37
public String toString() {
38
StringBuffer sb = new StringBuffer();
39
sb.append("ANNOTATION ["+getTypeSignature()+"] ["+
40
(isRuntimeVisible?"runtimeVisible":"runtimeInvisible")+"] [");
41
for (Iterator iter = evs.iterator(); iter.hasNext();) {
42
ElementNameValuePair element = (ElementNameValuePair) iter.next();
43
sb.append(element.toString());
44
if (iter.hasNext()) sb.append(",");
50
private Annotation(ConstantPool cpool) {
54
public Annotation(int index,ConstantPool cpool,boolean visible) {
56
this.typeIndex = index;
57
this.isRuntimeVisible = visible;
60
protected static Annotation read(DataInputStream dis,ConstantPool cpool,boolean isRuntimeVisible) throws IOException {
61
Annotation a = new Annotation(cpool);
62
a.typeIndex = dis.readUnsignedShort();
63
int elemValuePairCount = dis.readUnsignedShort();
64
for (int i=0;i<elemValuePairCount;i++) {
65
int nidx = dis.readUnsignedShort();
66
a.addElementNameValuePair(
67
new ElementNameValuePair(nidx,ElementValue.readElementValue(dis,cpool),cpool));
69
a.isRuntimeVisible(isRuntimeVisible);
73
protected void dump(DataOutputStream dos) throws IOException {
74
dos.writeShort(typeIndex); // u2 index of type name in cpool
75
dos.writeShort(evs.size()); // u2 element_value pair count
76
for (int i = 0 ; i<evs.size();i++) {
77
ElementNameValuePair envp = (ElementNameValuePair) evs.get(i);
82
public void addElementNameValuePair(ElementNameValuePair evp) {
87
public int getTypeIndex() {
91
public String getTypeSignature() {
92
ConstantUtf8 c = (ConstantUtf8)cpool.getConstant(typeIndex,Constants.CONSTANT_Utf8);
96
public String getTypeName() {
97
ConstantUtf8 c = (ConstantUtf8)cpool.getConstant(typeIndex,Constants.CONSTANT_Utf8);
98
return Utility.signatureToString(c.getBytes());
102
* Returns list of ElementNameValuePair objects
104
public List getValues() {
108
protected void isRuntimeVisible(boolean b) {
109
isRuntimeVisible = b;
112
public boolean isRuntimeVisible() {
113
return isRuntimeVisible;
116
public String toShortString() {
117
StringBuffer result = new StringBuffer();
119
result.append(getTypeName());
120
if (getValues().size()>0) {
122
for (Iterator iter = getValues().iterator(); iter.hasNext();) {
123
ElementNameValuePair element = (ElementNameValuePair) iter.next();
124
result.append(element.toShortString());
128
return result.toString();