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.classfile;
019
020import java.io.DataOutputStream;
021import java.io.IOException;
022
023import org.apache.bcel.Const;
024
025/**
026 * @since 6.0
027 */
028public class EnumElementValue extends ElementValue
029{
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 EnumElementValue(final int type, final int typeIdx, final int valueIdx,
036            final ConstantPool cpool)
037    {
038        super(type, cpool);
039        if (type != ENUM_CONSTANT) {
040            throw new RuntimeException(
041                    "Only element values of type enum can be built with this ctor - type specified: " + type);
042        }
043        this.typeIdx = typeIdx;
044        this.valueIdx = valueIdx;
045    }
046
047    @Override
048    public void dump(final DataOutputStream dos) throws IOException
049    {
050        dos.writeByte(super.getType()); // u1 type of value (ENUM_CONSTANT == 'e')
051        dos.writeShort(typeIdx); // u2
052        dos.writeShort(valueIdx); // u2
053    }
054
055    @Override
056    public String stringifyValue()
057    {
058        final ConstantUtf8 cu8 = (ConstantUtf8) super.getConstantPool().getConstant(valueIdx,
059                Const.CONSTANT_Utf8);
060        return cu8.getBytes();
061    }
062
063    public String getEnumTypeString()
064    {
065        final ConstantUtf8 cu8 = (ConstantUtf8) super.getConstantPool().getConstant(typeIdx,
066                Const.CONSTANT_Utf8);
067        return cu8.getBytes();// Utility.signatureToString(cu8.getBytes());
068    }
069
070    public String getEnumValueString()
071    {
072        final ConstantUtf8 cu8 = (ConstantUtf8) super.getConstantPool().getConstant(valueIdx,
073                Const.CONSTANT_Utf8);
074        return cu8.getBytes();
075    }
076
077    public int getValueIndex()
078    {
079        return valueIdx;
080    }
081
082    public int getTypeIndex()
083    {
084        return typeIdx;
085    }
086}