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.classfile;
18  
19  import java.io.DataOutputStream;
20  import java.io.IOException;
21  
22  /**
23   * @since 6.0
24   */
25  public class EnumElementValue extends ElementValue {
26      // For enum types, these two indices point to the type and value
27      private final int typeIdx;
28  
29      private final int valueIdx;
30  
31      public EnumElementValue(final int type, final int typeIdx, final int valueIdx, final ConstantPool cpool) {
32          super(type, cpool);
33          if (type != ENUM_CONSTANT) {
34              throw new ClassFormatException("Only element values of type enum can be built with this ctor - type specified: " + type);
35          }
36          this.typeIdx = typeIdx;
37          this.valueIdx = valueIdx;
38      }
39  
40      @Override
41      public void dump(final DataOutputStream dos) throws IOException {
42          dos.writeByte(super.getType()); // u1 type of value (ENUM_CONSTANT == 'e')
43          dos.writeShort(typeIdx); // u2
44          dos.writeShort(valueIdx); // u2
45      }
46  
47      public String getEnumTypeString() {
48          return super.getConstantPool().getConstantUtf8(typeIdx).getBytes();
49      }
50  
51      public String getEnumValueString() {
52          return super.getConstantPool().getConstantUtf8(valueIdx).getBytes();
53      }
54  
55      public int getTypeIndex() {
56          return typeIdx;
57      }
58  
59      public int getValueIndex() {
60          return valueIdx;
61      }
62  
63      @Override
64      public String stringifyValue() {
65          return super.getConstantPool().getConstantUtf8(valueIdx).getBytes();
66      }
67  }