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 * Represents a class element value in an annotation.
026 *
027 * @since 6.0
028 */
029public class ClassElementValue extends ElementValue {
030    // For primitive types and string type, this points to the value entry in
031    // the cpool
032    // For 'class' this points to the class entry in the cpool
033    private final int idx;
034
035    /**
036     * Constructs a ClassElementValue.
037     *
038     * @param type the type.
039     * @param idx the index.
040     * @param cpool the constant pool.
041     */
042    public ClassElementValue(final int type, final int idx, final ConstantPool cpool) {
043        super(type, cpool);
044        this.idx = idx;
045    }
046
047    @Override
048    public void dump(final DataOutputStream dos) throws IOException {
049        dos.writeByte(super.getType()); // u1 kind of value
050        dos.writeShort(idx);
051    }
052
053    /**
054     * Gets the class string.
055     *
056     * @return the class string.
057     */
058    public String getClassString() {
059        return super.getConstantPool().getConstantUtf8(idx).getBytes();
060    }
061
062    /**
063     * Gets the index.
064     *
065     * @return the index.
066     */
067    public int getIndex() {
068        return idx;
069    }
070
071    @Override
072    public String stringifyValue() {
073        return super.getConstantPool().getConstantUtf8(idx).getBytes();
074    }
075}