1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.bcel.generic;
20
21 import java.io.DataInput;
22 import java.io.DataOutputStream;
23 import java.io.IOException;
24
25 import org.apache.bcel.classfile.AnnotationElementValue;
26 import org.apache.bcel.classfile.AnnotationEntry;
27 import org.apache.bcel.classfile.ArrayElementValue;
28 import org.apache.bcel.classfile.ClassElementValue;
29 import org.apache.bcel.classfile.ElementValue;
30 import org.apache.bcel.classfile.EnumElementValue;
31 import org.apache.bcel.classfile.SimpleElementValue;
32
33
34
35
36
37
38 public abstract class ElementValueGen {
39
40
41 public static final int STRING = 's';
42
43
44 public static final int ENUM_CONSTANT = 'e';
45
46
47 public static final int CLASS = 'c';
48
49
50 public static final int ANNOTATION = '@';
51
52
53 public static final int ARRAY = '[';
54
55
56 public static final int PRIMITIVE_INT = 'I';
57
58
59 public static final int PRIMITIVE_BYTE = 'B';
60
61
62 public static final int PRIMITIVE_CHAR = 'C';
63
64
65 public static final int PRIMITIVE_DOUBLE = 'D';
66
67
68 public static final int PRIMITIVE_FLOAT = 'F';
69
70
71 public static final int PRIMITIVE_LONG = 'J';
72
73
74 public static final int PRIMITIVE_SHORT = 'S';
75
76
77 public static final int PRIMITIVE_BOOLEAN = 'Z';
78
79
80
81
82
83
84
85
86
87 public static ElementValueGen copy(final ElementValue value, final ConstantPoolGen cpool, final boolean copyPoolEntries) {
88 switch (value.getElementValueType()) {
89 case 'B':
90 case 'C':
91 case 'D':
92 case 'F':
93 case 'I':
94 case 'J':
95 case 'S':
96 case 'Z':
97 case 's':
98 return new SimpleElementValueGen((SimpleElementValue) value, cpool, copyPoolEntries);
99 case 'e':
100 return new EnumElementValueGen((EnumElementValue) value, cpool, copyPoolEntries);
101 case '@':
102 return new AnnotationElementValueGen((AnnotationElementValue) value, cpool, copyPoolEntries);
103 case '[':
104 return new ArrayElementValueGen((ArrayElementValue) value, cpool, copyPoolEntries);
105 case 'c':
106 return new ClassElementValueGen((ClassElementValue) value, cpool, copyPoolEntries);
107 default:
108 throw new UnsupportedOperationException("Not implemented yet! (" + value.getElementValueType() + ")");
109 }
110 }
111
112
113
114
115
116
117
118
119
120 public static ElementValueGen readElementValue(final DataInput dis, final ConstantPoolGen cpGen) throws IOException {
121 final int type = dis.readUnsignedByte();
122 switch (type) {
123 case 'B':
124 return new SimpleElementValueGen(PRIMITIVE_BYTE, dis.readUnsignedShort(), cpGen);
125 case 'C':
126 return new SimpleElementValueGen(PRIMITIVE_CHAR, dis.readUnsignedShort(), cpGen);
127 case 'D':
128 return new SimpleElementValueGen(PRIMITIVE_DOUBLE, dis.readUnsignedShort(), cpGen);
129 case 'F':
130 return new SimpleElementValueGen(PRIMITIVE_FLOAT, dis.readUnsignedShort(), cpGen);
131 case 'I':
132 return new SimpleElementValueGen(PRIMITIVE_INT, dis.readUnsignedShort(), cpGen);
133 case 'J':
134 return new SimpleElementValueGen(PRIMITIVE_LONG, dis.readUnsignedShort(), cpGen);
135 case 'S':
136 return new SimpleElementValueGen(PRIMITIVE_SHORT, dis.readUnsignedShort(), cpGen);
137 case 'Z':
138 return new SimpleElementValueGen(PRIMITIVE_BOOLEAN, dis.readUnsignedShort(), cpGen);
139 case 's':
140 return new SimpleElementValueGen(STRING, dis.readUnsignedShort(), cpGen);
141 case 'e':
142 return new EnumElementValueGen(dis.readUnsignedShort(), dis.readUnsignedShort(), cpGen);
143 case 'c':
144 return new ClassElementValueGen(dis.readUnsignedShort(), cpGen);
145 case '@':
146
147
148 return new AnnotationElementValueGen(ANNOTATION, new AnnotationEntryGen(AnnotationEntry.read(dis, cpGen.getConstantPool(), true), cpGen, false),
149 cpGen);
150 case '[':
151 final int numArrayVals = dis.readUnsignedShort();
152 final ElementValue[] evalues = new ElementValue[numArrayVals];
153 for (int j = 0; j < numArrayVals; j++) {
154 evalues[j] = ElementValue.readElementValue(dis, cpGen.getConstantPool());
155 }
156 return new ArrayElementValueGen(ARRAY, evalues, cpGen);
157 default:
158 throw new IllegalArgumentException("Unexpected element value kind in annotation: " + type);
159 }
160 }
161
162
163
164
165 @Deprecated
166 protected int type;
167
168
169
170
171 @Deprecated
172 protected ConstantPoolGen cpGen;
173
174
175
176
177
178
179
180 protected ElementValueGen(final int type, final ConstantPoolGen cpGen) {
181 this.type = type;
182 this.cpGen = cpGen;
183 }
184
185
186
187
188
189
190
191 public abstract void dump(DataOutputStream dos) throws IOException;
192
193
194
195
196
197
198 protected ConstantPoolGen getConstantPool() {
199 return cpGen;
200 }
201
202
203
204
205
206
207 public abstract ElementValue getElementValue();
208
209
210
211
212
213
214 public int getElementValueType() {
215 return type;
216 }
217
218
219
220
221
222
223 public abstract String stringifyValue();
224 }