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
39                                                                 // addClass(value.getEnumTypeString());
40              valueIdx = cpool.addUtf8(value.getEnumValueString()); // was
41                                                                    // 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(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());// 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 }