FileDocCategorySizeDatePackage
BranchInstruction.javaAPI DocJava SE 5 API8182Fri Aug 26 14:55:18 BST 2005com.sun.org.apache.bcel.internal.generic

BranchInstruction

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.
see
InstructionList
version
$Id: BranchInstruction.java,v 1.1.1.1 2001/10/29 20:00:07 jvanzyl Exp $
author
M. Dahm

Fields Summary
protected int
index
protected InstructionHandle
target
protected int
position
Constructors Summary
BranchInstruction()
Empty constructor needed for the Class.newInstance() statement in Instruction.readInstruction(). Not to be used otherwise.

protected BranchInstruction(short opcode, InstructionHandle target)
Common super constructor

param
opcodee Instruction opcode
param
target instruction to branch to

    super(opcode, (short)3);
    setTarget(target);
  
Methods Summary
public booleancontainsTarget(com.sun.org.apache.bcel.internal.generic.InstructionHandle ih)

return
true, if ih is target of this instruction

    return (target == ih);
  
voiddispose()
Inform target that it's not targeted anymore.

    setTarget(null);
    index=-1;
    position=-1;
  
public voiddump(java.io.DataOutputStream out)
Dump instruction as byte code to stream out.

param
out Output stream

    out.writeByte(opcode);

    index = getTargetOffset();

    if(Math.abs(index) >= 32767) // too large for short
      throw new ClassGenException("Branch target offset too large for short");

    out.writeShort(index); // May be negative, i.e., point backwards
  
public final intgetIndex()

return
target offset in byte code

 return index; 
public com.sun.org.apache.bcel.internal.generic.InstructionHandlegetTarget()

return
target of branch instruction

 return target; 
protected intgetTargetOffset(com.sun.org.apache.bcel.internal.generic.InstructionHandle target)

param
target branch target
return
the offset to `target' relative to this instruction

    if(target == null)
      throw new ClassGenException("Target of " + super.toString(true) + 
				  " is invalid null handle");

    int t = target.getPosition();

    if(t < 0)
      throw new ClassGenException("Invalid branch target position offset for " +
				  super.toString(true) + ":" + t + ":" + target);

    return t - position;
  
protected intgetTargetOffset()

return
the offset to this instruction's target

 return getTargetOffset(target); 
protected voidinitFromFile(com.sun.org.apache.bcel.internal.util.ByteSequence bytes, boolean wide)
Read needed data (e.g. index) from file. Conversion to a InstructionHandle is done in InstructionList(byte[]).

param
bytes input stream
param
wide wide prefix?
see
InstructionList

    length = 3;
    index  = bytes.readShort();
  
static final voidnotifyTarget(com.sun.org.apache.bcel.internal.generic.InstructionHandle old_ih, com.sun.org.apache.bcel.internal.generic.InstructionHandle new_ih, com.sun.org.apache.bcel.internal.generic.InstructionTargeter t)
Used by BranchInstruction, LocalVariableGen, CodeExceptionGen

    if(old_ih != null)
      old_ih.removeTargeter(t);
    if(new_ih != null)
      new_ih.addTargeter(t);
  
public voidsetTarget(com.sun.org.apache.bcel.internal.generic.InstructionHandle target)
Set branch target

param
target branch target

    notifyTarget(this.target, target, this);
    this.target = target;
  
public java.lang.StringtoString(boolean verbose)
Long output format: <position in byte code> <name of opcode> "["<opcode number>"]" "("<length of instruction>")" "<"<target instruction>">" "@"<branch target offset>

param
verbose long/short format switch
return
mnemonic for instruction

    String s = super.toString(verbose);
    String t = "null";

    if(verbose) {
      if(target != null) {
	if(target.getInstruction() == this)
	  t = "<points to itself>";
	else if(target.getInstruction() == null)
	  t = "<null instruction!!!?>";
	else
	  t = target.getInstruction().toString(false); // Avoid circles
      }
    } else {
      if(target != null) {
	index = getTargetOffset();
	t = "" + (index + position);
      }
    }

    return s + " -> " + t;
  
protected intupdatePosition(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.

param
offset additional offset caused by preceding (variable length) instructions
param
max_offset the maximum offset that may be caused by these instructions
return
additional offset caused by possible change of this instruction's length

    position += offset;
    return 0;
  
public voidupdateTarget(com.sun.org.apache.bcel.internal.generic.InstructionHandle old_ih, com.sun.org.apache.bcel.internal.generic.InstructionHandle new_ih)

param
old_ih old target
param
new_ih new target

    if(target == old_ih)
      setTarget(new_ih);
    else
      throw new ClassGenException("Not targeting " + old_ih + ", but " + target);