    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.generic;
    020
    021import java.io.DataOutputStream;
    022import java.io.IOException;
    023
    024import org.apache.bcel.classfile.ConstantUtf8;
    025import org.apache.bcel.classfile.ElementValue;
    026import org.apache.bcel.classfile.EnumElementValue;
    027
    028/**
    029 * @since 6.0
    030 */
    031public class EnumElementValueGen extends ElementValueGen {
    032    // For enum types, these two indices point to the type and value
    033    private final int typeIdx;
    034
    035    private final int valueIdx;
    036
    037    public EnumElementValueGen(final EnumElementValue value, final ConstantPoolGen cpool, final boolean copyPoolEntries) {
    038        super(ENUM_CONSTANT, cpool);
    039        if (copyPoolEntries) {
    040            typeIdx = cpool.addUtf8(value.getEnumTypeString()); // was addClass(value.getEnumTypeString());
    041            valueIdx = cpool.addUtf8(value.getEnumValueString()); // was 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(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(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}