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
023/**
024 * @since 6.0
025 */
026public class EnumElementValue extends ElementValue {
027    // For enum types, these two indices point to the type and value
028    private final int typeIdx;
029
030    private final int valueIdx;
031
032    public EnumElementValue(final int type, final int typeIdx, final int valueIdx, final ConstantPool cpool) {
033        super(type, cpool);
034        if (type != ENUM_CONSTANT) {
035            throw new IllegalArgumentException("Only element values of type enum can be built with this ctor - type specified: " + type);
036        }
037        this.typeIdx = typeIdx;
038        this.valueIdx = valueIdx;
039    }
040
041    @Override
042    public void dump(final DataOutputStream dos) throws IOException {
043        dos.writeByte(super.getType()); // u1 type of value (ENUM_CONSTANT == 'e')
044        dos.writeShort(typeIdx); // u2
045        dos.writeShort(valueIdx); // u2
046    }
047
048    public String getEnumTypeString() {
049        return super.getConstantPool().getConstantUtf8(typeIdx).getBytes();
050    }
051
052    public String getEnumValueString() {
053        return super.getConstantPool().getConstantUtf8(valueIdx).getBytes();
054    }
055
056    public int getTypeIndex() {
057        return typeIdx;
058    }
059
060    public int getValueIndex() {
061        return valueIdx;
062    }
063
064    @Override
065    public String stringifyValue() {
066        return super.getConstantPool().getConstantUtf8(valueIdx).getBytes();
067    }
068}