View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   https://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.bcel.classfile;
20  
21  import java.io.DataOutputStream;
22  import java.io.IOException;
23  
24  /**
25   * @since 6.0
26   */
27  public class EnumElementValue extends ElementValue {
28      // For enum types, these two indices point to the type and value
29      private final int typeIdx;
30  
31      private final int valueIdx;
32  
33      public EnumElementValue(final int type, final int typeIdx, final int valueIdx, final ConstantPool cpool) {
34          super(type, cpool);
35          if (type != ENUM_CONSTANT) {
36              throw new ClassFormatException("Only element values of type enum can be built with this ctor - type specified: " + type);
37          }
38          this.typeIdx = typeIdx;
39          this.valueIdx = valueIdx;
40      }
41  
42      @Override
43      public void dump(final DataOutputStream dos) throws IOException {
44          dos.writeByte(super.getType()); // u1 type of value (ENUM_CONSTANT == 'e')
45          dos.writeShort(typeIdx); // u2
46          dos.writeShort(valueIdx); // u2
47      }
48  
49      public String getEnumTypeString() {
50          return super.getConstantPool().getConstantUtf8(typeIdx).getBytes();
51      }
52  
53      public String getEnumValueString() {
54          return super.getConstantPool().getConstantUtf8(valueIdx).getBytes();
55      }
56  
57      public int getTypeIndex() {
58          return typeIdx;
59      }
60  
61      public int getValueIndex() {
62          return valueIdx;
63      }
64  
65      @Override
66      public String stringifyValue() {
67          return super.getConstantPool().getConstantUtf8(valueIdx).getBytes();
68      }
69  }