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 */
017package org.apache.bcel.generic;
018
019import java.io.DataOutputStream;
020import java.io.IOException;
021
022import org.apache.bcel.classfile.Constant;
023import org.apache.bcel.classfile.ConstantClass;
024import org.apache.bcel.classfile.ConstantPool;
025import org.apache.bcel.classfile.Utility;
026import org.apache.bcel.util.ByteSequence;
027
028/**
029 * Abstract super class for instructions that use an index into the constant pool such as LDC, INVOKEVIRTUAL, etc.
030 *
031 * @see ConstantPoolGen
032 * @see LDC
033 * @see INVOKEVIRTUAL
034 */
035public abstract class CPInstruction extends Instruction implements TypedInstruction, IndexedInstruction {
036
037    /**
038     * @deprecated (since 6.0) will be made private; do not access directly, use getter/setter
039     */
040    @Deprecated
041    protected int index; // index to constant pool
042
043    /**
044     * Empty constructor needed for Instruction.readInstruction. Not to be used otherwise.
045     */
046    CPInstruction() {
047    }
048
049    /**
050     * @param index to constant pool
051     */
052    protected CPInstruction(final short opcode, final int index) {
053        super(opcode, (short) 3);
054        setIndex(index);
055    }
056
057    /**
058     * Dump instruction as byte code to stream out.
059     *
060     * @param out Output stream
061     */
062    @Override
063    public void dump(final DataOutputStream out) throws IOException {
064        out.writeByte(super.getOpcode());
065        out.writeShort(index);
066    }
067
068    /**
069     * @return index in constant pool referred by this instruction.
070     */
071    @Override
072    public final int getIndex() {
073        return index;
074    }
075
076    /**
077     * @return type related with this instruction.
078     */
079    @Override
080    public Type getType(final ConstantPoolGen cpg) {
081        final ConstantPool cp = cpg.getConstantPool();
082        String name = cp.getConstantString(index, org.apache.bcel.Const.CONSTANT_Class);
083        if (!name.startsWith("[")) {
084            name = "L" + name + ";";
085        }
086        return Type.getType(name);
087    }
088
089    /**
090     * Read needed data (i.e., index) from file.
091     *
092     * @param bytes input stream
093     * @param wide wide prefix?
094     */
095    @Override
096    protected void initFromFile(final ByteSequence bytes, final boolean wide) throws IOException {
097        setIndex(bytes.readUnsignedShort());
098        super.setLength(3);
099    }
100
101    /**
102     * Sets the index to constant pool.
103     *
104     * @param index in constant pool.
105     */
106    @Override
107    public void setIndex(final int index) { // TODO could be package-protected?
108        if (index < 0) {
109            throw new ClassGenException("Negative index value: " + index);
110        }
111        this.index = index;
112    }
113
114    /**
115     * Long output format:
116     *
117     * &lt;name of opcode&gt; "["&lt;opcode number&gt;"]" "("&lt;length of instruction&gt;")" "&lt;"&lt; constant pool
118     * index&gt;"&gt;"
119     *
120     * @param verbose long/short format switch
121     * @return mnemonic for instruction
122     */
123    @Override
124    public String toString(final boolean verbose) {
125        return super.toString(verbose) + " " + index;
126    }
127
128    /**
129     * @return mnemonic for instruction with symbolic references resolved
130     */
131    @Override
132    public String toString(final ConstantPool cp) {
133        final Constant c = cp.getConstant(index);
134        String str = cp.constantToString(c);
135        if (c instanceof ConstantClass) {
136            str = Utility.packageToPath(str);
137        }
138        return org.apache.bcel.Const.getOpcodeName(super.getOpcode()) + " " + str;
139    }
140}