001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.bcel.generic; 018 019import java.io.DataOutputStream; 020import java.io.IOException; 021 022import org.apache.bcel.classfile.ConstantUtf8; 023import org.apache.bcel.classfile.ElementValue; 024import org.apache.bcel.classfile.EnumElementValue; 025 026/** 027 * @since 6.0 028 */ 029public class EnumElementValueGen extends ElementValueGen { 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 public EnumElementValueGen(final EnumElementValue value, final ConstantPoolGen cpool, final boolean copyPoolEntries) { 036 super(ENUM_CONSTANT, cpool); 037 if (copyPoolEntries) { 038 typeIdx = cpool.addUtf8(value.getEnumTypeString());// was 039 // addClass(value.getEnumTypeString()); 040 valueIdx = cpool.addUtf8(value.getEnumValueString()); // was 041 // addString(value.getEnumValueString()); 042 } else { 043 typeIdx = value.getTypeIndex(); 044 valueIdx = value.getValueIndex(); 045 } 046 } 047 048 /** 049 * This ctor assumes the constant pool already contains the right type and value - as indicated by typeIdx and valueIdx. 050 * This ctor is used for deserialization 051 */ 052 protected EnumElementValueGen(final int typeIdx, final int valueIdx, final ConstantPoolGen cpool) { 053 super(ElementValueGen.ENUM_CONSTANT, cpool); 054 if (super.getElementValueType() != ENUM_CONSTANT) { 055 throw new IllegalArgumentException("Only element values of type enum can be built with this ctor - type specified: " + super.getElementValueType()); 056 } 057 this.typeIdx = typeIdx; 058 this.valueIdx = valueIdx; 059 } 060 061 public EnumElementValueGen(final ObjectType t, final String value, final ConstantPoolGen cpool) { 062 super(ElementValueGen.ENUM_CONSTANT, cpool); 063 typeIdx = cpool.addUtf8(t.getSignature());// was addClass(t); 064 valueIdx = cpool.addUtf8(value);// was addString(value); 065 } 066 067 @Override 068 public void dump(final DataOutputStream dos) throws IOException { 069 dos.writeByte(super.getElementValueType()); // u1 type of value (ENUM_CONSTANT == 'e') 070 dos.writeShort(typeIdx); // u2 071 dos.writeShort(valueIdx); // u2 072 } 073 074 /** 075 * Return immutable variant of this EnumElementValue 076 */ 077 @Override 078 public ElementValue getElementValue() { 079 System.err.println("Duplicating value: " + getEnumTypeString() + ":" + getEnumValueString()); 080 return new EnumElementValue(super.getElementValueType(), typeIdx, valueIdx, getConstantPool().getConstantPool()); 081 } 082 083 // BCELBUG: Should we need to call utility.signatureToString() on the output 084 // here? 085 public String getEnumTypeString() { 086 // Constant cc = getConstantPool().getConstant(typeIdx); 087 // ConstantClass cu8 = 088 // (ConstantClass)getConstantPool().getConstant(typeIdx); 089 // return 090 // ((ConstantUtf8)getConstantPool().getConstant(cu8.getNameIndex())).getBytes(); 091 return ((ConstantUtf8) getConstantPool().getConstant(typeIdx)).getBytes(); 092 // return Utility.signatureToString(cu8.getBytes()); 093 } 094 095 public String getEnumValueString() { 096 return ((ConstantUtf8) getConstantPool().getConstant(valueIdx)).getBytes(); 097 // ConstantString cu8 = 098 // (ConstantString)getConstantPool().getConstant(valueIdx); 099 // return 100 // ((ConstantUtf8)getConstantPool().getConstant(cu8.getStringIndex())).getBytes(); 101 } 102 103 public int getTypeIndex() { 104 return typeIdx; 105 } 106 107 public int getValueIndex() { 108 return valueIdx; 109 } 110 111 @Override 112 public String stringifyValue() { 113 final ConstantUtf8 cu8 = (ConstantUtf8) getConstantPool().getConstant(valueIdx); 114 return cu8.getBytes(); 115 // ConstantString cu8 = 116 // (ConstantString)getConstantPool().getConstant(valueIdx); 117 // return 118 // ((ConstantUtf8)getConstantPool().getConstant(cu8.getStringIndex())).getBytes(); 119 } 120}