Class Instruction

java.lang.Object
org.apache.bcel.generic.Instruction
All Implemented Interfaces:
Cloneable
Direct Known Subclasses:
ACONST_NULL, ArithmeticInstruction, ArrayInstruction, ARRAYLENGTH, ATHROW, BIPUSH, BranchInstruction, BREAKPOINT, ConversionInstruction, CPInstruction, DCMPG, DCMPL, DCONST, FCMPG, FCMPL, FCONST, ICONST, IMPDEP1, IMPDEP2, LCMP, LCONST, LocalVariableInstruction, MONITORENTER, MONITOREXIT, NEWARRAY, NOP, RET, ReturnInstruction, SIPUSH, StackInstruction

public abstract class Instruction extends Object implements Cloneable
Abstract super class for all Java byte codes.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    private static InstructionComparator
     
    (package private) static final Instruction[]
     
    protected short
    Deprecated.
    (since 6.0) will be made private; do not access directly, use getter/setter
    protected short
    Deprecated.
    (since 6.0) will be made private; do not access directly, use getter/setter
  • Constructor Summary

    Constructors
    Constructor
    Description
    Empty constructor needed for Instruction.readInstruction.
    Instruction(short opcode, short length)
    Constructs an Instruction.
  • Method Summary

    Modifier and Type
    Method
    Description
    abstract void
    Call corresponding visitor method(s).
    int
    This method also gives right results for instructions whose effect on the stack depends on the constant pool entry they reference.
    Use with caution, since 'BranchInstruction's have a 'target' reference which is not copied correctly (only basic types are).
    (package private) void
    Some instructions may be reused, so don't do anything by default.
    void
    Dumps instruction as byte code to stream out.
    (package private) byte[]
    Dumps this instruction to a byte array.
    boolean
    equals(Object that)
    Tests for equality, delegated to comparator
    Deprecated.
    (6.0) use the built in comparator, or wrap this class in another object that implements these methods
    int
    Gets the length (in bytes) of instruction.
    Gets the name of instruction, that is, opcode name.
    short
    Gets this instruction's opcode.
    int
    Gets the hashCode of this object.
    protected void
    initFromFile(ByteSequence bytes, boolean wide)
    Reads needed data (for example index) from file.
    (package private) static boolean
    Tests if the value can fit in a non-negative short.
    static boolean
    isValidByte(int value)
    Tests if the value can fit in a byte (signed).
    static boolean
    isValidShort(int value)
    Tests if the value can fit in a short (signed).
    int
    This method also gives right results for instructions whose effect on the stack depends on the constant pool entry they reference.
    (package private) static Instruction
    readInstruction(byte[] code)
    Reads an instruction from (byte code) input stream and return the appropriate object.
    Reads an instruction from (byte code) input stream and return the appropriate object.
    static void
    Deprecated.
    (6.0) use the built in comparator, or wrap this class in another object that implements these methods
    (package private) final void
    setLength(int length)
    Needed in readInstruction and subclasses in this package
    (package private) final void
    setOpcode(short opcode)
    Needed in readInstruction and subclasses in this package.
     
    toString(boolean verbose)
    Long output format: <name of opcode> "["<opcode number>"]" "("<length of instruction>")"
    Gets the mnemonic for instruction with symbolic references resolved.

    Methods inherited from class java.lang.Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait
  • Field Details

    • EMPTY_ARRAY

      static final Instruction[] EMPTY_ARRAY
    • cmp

      private static InstructionComparator cmp
    • length

      @Deprecated protected short length
      Deprecated.
      (since 6.0) will be made private; do not access directly, use getter/setter
    • opcode

      @Deprecated protected short opcode
      Deprecated.
      (since 6.0) will be made private; do not access directly, use getter/setter
  • Constructor Details

    • Instruction

      Instruction()
      Empty constructor needed for Instruction.readInstruction. Not to be used otherwise.
    • Instruction

      public Instruction(short opcode, short length)
      Constructs an Instruction.
      Parameters:
      opcode - The opcode.
      length - The instruction length.
  • Method Details

    • getComparator

      @Deprecated public static InstructionComparator getComparator()
      Deprecated.
      (6.0) use the built in comparator, or wrap this class in another object that implements these methods
      Gets Comparator object used in the equals() method to determine equality of instructions.
      Returns:
      currently used comparator for equals().
    • isNonNegativeUShort

      static boolean isNonNegativeUShort(int value)
      Tests if the value can fit in a non-negative short.
      Parameters:
      value - The value to check.
      Returns:
      true if the value is in range.
    • isValidByte

      public static boolean isValidByte(int value)
      Tests if the value can fit in a byte (signed).
      Parameters:
      value - The value to check.
      Returns:
      true if the value is in range.
      Since:
      6.0
    • isValidShort

      public static boolean isValidShort(int value)
      Tests if the value can fit in a short (signed).
      Parameters:
      value - The value to check.
      Returns:
      true if the value is in range.
      Since:
      6.0
    • readInstruction

      static Instruction readInstruction(byte[] code) throws IOException
      Reads an instruction from (byte code) input stream and return the appropriate object.
      Parameters:
      code - byte array containing the instruction to read.
      Returns:
      instruction object being read.
      Throws:
      IOException - Thrown when an I/O exception of some sort has occurred.
    • readInstruction

      public static Instruction readInstruction(ByteSequence bytes) throws IOException
      Reads an instruction from (byte code) input stream and return the appropriate object.

      If the Instruction is defined in InstructionConst, then the singleton instance is returned.

      Parameters:
      bytes - input stream bytes.
      Returns:
      instruction object being read.
      Throws:
      IOException - Thrown when an I/O exception of some sort has occurred.
      See Also:
    • setComparator

      @Deprecated public static void setComparator(InstructionComparator c)
      Deprecated.
      (6.0) use the built in comparator, or wrap this class in another object that implements these methods
      Sets comparator to be used for equals().
      Parameters:
      c - The comparator.
    • accept

      public abstract void accept(Visitor v)
      Call corresponding visitor method(s). The order is: Call visitor methods of implemented interfaces first, then call methods according to the class hierarchy in descending order, that is, the most specific visitXXX() call comes last.
      Parameters:
      v - Visitor object.
    • consumeStack

      public int consumeStack(ConstantPoolGen cpg)
      This method also gives right results for instructions whose effect on the stack depends on the constant pool entry they reference.
      Parameters:
      cpg - The constant pool generator.
      Returns:
      Number of words consumed from stack by this instruction, or Constants.UNPREDICTABLE, if this cannot be computed statically.
    • copy

      public Instruction copy()
      Use with caution, since 'BranchInstruction's have a 'target' reference which is not copied correctly (only basic types are). This also applies for 'Select' instructions with their multiple branch targets.
      Returns:
      (shallow) copy of an instruction.
      See Also:
    • dispose

      void dispose()
      Some instructions may be reused, so don't do anything by default.
    • dump

      public void dump(DataOutputStream out) throws IOException
      Dumps instruction as byte code to stream out.
      Parameters:
      out - Output stream.
      Throws:
      IOException - Thrown when an I/O exception of some sort has occurred.
    • dumpToByteArray

      byte[] dumpToByteArray() throws IOException
      Dumps this instruction to a byte array.
      Returns:
      the byte array containing the dumped instruction
      Throws:
      IOException - Thrown if an I/O error occurs.
    • equals

      public boolean equals(Object that)
      Tests for equality, delegated to comparator
      Overrides:
      equals in class Object
      Returns:
      true if that is an Instruction and has the same opcode.
    • getLength

      public int getLength()
      Gets the length (in bytes) of instruction.
      Returns:
      length (in bytes) of instruction.
    • getName

      public String getName()
      Gets the name of instruction, that is, opcode name.
      Returns:
      name of instruction, that is, opcode name.
    • getOpcode

      public short getOpcode()
      Gets this instruction's opcode.
      Returns:
      this instruction's opcode.
    • hashCode

      public int hashCode()
      Gets the hashCode of this object.
      Overrides:
      hashCode in class Object
      Returns:
      The hashCode.
      Since:
      6.0
    • initFromFile

      protected void initFromFile(ByteSequence bytes, boolean wide) throws IOException
      Reads needed data (for example index) from file.
      Parameters:
      bytes - byte sequence to read from.
      wide - "wide" instruction flag.
      Throws:
      IOException - Thrown if the implementation needs to read data from the file
    • produceStack

      public int produceStack(ConstantPoolGen cpg)
      This method also gives right results for instructions whose effect on the stack depends on the constant pool entry they reference.
      Parameters:
      cpg - The constant pool generator.
      Returns:
      Number of words produced onto stack by this instruction, or Constants.UNPREDICTABLE, if this cannot be computed statically.
    • setLength

      final void setLength(int length)
      Needed in readInstruction and subclasses in this package
      Since:
      6.0
    • setOpcode

      final void setOpcode(short opcode)
      Needed in readInstruction and subclasses in this package.
    • toString

      public String toString()
      Overrides:
      toString in class Object
      Returns:
      mnemonic for instruction in verbose format.
    • toString

      public String toString(boolean verbose)
      Long output format: <name of opcode> "["<opcode number>"]" "("<length of instruction>")"
      Parameters:
      verbose - long/short format switch.
      Returns:
      mnemonic for instruction.
    • toString

      public String toString(ConstantPool cp)
      Gets the mnemonic for instruction with symbolic references resolved.
      Parameters:
      cp - The constant pool.
      Returns:
      mnemonic for instruction with symbolic references resolved.