1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.bcel.classfile;
19
20 import java.io.DataInput;
21 import java.io.DataOutputStream;
22 import java.io.IOException;
23
24 import org.apache.bcel.Const;
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 public abstract class ElementValue {
52
53 public static final byte STRING = 's';
54 public static final byte ENUM_CONSTANT = 'e';
55 public static final byte CLASS = 'c';
56 public static final byte ANNOTATION = '@';
57 public static final byte ARRAY = '[';
58 public static final byte PRIMITIVE_INT = 'I';
59 public static final byte PRIMITIVE_BYTE = 'B';
60 public static final byte PRIMITIVE_CHAR = 'C';
61 public static final byte PRIMITIVE_DOUBLE = 'D';
62 public static final byte PRIMITIVE_FLOAT = 'F';
63 public static final byte PRIMITIVE_LONG = 'J';
64 public static final byte PRIMITIVE_SHORT = 'S';
65 public static final byte PRIMITIVE_BOOLEAN = 'Z';
66
67
68
69
70
71
72
73
74
75 public static ElementValue readElementValue(final DataInput input, final ConstantPool cpool) throws IOException {
76 return readElementValue(input, cpool, 0);
77 }
78
79
80
81
82
83
84
85
86
87
88
89 public static ElementValue readElementValue(final DataInput input, final ConstantPool cpool, int arrayNesting)
90 throws IOException {
91 final byte tag = input.readByte();
92 switch (tag) {
93 case PRIMITIVE_BYTE:
94 case PRIMITIVE_CHAR:
95 case PRIMITIVE_DOUBLE:
96 case PRIMITIVE_FLOAT:
97 case PRIMITIVE_INT:
98 case PRIMITIVE_LONG:
99 case PRIMITIVE_SHORT:
100 case PRIMITIVE_BOOLEAN:
101 case STRING:
102 return new SimpleElementValue(tag, input.readUnsignedShort(), cpool);
103
104 case ENUM_CONSTANT:
105 return new EnumElementValue(ENUM_CONSTANT, input.readUnsignedShort(), input.readUnsignedShort(), cpool);
106
107 case CLASS:
108 return new ClassElementValue(CLASS, input.readUnsignedShort(), cpool);
109
110 case ANNOTATION:
111
112 return new AnnotationElementValue(ANNOTATION, AnnotationEntry.read(input, cpool, false), cpool);
113
114 case ARRAY:
115 arrayNesting++;
116 if (arrayNesting > Const.MAX_ARRAY_DIMENSIONS) {
117
118 throw new ClassFormatException(String.format("Arrays are only valid if they represent %,d or fewer dimensions.", Const.MAX_ARRAY_DIMENSIONS));
119 }
120 final int numArrayVals = input.readUnsignedShort();
121 final ElementValue[] evalues = new ElementValue[numArrayVals];
122 for (int j = 0; j < numArrayVals; j++) {
123 evalues[j] = ElementValue.readElementValue(input, cpool, arrayNesting);
124 }
125 return new ArrayElementValue(ARRAY, evalues, cpool);
126
127 default:
128 throw new ClassFormatException("Unexpected element value tag in annotation: " + tag);
129 }
130 }
131
132
133
134
135 @java.lang.Deprecated
136 protected int type;
137
138
139
140 @java.lang.Deprecated
141 protected ConstantPool cpool;
142
143 protected ElementValue(final int type, final ConstantPool cpool) {
144 this.type = type;
145 this.cpool = cpool;
146 }
147
148 public abstract void dump(DataOutputStream dos) throws IOException;
149
150
151 final ConstantPool getConstantPool() {
152 return cpool;
153 }
154
155 public int getElementValueType() {
156 return type;
157 }
158
159
160 final int getType() {
161 return type;
162 }
163
164 public abstract String stringifyValue();
165
166 public String toShortString() {
167 return stringifyValue();
168 }
169
170 @Override
171 public String toString() {
172 return stringifyValue();
173 }
174 }