|
1 /* |
|
2 * reserved comment block |
|
3 * DO NOT REMOVE OR ALTER! |
|
4 */ |
|
5 /* |
|
6 * Licensed to the Apache Software Foundation (ASF) under one or more |
|
7 * contributor license agreements. See the NOTICE file distributed with |
|
8 * this work for additional information regarding copyright ownership. |
|
9 * The ASF licenses this file to You under the Apache License, Version 2.0 |
|
10 * (the "License"); you may not use this file except in compliance with |
|
11 * the License. You may obtain a copy of the License at |
|
12 * |
|
13 * http://www.apache.org/licenses/LICENSE-2.0 |
|
14 * |
|
15 * Unless required by applicable law or agreed to in writing, software |
|
16 * distributed under the License is distributed on an "AS IS" BASIS, |
|
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
18 * See the License for the specific language governing permissions and |
|
19 * limitations under the License. |
|
20 */ |
|
21 |
|
22 package com.sun.org.apache.bcel.internal.generic; |
|
23 |
|
24 import java.io.DataOutputStream; |
|
25 import java.io.IOException; |
|
26 |
|
27 import com.sun.org.apache.bcel.internal.classfile.ConstantUtf8; |
|
28 import com.sun.org.apache.bcel.internal.classfile.ElementValue; |
|
29 import com.sun.org.apache.bcel.internal.classfile.EnumElementValue; |
|
30 |
|
31 /** |
|
32 * @since 6.0 |
|
33 */ |
|
34 public class EnumElementValueGen extends ElementValueGen |
|
35 { |
|
36 // For enum types, these two indices point to the type and value |
|
37 private int typeIdx; |
|
38 |
|
39 private int valueIdx; |
|
40 |
|
41 /** |
|
42 * This ctor assumes the constant pool already contains the right type and |
|
43 * value - as indicated by typeIdx and valueIdx. This ctor is used for |
|
44 * deserialization |
|
45 */ |
|
46 protected EnumElementValueGen(final int typeIdx, final int valueIdx, |
|
47 final ConstantPoolGen cpool) |
|
48 { |
|
49 super(ElementValueGen.ENUM_CONSTANT, cpool); |
|
50 if (super.getElementValueType() != ENUM_CONSTANT) { |
|
51 throw new RuntimeException( |
|
52 "Only element values of type enum can be built with this ctor - type specified: " |
|
53 + super.getElementValueType()); |
|
54 } |
|
55 this.typeIdx = typeIdx; |
|
56 this.valueIdx = valueIdx; |
|
57 } |
|
58 |
|
59 /** |
|
60 * Return immutable variant of this EnumElementValue |
|
61 */ |
|
62 @Override |
|
63 public ElementValue getElementValue() |
|
64 { |
|
65 System.err.println("Duplicating value: " + getEnumTypeString() + ":" |
|
66 + getEnumValueString()); |
|
67 return new EnumElementValue(super.getElementValueType(), typeIdx, valueIdx, |
|
68 getConstantPool().getConstantPool()); |
|
69 } |
|
70 |
|
71 public EnumElementValueGen(final ObjectType t, final String value, final ConstantPoolGen cpool) |
|
72 { |
|
73 super(ElementValueGen.ENUM_CONSTANT, cpool); |
|
74 typeIdx = cpool.addUtf8(t.getSignature());// was addClass(t); |
|
75 valueIdx = cpool.addUtf8(value);// was addString(value); |
|
76 } |
|
77 |
|
78 public EnumElementValueGen(final EnumElementValue value, final ConstantPoolGen cpool, |
|
79 final boolean copyPoolEntries) |
|
80 { |
|
81 super(ENUM_CONSTANT, cpool); |
|
82 if (copyPoolEntries) |
|
83 { |
|
84 typeIdx = cpool.addUtf8(value.getEnumTypeString());// was |
|
85 // addClass(value.getEnumTypeString()); |
|
86 valueIdx = cpool.addUtf8(value.getEnumValueString()); // was |
|
87 // addString(value.getEnumValueString()); |
|
88 } |
|
89 else |
|
90 { |
|
91 typeIdx = value.getTypeIndex(); |
|
92 valueIdx = value.getValueIndex(); |
|
93 } |
|
94 } |
|
95 |
|
96 @Override |
|
97 public void dump(final DataOutputStream dos) throws IOException |
|
98 { |
|
99 dos.writeByte(super.getElementValueType()); // u1 type of value (ENUM_CONSTANT == 'e') |
|
100 dos.writeShort(typeIdx); // u2 |
|
101 dos.writeShort(valueIdx); // u2 |
|
102 } |
|
103 |
|
104 @Override |
|
105 public String stringifyValue() |
|
106 { |
|
107 final ConstantUtf8 cu8 = (ConstantUtf8) getConstantPool().getConstant(valueIdx); |
|
108 return cu8.getBytes(); |
|
109 // ConstantString cu8 = |
|
110 // (ConstantString)getConstantPool().getConstant(valueIdx); |
|
111 // return |
|
112 // ((ConstantUtf8)getConstantPool().getConstant(cu8.getStringIndex())).getBytes(); |
|
113 } |
|
114 |
|
115 // BCELBUG: Should we need to call utility.signatureToString() on the output |
|
116 // here? |
|
117 public String getEnumTypeString() |
|
118 { |
|
119 // Constant cc = getConstantPool().getConstant(typeIdx); |
|
120 // ConstantClass cu8 = |
|
121 // (ConstantClass)getConstantPool().getConstant(typeIdx); |
|
122 // return |
|
123 // ((ConstantUtf8)getConstantPool().getConstant(cu8.getNameIndex())).getBytes(); |
|
124 return ((ConstantUtf8) getConstantPool().getConstant(typeIdx)) |
|
125 .getBytes(); |
|
126 // return Utility.signatureToString(cu8.getBytes()); |
|
127 } |
|
128 |
|
129 public String getEnumValueString() |
|
130 { |
|
131 return ((ConstantUtf8) getConstantPool().getConstant(valueIdx)).getBytes(); |
|
132 // ConstantString cu8 = |
|
133 // (ConstantString)getConstantPool().getConstant(valueIdx); |
|
134 // return |
|
135 // ((ConstantUtf8)getConstantPool().getConstant(cu8.getStringIndex())).getBytes(); |
|
136 } |
|
137 |
|
138 public int getValueIndex() |
|
139 { |
|
140 return valueIdx; |
|
141 } |
|
142 |
|
143 public int getTypeIndex() |
|
144 { |
|
145 return typeIdx; |
|
146 } |
|
147 } |