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