# Class BranchInstruction

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

All Implemented Interfaces:
  `Cloneable, InstructionTargeter`

Direct Known Subclasses:
  `GotoInstruction, IfInstruction, JsrInstruction, Select`

public abstract class BranchInstruction extends Instruction implements InstructionTargeter
Abstract super class for branching instructions like GOTO, IFEQ, and so on. Branch instructions may have a variable length, namely GOTO, JSR, LOOKUPSWITCH and TABLESWITCH.

See Also:
  - `InstructionList`

- ## 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
  `protected int`
  `position`
  Deprecated.
  (since 6.0) will be made private; do not access directly, use getter/setter
  `protected InstructionHandle`
  `target`
  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`
  `BranchInstruction(short opcode, InstructionHandle target)`
  Common super constructor

- ## Method Summary

  Modifier and Type
  Method
  Description
  `boolean`
  `containsTarget(InstructionHandle ih)`
  Tests whether this targeter targets the specified instruction handle.
  `void`
  `dump(DataOutputStream out)`
  Dump instruction as byte code to stream out.
  `final int`
  `getIndex()`
  `protected int`
  `getPosition()`
  `InstructionHandle`
  `getTarget()`
  `protected int`
  `getTargetOffset()`
  `protected int`
  `getTargetOffset(InstructionHandle target)`
  `protected void`
  `initFromFile(ByteSequence bytes, boolean wide)`
  Reads needed data (for example index) from file.
  `protected void`
  `setIndex(int index)`
  `protected void`
  `setPosition(int position)`
  `void`
  `setTarget(InstructionHandle target)`
  Sets branch target
  `String`
  `toString(boolean verbose)`
  Long output format: <position in byte code> <name of opcode> "["<opcode number>"]" "("<length of instruction>")" "<"<target instruction>">" "@"<branch target offset>
  `protected int`
  `updatePosition(int offset, int maxOffset)`
  Called by InstructionList.setPositions when setting the position for every instruction.
  `void`
  `updateTarget(InstructionHandle oldIh, InstructionHandle newIh)`
  Replaces the target of this targeter from this old handle to the new handle.

  ### Methods inherited from class Instruction

  `accept, consumeStack, copy, equals, getComparator, getLength, getName, getOpcode, hashCode, isValidByte, isValidShort, produceStack, readInstruction, setComparator, toString, 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

  - ### target

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

  - ### position

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

- ## Constructor Details

  - ### BranchInstruction

    protected BranchInstruction(short opcode, InstructionHandle target)
    Common super constructor
    Parameters:
      `opcode` - Instruction opcode
      `target` - instruction to branch to

- ## Method Details

  - ### containsTarget

    public boolean containsTarget(InstructionHandle ih)
    Description copied from interface: `InstructionTargeter`
    Tests whether this targeter targets the specified instruction handle.
    Specified by:
      `containsTarget` in interface `InstructionTargeter`
    Parameters:
      `ih` - the instruction handle to test.
    Returns:
      true, if ih is target of this instruction

  - ### 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()
    Returns:
      target offset in byte code

  - ### getPosition

    protected int getPosition()
    Returns:
      the position
    Since:
      6.0

  - ### getTarget

    public InstructionHandle getTarget()
    Returns:
      target of branch instruction

  - ### getTargetOffset

    protected int getTargetOffset()
    Returns:
      the offset to this instruction's target

  - ### getTargetOffset

    protected int getTargetOffset(InstructionHandle target)
    Parameters:
      `target` - branch target
    Returns:
      the offset to 'target' relative to this instruction

  - ### initFromFile

    protected void initFromFile(ByteSequence bytes, boolean wide) throws IOException
    Reads needed data (for example index) from file. Conversion to a InstructionHandle is done in InstructionList(byte[]).
    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
    See Also:
      - `InstructionList`

  - ### setIndex

    protected void setIndex(int index)
    Parameters:
      `index` - the index to set
    Since:
      6.0

  - ### setPosition

    protected void setPosition(int position)
    Parameters:
      `position` - the position to set
    Since:
      6.0

  - ### setTarget

    public void setTarget(InstructionHandle target)
    Sets branch target
    Parameters:
      `target` - branch target

  - ### toString

    public String toString(boolean verbose)
    Long output format: <position in byte code> <name of opcode> "["<opcode number>"]" "("<length of instruction>")" "<"<target instruction>">" "@"<branch target offset>
    Overrides:
      `toString` in class `Instruction`
    Parameters:
      `verbose` - long/short format switch
    Returns:
      mnemonic for instruction

  - ### updatePosition

    protected int updatePosition(int offset, int maxOffset)
    Called by InstructionList.setPositions when setting the position for every instruction. In the presence of variable length instructions 'setPositions' performs multiple passes over the instruction list to calculate the correct (byte) positions and offsets by calling this function.
    Parameters:
      `offset` - additional offset caused by preceding (variable length) instructions
      `maxOffset` - the maximum offset that may be caused by these instructions
    Returns:
      additional offset caused by possible change of this instruction's length

  - ### updateTarget

    public void updateTarget(InstructionHandle oldIh, InstructionHandle newIh)
    Description copied from interface: `InstructionTargeter`
    Replaces the target of this targeter from this old handle to the new handle.
    Specified by:
      `updateTarget` in interface `InstructionTargeter`
    Parameters:
      `oldIh` - old target
      `newIh` - new target