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
167
168
169
170
171
172
173
174
175
|
/* *******************************************************************
* Copyright (c) 2004, 2013 IBM Corporation
*
* All rights reserved.
* This program and the accompanying materials are made available
* under the terms of the Eclipse Public License v 2.0
* which accompanies this distribution and is available at
* https://www.eclipse.org/org/documents/epl-2.0/EPL-2.0.txt
*
* Contributors:
* Andy Clement initial implementation
* ******************************************************************/
package org.aspectj.apache.bcel.classfile.annotation;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.aspectj.apache.bcel.classfile.ConstantPool;
import org.aspectj.apache.bcel.classfile.ConstantUtf8;
import org.aspectj.apache.bcel.classfile.Utility;
import org.aspectj.apache.bcel.generic.ObjectType;
public class AnnotationGen {
public static final AnnotationGen[] NO_ANNOTATIONS = new AnnotationGen[0];
private int typeIndex;
private List<NameValuePair> pairs = Collections.emptyList();
private ConstantPool cpool;
private boolean isRuntimeVisible = false;
public AnnotationGen(AnnotationGen a, ConstantPool cpool, boolean copyPoolEntries) {
this.cpool = cpool;
if (copyPoolEntries) {
typeIndex = cpool.addUtf8(a.getTypeSignature());
} else {
typeIndex = a.getTypeIndex();
}
isRuntimeVisible = a.isRuntimeVisible();
pairs = copyValues(a.getValues(), cpool, copyPoolEntries);
}
private List<NameValuePair> copyValues(List<NameValuePair> in, ConstantPool cpool, boolean copyPoolEntries) {
List<NameValuePair> out = new ArrayList<>();
for (NameValuePair nvp : in) {
out.add(new NameValuePair(nvp, cpool, copyPoolEntries));
}
return out;
}
private AnnotationGen(ConstantPool cpool) {
this.cpool = cpool;
}
/**
* Retrieve an immutable version of this AnnotationGen
*/
public AnnotationGen(ObjectType type, List<NameValuePair> pairs, boolean runtimeVisible, ConstantPool cpool) {
this.cpool = cpool;
if (type != null) {
this.typeIndex = cpool.addUtf8(type.getSignature()); // Only null for funky *temporary* FakeAnnotation objects
}
this.pairs = pairs;
isRuntimeVisible = runtimeVisible;
}
public static AnnotationGen read(DataInputStream dis, ConstantPool 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 NameValuePair(nidx, ElementValue.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(pairs.size()); // u2 element_value pair count
for (NameValuePair envp : pairs) {
envp.dump(dos);
}
}
public void addElementNameValuePair(NameValuePair evp) {
if (pairs == Collections.EMPTY_LIST) {
pairs = new ArrayList<>();
}
pairs.add(evp);
}
public int getTypeIndex() {
return typeIndex;
}
public String getTypeSignature() {
ConstantUtf8 utf8 = (ConstantUtf8) cpool.getConstant(typeIndex);
return utf8.getValue();
}
public String getTypeName() {
return Utility.signatureToString(getTypeSignature());
}
public List<NameValuePair> getValues() {
return pairs;
}
@Override
public String toString() {
StringBuilder s = new StringBuilder();
s.append("AnnotationGen:[" + getTypeName() + " #" + pairs.size() + " {");
for (int i = 0; i < pairs.size(); i++) {
s.append(pairs.get(i));
if (i + 1 < pairs.size())
s.append(",");
}
s.append("}]");
return s.toString();
}
public String toShortString() {
StringBuilder s = new StringBuilder();
s.append("@").append(getTypeName());
if (pairs.size()!=0) {
s.append("(");
for (int i = 0; i < pairs.size(); i++) {
s.append(pairs.get(i));
if (i + 1 < pairs.size())
s.append(",");
}
s.append(")");
}
return s.toString();
}
private void isRuntimeVisible(boolean b) {
isRuntimeVisible = b;
}
public boolean isRuntimeVisible() {
return isRuntimeVisible;
}
/**
* @return true if the annotation has a value with the specified name and (toString'd) value
*/
public boolean hasNameValuePair(String name, String value) {
for (NameValuePair pair : pairs) {
if (pair.getNameString().equals(name)) {
if (pair.getValue().stringifyValue().equals(value)) {
return true;
}
}
}
return false;
}
/**
* @return true if the annotation has a value with the specified name
*/
public boolean hasNamedValue(String name) {
for (NameValuePair pair : pairs) {
if (pair.getNameString().equals(name)) {
return true;
}
}
return false;
}
}
|