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