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