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.DataOutputStream;
20 import java.io.IOException;
21
22 import org.apache.bcel.classfile.ConstantUtf8;
23 import org.apache.bcel.classfile.ElementValue;
24 import org.apache.bcel.classfile.EnumElementValue;
25
26
27
28
29 public class EnumElementValueGen extends ElementValueGen {
30
31 private final int typeIdx;
32
33 private final int valueIdx;
34
35 public EnumElementValueGen(final EnumElementValue value, final ConstantPoolGen cpool, final boolean copyPoolEntries) {
36 super(ENUM_CONSTANT, cpool);
37 if (copyPoolEntries) {
38 typeIdx = cpool.addUtf8(value.getEnumTypeString());
39
40 valueIdx = cpool.addUtf8(value.getEnumValueString());
41
42 } else {
43 typeIdx = value.getTypeIndex();
44 valueIdx = value.getValueIndex();
45 }
46 }
47
48
49
50
51
52 protected EnumElementValueGen(final int typeIdx, final int valueIdx, final ConstantPoolGen cpool) {
53 super(ElementValueGen.ENUM_CONSTANT, cpool);
54 if (super.getElementValueType() != ENUM_CONSTANT) {
55 throw new IllegalArgumentException("Only element values of type enum can be built with this ctor - type specified: " + super.getElementValueType());
56 }
57 this.typeIdx = typeIdx;
58 this.valueIdx = valueIdx;
59 }
60
61 public EnumElementValueGen(final ObjectType t, final String value, final ConstantPoolGen cpool) {
62 super(ElementValueGen.ENUM_CONSTANT, cpool);
63 typeIdx = cpool.addUtf8(t.getSignature());
64 valueIdx = cpool.addUtf8(value);
65 }
66
67 @Override
68 public void dump(final DataOutputStream dos) throws IOException {
69 dos.writeByte(super.getElementValueType());
70 dos.writeShort(typeIdx);
71 dos.writeShort(valueIdx);
72 }
73
74
75
76
77 @Override
78 public ElementValue getElementValue() {
79 System.err.println("Duplicating value: " + getEnumTypeString() + ":" + getEnumValueString());
80 return new EnumElementValue(super.getElementValueType(), typeIdx, valueIdx, getConstantPool().getConstantPool());
81 }
82
83
84
85 public String getEnumTypeString() {
86
87
88
89
90
91 return ((ConstantUtf8) getConstantPool().getConstant(typeIdx)).getBytes();
92
93 }
94
95 public String getEnumValueString() {
96 return ((ConstantUtf8) getConstantPool().getConstant(valueIdx)).getBytes();
97
98
99
100
101 }
102
103 public int getTypeIndex() {
104 return typeIdx;
105 }
106
107 public int getValueIndex() {
108 return valueIdx;
109 }
110
111 @Override
112 public String stringifyValue() {
113 final ConstantUtf8 cu8 = (ConstantUtf8) getConstantPool().getConstant(valueIdx);
114 return cu8.getBytes();
115
116
117
118
119 }
120 }