1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
|
/* *******************************************************************
* Copyright (c) 2004 IBM Corporation
*
* All rights reserved.
* This program and the accompanying materials are made available
* under the terms of the Common Public License v1.0
* which accompanies this distribution and is available at
* http://www.eclipse.org/legal/cpl-v10.html
*
* Contributors:
* Andy Clement initial implementation
* ******************************************************************/
package org.aspectj.apache.bcel.generic.annotation;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.aspectj.apache.bcel.classfile.ConstantUtf8;
import org.aspectj.apache.bcel.classfile.annotation.Annotation;
import org.aspectj.apache.bcel.classfile.annotation.ElementNameValuePair;
import org.aspectj.apache.bcel.generic.ConstantPoolGen;
import org.aspectj.apache.bcel.generic.ObjectType;
public class AnnotationGen {
private int typeIndex;
private List /* ElementNameValuePairGen */ evs;
private ConstantPoolGen cpool;
private boolean isRuntimeVisible = false;
/**
* Here we are taking a fixed annotation of type Annotation and building a
* modifiable AnnotationGen object. If the pool passed in is for a different
* class file, then copyPoolEntries should have been passed as true as that
* will force us to do a deep copy of the annotation and move the cpool entries
* across.
* We need to copy the type and the element name value pairs and the visibility.
*/
public AnnotationGen(Annotation a,ConstantPoolGen cpool,boolean copyPoolEntries) {
this.cpool = cpool;
if (copyPoolEntries) {
typeIndex = cpool.addUtf8(a.getTypeSignature());
} else {
typeIndex = a.getTypeIndex();
}
isRuntimeVisible = a.isRuntimeVisible();
evs = copyValues(a.getValues(),cpool,copyPoolEntries);
}
private List copyValues(List in,ConstantPoolGen cpool,boolean copyPoolEntries) {
List out = new ArrayList();
for (Iterator iter = in.iterator(); iter.hasNext();) {
ElementNameValuePair nvp = (ElementNameValuePair) iter.next();
out.add(new ElementNameValuePairGen(nvp,cpool,copyPoolEntries));
}
return out;
}
private AnnotationGen(ConstantPoolGen cpool) {
this.cpool = cpool;
}
/**
* Retrieve an immutable version of this AnnotationGen
*/
public Annotation getAnnotation() {
Annotation a = new Annotation(typeIndex,cpool.getConstantPool(),isRuntimeVisible);
for (Iterator iter = evs.iterator(); iter.hasNext();) {
ElementNameValuePairGen element = (ElementNameValuePairGen) iter.next();
a.addElementNameValuePair(element.getElementNameValuePair());
}
return a;
}
public AnnotationGen(ObjectType type,List /*ElementNameValuePairGen*/ elements,boolean vis,ConstantPoolGen cpool) {
this.cpool = cpool;
this.typeIndex = cpool.addUtf8(type.getSignature());
evs = elements;
isRuntimeVisible = vis;
}
public static AnnotationGen read(DataInputStream dis,ConstantPoolGen cpool,boolean b) throws IOException {
AnnotationGen a = new AnnotationGen(cpool);
a.typeIndex = dis.readUnsignedShort();
int elemValuePairCount = dis.readUnsignedShort();
for (int i=0;i<elemValuePairCount;i++) {
int nidx = dis.readUnsignedShort();
a.addElementNameValuePair(
new ElementNameValuePairGen(nidx,ElementValueGen.readElementValue(dis,cpool),cpool));
}
a.isRuntimeVisible(b);
return a;
}
public void dump(DataOutputStream dos) throws IOException {
dos.writeShort(typeIndex); // u2 index of type name in cpool
dos.writeShort(evs.size()); // u2 element_value pair count
for (int i = 0 ; i<evs.size();i++) {
ElementNameValuePairGen envp = (ElementNameValuePairGen) evs.get(i);
envp.dump(dos);
}
}
public void addElementNameValuePair(ElementNameValuePairGen evp) {
if (evs == null) evs = new ArrayList();
evs.add(evp);
}
public int getTypeIndex() {
return typeIndex;
}
public final String getTypeSignature() {
// ConstantClass c = (ConstantClass)cpool.getConstant(typeIndex);
ConstantUtf8 utf8 = (ConstantUtf8)cpool.getConstant(typeIndex/*c.getNameIndex()*/);
return utf8.getBytes();
}
public final String getTypeName() {
return getTypeSignature();// BCELBUG: Should I use this instead? Utility.signatureToString(getTypeSignature());
}
/**
* Returns list of ElementNameValuePair objects
*/
public List getValues() {
return evs;
}
public String toString() {
StringBuffer s = new StringBuffer();
s.append("AnnotationGen:["+getTypeName()+" #"+evs.size()+" {");
for (int i = 0; i<evs.size();i++) {
s.append(evs.get(i));
if (i+1<evs.size()) s.append(",");
}
s.append("}]");
return s.toString();
}
public String toShortString() {
StringBuffer s = new StringBuffer();
s.append("@"+getTypeName()+"(");
for (int i = 0; i<evs.size();i++) {
s.append(evs.get(i));
if (i+1<evs.size()) s.append(",");
}
s.append(")");
return s.toString();
}
private void isRuntimeVisible(boolean b) {
isRuntimeVisible = b;
}
public boolean isRuntimeVisible() {
return isRuntimeVisible;
}
}
|