1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.bcel.classfile;
20
21 import java.io.DataOutputStream;
22 import java.io.IOException;
23
24 import org.apache.bcel.Const;
25
26
27
28
29
30
31 public class SimpleElementValue extends ElementValue {
32 private int index;
33
34
35
36
37
38
39
40
41 public SimpleElementValue(final int type, final int index, final ConstantPool cpool) {
42 super(type, cpool);
43 this.index = index;
44 }
45
46 @Override
47 public void dump(final DataOutputStream dos) throws IOException {
48 final int type = super.getType();
49 dos.writeByte(type);
50 switch (type) {
51 case PRIMITIVE_INT:
52 case PRIMITIVE_BYTE:
53 case PRIMITIVE_CHAR:
54 case PRIMITIVE_FLOAT:
55 case PRIMITIVE_LONG:
56 case PRIMITIVE_BOOLEAN:
57 case PRIMITIVE_SHORT:
58 case PRIMITIVE_DOUBLE:
59 case STRING:
60 dos.writeShort(getIndex());
61 break;
62 default:
63 throw new ClassFormatException("SimpleElementValue doesn't know how to write out type " + type);
64 }
65 }
66
67
68
69
70
71
72 public int getIndex() {
73 return index;
74 }
75
76
77
78
79
80
81 public boolean getValueBoolean() {
82 if (super.getType() != PRIMITIVE_BOOLEAN) {
83 throw new IllegalStateException("Don't call getValueBoolean() on a non BOOLEAN ElementValue");
84 }
85 final ConstantInteger bo = (ConstantInteger) super.getConstantPool().getConstant(getIndex());
86 return bo.getBytes() != 0;
87 }
88
89
90
91
92
93
94 public byte getValueByte() {
95 if (super.getType() != PRIMITIVE_BYTE) {
96 throw new IllegalStateException("Don't call getValueByte() on a non BYTE ElementValue");
97 }
98 return (byte) super.getConstantPool().getConstantInteger(getIndex()).getBytes();
99 }
100
101
102
103
104
105
106 public char getValueChar() {
107 if (super.getType() != PRIMITIVE_CHAR) {
108 throw new IllegalStateException("Don't call getValueChar() on a non CHAR ElementValue");
109 }
110 return (char) super.getConstantPool().getConstantInteger(getIndex()).getBytes();
111 }
112
113
114
115
116
117
118 public double getValueDouble() {
119 if (super.getType() != PRIMITIVE_DOUBLE) {
120 throw new IllegalStateException("Don't call getValueDouble() on a non DOUBLE ElementValue");
121 }
122 final ConstantDouble d = (ConstantDouble) super.getConstantPool().getConstant(getIndex());
123 return d.getBytes();
124 }
125
126
127
128
129
130
131 public float getValueFloat() {
132 if (super.getType() != PRIMITIVE_FLOAT) {
133 throw new IllegalStateException("Don't call getValueFloat() on a non FLOAT ElementValue");
134 }
135 final ConstantFloat f = (ConstantFloat) super.getConstantPool().getConstant(getIndex());
136 return f.getBytes();
137 }
138
139
140
141
142
143
144 public int getValueInt() {
145 if (super.getType() != PRIMITIVE_INT) {
146 throw new IllegalStateException("Don't call getValueInt() on a non INT ElementValue");
147 }
148 return super.getConstantPool().getConstantInteger(getIndex()).getBytes();
149 }
150
151
152
153
154
155
156 public long getValueLong() {
157 if (super.getType() != PRIMITIVE_LONG) {
158 throw new IllegalStateException("Don't call getValueLong() on a non LONG ElementValue");
159 }
160 final ConstantLong j = (ConstantLong) super.getConstantPool().getConstant(getIndex());
161 return j.getBytes();
162 }
163
164
165
166
167
168
169 public short getValueShort() {
170 if (super.getType() != PRIMITIVE_SHORT) {
171 throw new IllegalStateException("Don't call getValueShort() on a non SHORT ElementValue");
172 }
173 final ConstantInteger s = (ConstantInteger) super.getConstantPool().getConstant(getIndex());
174 return (short) s.getBytes();
175 }
176
177
178
179
180
181
182 public String getValueString() {
183 if (super.getType() != STRING) {
184 throw new IllegalStateException("Don't call getValueString() on a non STRING ElementValue");
185 }
186 return super.getConstantPool().getConstantUtf8(getIndex()).getBytes();
187 }
188
189
190
191
192
193
194 public void setIndex(final int index) {
195 this.index = index;
196 }
197
198
199 @Override
200 public String stringifyValue() {
201 final ConstantPool cpool = super.getConstantPool();
202 final int type = super.getType();
203 switch (type) {
204 case PRIMITIVE_INT:
205 return Integer.toString(cpool.getConstantInteger(getIndex()).getBytes());
206 case PRIMITIVE_LONG:
207 final ConstantLong j = cpool.getConstant(getIndex(), Const.CONSTANT_Long, ConstantLong.class);
208 return Long.toString(j.getBytes());
209 case PRIMITIVE_DOUBLE:
210 final ConstantDouble d = cpool.getConstant(getIndex(), Const.CONSTANT_Double, ConstantDouble.class);
211 return Double.toString(d.getBytes());
212 case PRIMITIVE_FLOAT:
213 final ConstantFloat f = cpool.getConstant(getIndex(), Const.CONSTANT_Float, ConstantFloat.class);
214 return Float.toString(f.getBytes());
215 case PRIMITIVE_SHORT:
216 final ConstantInteger s = cpool.getConstantInteger(getIndex());
217 return Integer.toString(s.getBytes());
218 case PRIMITIVE_BYTE:
219 final ConstantInteger b = cpool.getConstantInteger(getIndex());
220 return Integer.toString(b.getBytes());
221 case PRIMITIVE_CHAR:
222 final ConstantInteger ch = cpool.getConstantInteger(getIndex());
223 return String.valueOf((char) ch.getBytes());
224 case PRIMITIVE_BOOLEAN:
225 final ConstantInteger bo = cpool.getConstantInteger(getIndex());
226 if (bo.getBytes() == 0) {
227 return "false";
228 }
229 return "true";
230 case STRING:
231 return cpool.getConstantUtf8(getIndex()).getBytes();
232 default:
233 throw new IllegalStateException("SimpleElementValue class does not know how to stringify type " + type);
234 }
235 }
236
237 @Override
238 public String toString() {
239 return stringifyValue();
240 }
241 }