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 * 017 */ 018package org.apache.bcel.generic; 019 020import java.io.DataOutputStream; 021import java.io.IOException; 022 023import org.apache.bcel.classfile.ConstantUtf8; 024import org.apache.bcel.classfile.ElementValue; 025import org.apache.bcel.classfile.EnumElementValue; 026 027/** 028 * @since 6.0 029 */ 030public class EnumElementValueGen extends ElementValueGen { 031 // For enum types, these two indices point to the type and value 032 private final int typeIdx; 033 034 private final int valueIdx; 035 036 public EnumElementValueGen(final EnumElementValue value, final ConstantPoolGen cpool, final boolean copyPoolEntries) { 037 super(ENUM_CONSTANT, cpool); 038 if (copyPoolEntries) { 039 typeIdx = cpool.addUtf8(value.getEnumTypeString());// was 040 // addClass(value.getEnumTypeString()); 041 valueIdx = cpool.addUtf8(value.getEnumValueString()); // was 042 // addString(value.getEnumValueString()); 043 } else { 044 typeIdx = value.getTypeIndex(); 045 valueIdx = value.getValueIndex(); 046 } 047 } 048 049 /** 050 * This ctor assumes the constant pool already contains the right type and value - as indicated by typeIdx and valueIdx. 051 * This ctor is used for deserialization 052 */ 053 protected EnumElementValueGen(final int typeIdx, final int valueIdx, final ConstantPoolGen cpool) { 054 super(ElementValueGen.ENUM_CONSTANT, cpool); 055 if (super.getElementValueType() != ENUM_CONSTANT) { 056 throw new IllegalArgumentException("Only element values of type enum can be built with this ctor - type specified: " + super.getElementValueType()); 057 } 058 this.typeIdx = typeIdx; 059 this.valueIdx = valueIdx; 060 } 061 062 public EnumElementValueGen(final ObjectType t, final String value, final ConstantPoolGen cpool) { 063 super(ElementValueGen.ENUM_CONSTANT, cpool); 064 typeIdx = cpool.addUtf8(t.getSignature());// was addClass(t); 065 valueIdx = cpool.addUtf8(value);// was addString(value); 066 } 067 068 @Override 069 public void dump(final DataOutputStream dos) throws IOException { 070 dos.writeByte(super.getElementValueType()); // u1 type of value (ENUM_CONSTANT == 'e') 071 dos.writeShort(typeIdx); // u2 072 dos.writeShort(valueIdx); // u2 073 } 074 075 /** 076 * Return immutable variant of this EnumElementValue 077 */ 078 @Override 079 public ElementValue getElementValue() { 080 System.err.println("Duplicating value: " + getEnumTypeString() + ":" + getEnumValueString()); 081 return new EnumElementValue(super.getElementValueType(), typeIdx, valueIdx, getConstantPool().getConstantPool()); 082 } 083 084 // BCELBUG: Should we need to call utility.signatureToString() on the output 085 // here? 086 public String getEnumTypeString() { 087 // Constant cc = getConstantPool().getConstant(typeIdx); 088 // ConstantClass cu8 = 089 // (ConstantClass)getConstantPool().getConstant(typeIdx); 090 // return 091 // ((ConstantUtf8)getConstantPool().getConstant(cu8.getNameIndex())).getBytes(); 092 return ((ConstantUtf8) getConstantPool().getConstant(typeIdx)).getBytes(); 093 // return Utility.signatureToString(cu8.getBytes()); 094 } 095 096 public String getEnumValueString() { 097 return ((ConstantUtf8) getConstantPool().getConstant(valueIdx)).getBytes(); 098 // ConstantString cu8 = 099 // (ConstantString)getConstantPool().getConstant(valueIdx); 100 // return 101 // ((ConstantUtf8)getConstantPool().getConstant(cu8.getStringIndex())).getBytes(); 102 } 103 104 public int getTypeIndex() { 105 return typeIdx; 106 } 107 108 public int getValueIndex() { 109 return valueIdx; 110 } 111 112 @Override 113 public String stringifyValue() { 114 final ConstantUtf8 cu8 = (ConstantUtf8) getConstantPool().getConstant(valueIdx); 115 return cu8.getBytes(); 116 // ConstantString cu8 = 117 // (ConstantString)getConstantPool().getConstant(valueIdx); 118 // return 119 // ((ConstantUtf8)getConstantPool().getConstant(cu8.getStringIndex())).getBytes(); 120 } 121}