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 * Represents an enum element value in an annotation.
26 *
27 * @since 6.0
28 */
29 public class EnumElementValue extends ElementValue {
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 /**
36 * Constructs an EnumElementValue.
37 *
38 * @param type the element value type.
39 * @param typeIdx the type index.
40 * @param valueIdx the value index.
41 * @param cpool the constant pool.
42 */
43 public EnumElementValue(final int type, final int typeIdx, final int valueIdx, final ConstantPool cpool) {
44 super(type, cpool);
45 if (type != ENUM_CONSTANT) {
46 throw new ClassFormatException("Only element values of type enum can be built with this ctor - type specified: " + type);
47 }
48 this.typeIdx = typeIdx;
49 this.valueIdx = valueIdx;
50 }
51
52 @Override
53 public void dump(final DataOutputStream dos) throws IOException {
54 dos.writeByte(super.getType()); // u1 type of value (ENUM_CONSTANT == 'e')
55 dos.writeShort(typeIdx); // u2
56 dos.writeShort(valueIdx); // u2
57 }
58
59 /**
60 * Gets the enum type string.
61 *
62 * @return the enum type string.
63 */
64 public String getEnumTypeString() {
65 return super.getConstantPool().getConstantUtf8(typeIdx).getBytes();
66 }
67
68 /**
69 * Gets the enum value string.
70 *
71 * @return the enum value string.
72 */
73 public String getEnumValueString() {
74 return super.getConstantPool().getConstantUtf8(valueIdx).getBytes();
75 }
76
77 /**
78 * Gets the type index.
79 *
80 * @return the type index.
81 */
82 public int getTypeIndex() {
83 return typeIdx;
84 }
85
86 /**
87 * Gets the value index.
88 *
89 * @return the value index.
90 */
91 public int getValueIndex() {
92 return valueIdx;
93 }
94
95 @Override
96 public String stringifyValue() {
97 return super.getConstantPool().getConstantUtf8(valueIdx).getBytes();
98 }
99 }