001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * https://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, 013 * software distributed under the License is distributed on an 014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 * KIND, either express or implied. See the License for the 016 * specific language governing permissions and limitations 017 * under the License. 018 */ 019package org.apache.bcel.classfile; 020 021import java.io.DataOutputStream; 022import java.io.IOException; 023 024/** 025 * Represents an enum element value in an annotation. 026 * 027 * @since 6.0 028 */ 029public class EnumElementValue extends ElementValue { 030 // For enum types, these two indices point to the type and value 031 private final int typeIdx; 032 033 private final int valueIdx; 034 035 /** 036 * Constructs an EnumElementValue. 037 * 038 * @param type the element value type. 039 * @param typeIdx the type index. 040 * @param valueIdx the value index. 041 * @param cpool the constant pool. 042 */ 043 public EnumElementValue(final int type, final int typeIdx, final int valueIdx, final ConstantPool cpool) { 044 super(type, cpool); 045 if (type != ENUM_CONSTANT) { 046 throw new ClassFormatException("Only element values of type enum can be built with this ctor - type specified: " + type); 047 } 048 this.typeIdx = typeIdx; 049 this.valueIdx = valueIdx; 050 } 051 052 @Override 053 public void dump(final DataOutputStream dos) throws IOException { 054 dos.writeByte(super.getType()); // u1 type of value (ENUM_CONSTANT == 'e') 055 dos.writeShort(typeIdx); // u2 056 dos.writeShort(valueIdx); // u2 057 } 058 059 /** 060 * Gets the enum type string. 061 * 062 * @return the enum type string. 063 */ 064 public String getEnumTypeString() { 065 return super.getConstantPool().getConstantUtf8(typeIdx).getBytes(); 066 } 067 068 /** 069 * Gets the enum value string. 070 * 071 * @return the enum value string. 072 */ 073 public String getEnumValueString() { 074 return super.getConstantPool().getConstantUtf8(valueIdx).getBytes(); 075 } 076 077 /** 078 * Gets the type index. 079 * 080 * @return the type index. 081 */ 082 public int getTypeIndex() { 083 return typeIdx; 084 } 085 086 /** 087 * Gets the value index. 088 * 089 * @return the value index. 090 */ 091 public int getValueIndex() { 092 return valueIdx; 093 } 094 095 @Override 096 public String stringifyValue() { 097 return super.getConstantPool().getConstantUtf8(valueIdx).getBytes(); 098 } 099}