# Class CPInstruction

java.lang.Object
org.apache.bcel.generic.Instruction
org.apache.bcel.generic.CPInstruction

All Implemented Interfaces:
  `Cloneable, IndexedInstruction, TypedInstruction`

Direct Known Subclasses:
  `ANEWARRAY, CHECKCAST, FieldOrMethod, INSTANCEOF, LDC, LDC2_W, MULTIANEWARRAY, NameSignatureInstruction, NEW`

public abstract class CPInstruction extends Instruction implements TypedInstruction, IndexedInstruction
Abstract super class for instructions that use an index into the constant pool such as LDC, INVOKEVIRTUAL, etc.

See Also:
  - `ConstantPoolGen`
  - `LDC`
  - `INVOKEVIRTUAL`

- ## Field Summary

  Fields
  Modifier and Type
  Field
  Description
  `protected int`
  `index`
  Deprecated.
  (since 6.0) will be made private; do not access directly, use getter/setter

  ### Fields inherited from class Instruction

  `length, opcode`

- ## Constructor Summary

  Constructors
  Modifier
  Constructor
  Description
  `protected`
  `CPInstruction(short opcode, int index)`

- ## Method Summary

  Modifier and Type
  Method
  Description
  `void`
  `dump(DataOutputStream out)`
  Dump instruction as byte code to stream out.
  `final int`
  `getIndex()`
  `Type`
  `getType(ConstantPoolGen cpg)`
  `protected void`
  `initFromFile(ByteSequence bytes, boolean wide)`
  Reads needed data (i.e., index) from file.
  `void`
  `setIndex(int index)`
  Sets the index to constant pool.
  `String`
  `toString(boolean verbose)`
  Long output format: <name of opcode> "["<opcode number>"]" "("<length of instruction>")" "<"< constant pool index>">"
  `String`
  `toString(ConstantPool cp)`

  ### Methods inherited from class Instruction

  `accept, consumeStack, copy, equals, getComparator, getLength, getName, getOpcode, hashCode, isValidByte, isValidShort, produceStack, readInstruction, setComparator, toString`

  ### Methods inherited from class Object

  `clone, finalize, getClass, notify, notifyAll, wait, wait, wait`

- ## Field Details

  - ### index

    @Deprecated protected int index
    Deprecated.
    (since 6.0) will be made private; do not access directly, use getter/setter

- ## Constructor Details

  - ### CPInstruction

    protected CPInstruction(short opcode, int index)
    Parameters:
      `index` - to constant pool

- ## Method Details

  - ### dump

    public void dump(DataOutputStream out) throws IOException
    Dump instruction as byte code to stream out.
    Overrides:
      `dump` in class `Instruction`
    Parameters:
      `out` - Output stream
    Throws:
      `IOException` - Thrown when an I/O exception of some sort has occurred.

  - ### getIndex

    public final int getIndex()
    Specified by:
      `getIndex` in interface `IndexedInstruction`
    Returns:
      index in constant pool referred by this instruction.

  - ### getType

    public Type getType(ConstantPoolGen cpg)
    Specified by:
      `getType` in interface `TypedInstruction`
    Returns:
      type related with this instruction.

  - ### initFromFile

    protected void initFromFile(ByteSequence bytes, boolean wide) throws IOException
    Reads needed data (i.e., index) from file.
    Overrides:
      `initFromFile` in class `Instruction`
    Parameters:
      `bytes` - input stream
      `wide` - wide prefix?
    Throws:
      `IOException` - may be thrown if the implementation needs to read data from the file

  - ### setIndex

    public void setIndex(int index)
    Sets the index to constant pool.
    Specified by:
      `setIndex` in interface `IndexedInstruction`
    Parameters:
      `index` - in constant pool.

  - ### toString

    public String toString(boolean verbose)
    Long output format: <name of opcode> "["<opcode number>"]" "("<length of instruction>")" "<"< constant pool index>">"
    Overrides:
      `toString` in class `Instruction`
    Parameters:
      `verbose` - long/short format switch
    Returns:
      mnemonic for instruction

  - ### toString

    public String toString(ConstantPool cp)
    Overrides:
      `toString` in class `Instruction`
    Returns:
      mnemonic for instruction with symbolic references resolved