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