1 /* 2 * reserved comment block 3 * DO NOT REMOVE OR ALTER! 4 */ 5 /* 6 * Licensed to the Apache Software Foundation (ASF) under one or more 7 * contributor license agreements. See the NOTICE file distributed with 8 * this work for additional information regarding copyright ownership. 9 * The ASF licenses this file to You under the Apache License, Version 2.0 10 * (the "License"); you may not use this file except in compliance with 11 * the License. You may obtain a copy of the License at 12 * 13 * http://www.apache.org/licenses/LICENSE-2.0 14 * 15 * Unless required by applicable law or agreed to in writing, software 16 * distributed under the License is distributed on an "AS IS" BASIS, 17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 * See the License for the specific language governing permissions and 19 * limitations under the License. 20 */ 21 22 package com.sun.org.apache.bcel.internal.classfile; 23 24 import java.io.DataOutputStream; 25 import java.io.IOException; 26 27 import com.sun.org.apache.bcel.internal.Const; 28 29 /** 30 * @since 6.0 31 */ 32 public class EnumElementValue extends ElementValue 33 { 34 // For enum types, these two indices point to the type and value 35 private final int typeIdx; 36 37 private final int valueIdx; 38 39 public EnumElementValue(final int type, final int typeIdx, final int valueIdx, 40 final ConstantPool cpool) 41 { 42 super(type, cpool); 43 if (type != ENUM_CONSTANT) { 44 throw new RuntimeException( 45 "Only element values of type enum can be built with this ctor - type specified: " + type); 46 } 47 this.typeIdx = typeIdx; 48 this.valueIdx = valueIdx; 49 } 50 51 @Override 52 public void dump(final DataOutputStream dos) throws IOException 53 { 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 @Override 60 public String stringifyValue() 61 { 62 final ConstantUtf8 cu8 = (ConstantUtf8) super.getConstantPool().getConstant(valueIdx, 63 Const.CONSTANT_Utf8); 64 return cu8.getBytes(); 65 } 66 67 public String getEnumTypeString() 68 { 69 final ConstantUtf8 cu8 = (ConstantUtf8) super.getConstantPool().getConstant(typeIdx, 70 Const.CONSTANT_Utf8); 71 return cu8.getBytes();// Utility.signatureToString(cu8.getBytes()); 72 } 73 74 public String getEnumValueString() 75 { 76 final ConstantUtf8 cu8 = (ConstantUtf8) super.getConstantPool().getConstant(valueIdx, 77 Const.CONSTANT_Utf8); 78 return cu8.getBytes(); 79 } 80 81 public int getValueIndex() 82 { 83 return valueIdx; 84 } 85 86 public int getTypeIndex() 87 { 88 return typeIdx; 89 } 90 }