1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * https://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19 package org.apache.bcel.generic;
20
21 import java.io.DataOutputStream;
22 import java.io.IOException;
23
24 import org.apache.bcel.classfile.ConstantUtf8;
25 import org.apache.bcel.classfile.ElementValue;
26 import org.apache.bcel.classfile.EnumElementValue;
27
28 /**
29 * @since 6.0
30 */
31 public class EnumElementValueGen extends ElementValueGen {
32 // For enum types, these two indices point to the type and value
33 private final int typeIdx;
34
35 private final int valueIdx;
36
37 public EnumElementValueGen(final EnumElementValue value, final ConstantPoolGen cpool, final boolean copyPoolEntries) {
38 super(ENUM_CONSTANT, cpool);
39 if (copyPoolEntries) {
40 typeIdx = cpool.addUtf8(value.getEnumTypeString()); // was addClass(value.getEnumTypeString());
41 valueIdx = cpool.addUtf8(value.getEnumValueString()); // was addString(value.getEnumValueString());
42 } else {
43 typeIdx = value.getTypeIndex();
44 valueIdx = value.getValueIndex();
45 }
46 }
47
48 /**
49 * This ctor assumes the constant pool already contains the right type and value - as indicated by typeIdx and valueIdx.
50 * This ctor is used for deserialization
51 */
52 protected EnumElementValueGen(final int typeIdx, final int valueIdx, final ConstantPoolGen cpool) {
53 super(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(ENUM_CONSTANT, cpool);
63 typeIdx = cpool.addUtf8(t.getSignature()); // was addClass(t);
64 valueIdx = cpool.addUtf8(value); // was addString(value);
65 }
66
67 @Override
68 public void dump(final DataOutputStream dos) throws IOException {
69 dos.writeByte(super.getElementValueType()); // u1 type of value (ENUM_CONSTANT == 'e')
70 dos.writeShort(typeIdx); // u2
71 dos.writeShort(valueIdx); // u2
72 }
73
74 /**
75 * Return immutable variant of this EnumElementValue
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 // BCELBUG: Should we need to call utility.signatureToString() on the output
84 // here?
85 public String getEnumTypeString() {
86 // Constant cc = getConstantPool().getConstant(typeIdx);
87 // ConstantClass cu8 =
88 // (ConstantClass) getConstantPool().getConstant(typeIdx);
89 // return
90 // ((ConstantUtf8) getConstantPool().getConstant(cu8.getNameIndex())).getBytes();
91 return ((ConstantUtf8) getConstantPool().getConstant(typeIdx)).getBytes();
92 // return Utility.signatureToString(cu8.getBytes());
93 }
94
95 public String getEnumValueString() {
96 return ((ConstantUtf8) getConstantPool().getConstant(valueIdx)).getBytes();
97 // ConstantString cu8 =
98 // (ConstantString) getConstantPool().getConstant(valueIdx);
99 // return
100 // ((ConstantUtf8) getConstantPool().getConstant(cu8.getStringIndex())).getBytes();
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 // ConstantString cu8 =
116 // (ConstantString) getConstantPool().getConstant(valueIdx);
117 // return
118 // ((ConstantUtf8) getConstantPool().getConstant(cu8.getStringIndex())).getBytes();
119 }
120 }