• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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  */
18 package org.apache.bcel.generic;
19 
20 import java.io.DataOutputStream;
21 import java.io.IOException;
22 
23 import org.apache.bcel.classfile.ConstantUtf8;
24 import org.apache.bcel.classfile.ElementValue;
25 import org.apache.bcel.classfile.EnumElementValue;
26 
27 /**
28  * @since 6.0
29  */
30 public class EnumElementValueGen extends ElementValueGen
31 {
32     // For enum types, these two indices point to the type and value
33     private int typeIdx;
34 
35     private int valueIdx;
36 
37     /**
38      * This ctor assumes the constant pool already contains the right type and
39      * value - as indicated by typeIdx and valueIdx. This ctor is used for
40      * deserialization
41      */
EnumElementValueGen(final int typeIdx, final int valueIdx, final ConstantPoolGen cpool)42     protected EnumElementValueGen(final int typeIdx, final int valueIdx,
43             final ConstantPoolGen cpool)
44     {
45         super(ElementValueGen.ENUM_CONSTANT, cpool);
46         if (super.getElementValueType() != ENUM_CONSTANT) {
47             throw new RuntimeException(
48                     "Only element values of type enum can be built with this ctor - type specified: " + super.getElementValueType());
49         }
50         this.typeIdx = typeIdx;
51         this.valueIdx = valueIdx;
52     }
53 
54     /**
55      * Return immutable variant of this EnumElementValue
56      */
57     @Override
getElementValue()58     public ElementValue getElementValue()
59     {
60         System.err.println("Duplicating value: " + getEnumTypeString() + ":"
61                 + getEnumValueString());
62         return new EnumElementValue(super.getElementValueType(), typeIdx, valueIdx,
63                 getConstantPool().getConstantPool());
64     }
65 
EnumElementValueGen(final ObjectType t, final String value, final ConstantPoolGen cpool)66     public EnumElementValueGen(final ObjectType t, final String value, final ConstantPoolGen cpool)
67     {
68         super(ElementValueGen.ENUM_CONSTANT, cpool);
69         typeIdx = cpool.addUtf8(t.getSignature());// was addClass(t);
70         valueIdx = cpool.addUtf8(value);// was addString(value);
71     }
72 
EnumElementValueGen(final EnumElementValue value, final ConstantPoolGen cpool, final boolean copyPoolEntries)73     public EnumElementValueGen(final EnumElementValue value, final ConstantPoolGen cpool,
74             final boolean copyPoolEntries)
75     {
76         super(ENUM_CONSTANT, cpool);
77         if (copyPoolEntries)
78         {
79             typeIdx = cpool.addUtf8(value.getEnumTypeString());// was
80                                                                 // addClass(value.getEnumTypeString());
81             valueIdx = cpool.addUtf8(value.getEnumValueString()); // was
82                                                                     // addString(value.getEnumValueString());
83         }
84         else
85         {
86             typeIdx = value.getTypeIndex();
87             valueIdx = value.getValueIndex();
88         }
89     }
90 
91     @Override
dump(final DataOutputStream dos)92     public void dump(final DataOutputStream dos) throws IOException
93     {
94         dos.writeByte(super.getElementValueType()); // u1 type of value (ENUM_CONSTANT == 'e')
95         dos.writeShort(typeIdx); // u2
96         dos.writeShort(valueIdx); // u2
97     }
98 
99     @Override
stringifyValue()100     public String stringifyValue()
101     {
102         final ConstantUtf8 cu8 = (ConstantUtf8) getConstantPool().getConstant(valueIdx);
103         return cu8.getBytes();
104         // ConstantString cu8 =
105         // (ConstantString)getConstantPool().getConstant(valueIdx);
106         // return
107         // ((ConstantUtf8)getConstantPool().getConstant(cu8.getStringIndex())).getBytes();
108     }
109 
110     // BCELBUG: Should we need to call utility.signatureToString() on the output
111     // here?
getEnumTypeString()112     public String getEnumTypeString()
113     {
114         // Constant cc = getConstantPool().getConstant(typeIdx);
115         // ConstantClass cu8 =
116         // (ConstantClass)getConstantPool().getConstant(typeIdx);
117         // return
118         // ((ConstantUtf8)getConstantPool().getConstant(cu8.getNameIndex())).getBytes();
119         return ((ConstantUtf8) getConstantPool().getConstant(typeIdx))
120                 .getBytes();
121         // return Utility.signatureToString(cu8.getBytes());
122     }
123 
getEnumValueString()124     public String getEnumValueString()
125     {
126         return ((ConstantUtf8) getConstantPool().getConstant(valueIdx)).getBytes();
127         // ConstantString cu8 =
128         // (ConstantString)getConstantPool().getConstant(valueIdx);
129         // return
130         // ((ConstantUtf8)getConstantPool().getConstant(cu8.getStringIndex())).getBytes();
131     }
132 
getValueIndex()133     public int getValueIndex()
134     {
135         return valueIdx;
136     }
137 
getTypeIndex()138     public int getTypeIndex()
139     {
140         return typeIdx;
141     }
142 }
143