org.apache.bcel.generic

Class BranchInstruction

Implemented Interfaces:
Cloneable, InstructionTargeter, Serializable
Known Direct Subclasses:
GotoInstruction, IfInstruction, JsrInstruction, Select

public abstract class BranchInstruction
extends Instruction
implements InstructionTargeter

Abstract super class for branching instructions like GOTO, IFEQ, etc.. Branch instructions may have a variable length, namely GOTO, JSR, LOOKUPSWITCH and TABLESWITCH.
Version:
$Id: BranchInstruction.java 386056 2006-03-15 11:31:56Z tcurdt $
Author:
M. Dahm
See Also:
InstructionList

Field Summary

protected int
index
protected int
position
protected InstructionHandle
target

Fields inherited from class org.apache.bcel.generic.Instruction

cmp, length, opcode

Constructor Summary

BranchInstruction()
Empty constructor needed for the Class.newInstance() statement in Instruction.readInstruction().
BranchInstruction(short opcode, InstructionHandle target)
Common super constructor

Method Summary

boolean
containsTarget(InstructionHandle ih)
(package private) void
dispose()
Inform target that it's not targeted anymore.
void
dump(DataOutputStream out)
Dump instruction as byte code to stream out.
int
getIndex()
InstructionHandle
getTarget()
protected int
getTargetOffset()
protected int
getTargetOffset(InstructionHandle _target)
protected void
initFromFile(ByteSequence bytes, boolean wide)
Read needed data (e.g.
(package private) static void
notifyTarget(InstructionHandle old_ih, InstructionHandle new_ih, InstructionTargeter t)
Used by BranchInstruction, LocalVariableGen, CodeExceptionGen
void
setTarget(InstructionHandle target)
Set 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 max_offset)
Called by InstructionList.setPositions when setting the position for every instruction.
void
updateTarget(InstructionHandle old_ih, InstructionHandle new_ih)

Methods inherited from class org.apache.bcel.generic.Instruction

accept, className, consumeStack, copy, dispose, dump, equals, getComparator, getLength, getName, getOpcode, initFromFile, produceStack, readInstruction, setComparator, setOpcode, toString, toString, toString

Field Details

index

protected int index

position

protected int position

target

protected InstructionHandle target

Constructor Details

BranchInstruction

(package private)  BranchInstruction()
Empty constructor needed for the Class.newInstance() statement in Instruction.readInstruction(). Not to be used otherwise.

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)
Specified by:
containsTarget in interface InstructionTargeter
Returns:
true, if ih is target of this instruction

dispose

(package private)  void dispose()
Inform target that it's not targeted anymore.
Overrides:
dispose in interface Instruction

dump

public void dump(DataOutputStream out)
            throws IOException
Dump instruction as byte code to stream out.
Overrides:
dump in interface Instruction
Parameters:
out - Output stream

getIndex

public final int getIndex()
Returns:
target offset in byte code

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
Read needed data (e.g. index) from file. Conversion to a InstructionHandle is done in InstructionList(byte[]).
Overrides:
initFromFile in interface Instruction
Parameters:
bytes - input stream
wide - wide prefix?

notifyTarget

(package private) static final void notifyTarget(InstructionHandle old_ih,
                                                 InstructionHandle new_ih,
                                                 InstructionTargeter t)
Used by BranchInstruction, LocalVariableGen, CodeExceptionGen

setTarget

public void setTarget(InstructionHandle target)
Set 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 interface Instruction
Parameters:
verbose - long/short format switch
Returns:
mnemonic for instruction

updatePosition

protected int updatePosition(int offset,
                             int max_offset)
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
max_offset - 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 old_ih,
                         InstructionHandle new_ih)
Specified by:
updateTarget in interface InstructionTargeter
Parameters:
old_ih - old target
new_ih - new target