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