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   */
18  package org.apache.bcel.classfile;
19  
20  import java.io.DataOutputStream;
21  import java.io.IOException;
22  
23  import org.apache.bcel.Const;
24  
25  /**
26   * @since 6.0
27   */
28  public class EnumElementValue extends ElementValue
29  {
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 EnumElementValue(final int type, final int typeIdx, final int valueIdx,
36              final ConstantPool cpool)
37      {
38          super(type, cpool);
39          if (type != ENUM_CONSTANT) {
40              throw new RuntimeException(
41                      "Only element values of type enum can be built with this ctor - type specified: " + type);
42          }
43          this.typeIdx = typeIdx;
44          this.valueIdx = valueIdx;
45      }
46  
47      @Override
48      public void dump(final DataOutputStream dos) throws IOException
49      {
50          dos.writeByte(super.getType()); // u1 type of value (ENUM_CONSTANT == 'e')
51          dos.writeShort(typeIdx); // u2
52          dos.writeShort(valueIdx); // u2
53      }
54  
55      @Override
56      public String stringifyValue()
57      {
58          final ConstantUtf8 cu8 = (ConstantUtf8) super.getConstantPool().getConstant(valueIdx,
59                  Const.CONSTANT_Utf8);
60          return cu8.getBytes();
61      }
62  
63      public String getEnumTypeString()
64      {
65          final ConstantUtf8 cu8 = (ConstantUtf8) super.getConstantPool().getConstant(typeIdx,
66                  Const.CONSTANT_Utf8);
67          return cu8.getBytes();// Utility.signatureToString(cu8.getBytes());
68      }
69  
70      public String getEnumValueString()
71      {
72          final ConstantUtf8 cu8 = (ConstantUtf8) super.getConstantPool().getConstant(valueIdx,
73                  Const.CONSTANT_Utf8);
74          return cu8.getBytes();
75      }
76  
77      public int getValueIndex()
78      {
79          return valueIdx;
80      }
81  
82      public int getTypeIndex()
83      {
84          return typeIdx;
85      }
86  }