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