org.apache.bcel.generic

Class InstructionHandle

Implemented Interfaces:
java.io.Serializable
Known Direct Subclasses:
BranchHandle

public class InstructionHandle
extends java.lang.Object
implements java.io.Serializable

Instances of this class give users a handle to the instructions contained in an InstructionList. Instruction objects may be used more than once within a list, this is useful because it saves memory and may be much faster. Within an InstructionList an InstructionHandle object is wrapped around all instructions, i.e., it implements a cell in a doubly-linked list. From the outside only the next and the previous instruction (handle) are accessible. One can traverse the list via an Enumeration returned by InstructionList.elements().
Version:
$Id: InstructionHandle.java,v 1.1.1.1 2001/10/29 20:00:19 jvanzyl Exp $
Author:
M. Dahm
See Also:
Instruction, BranchHandle, InstructionList, Serialized Form

Field Summary

protected int
i_position

Constructor Summary

InstructionHandle(Instruction i)

Method Summary

void
accept(Visitor v)
Convenience method, simply calls accept() on the contained instruction.
void
addAttribute(Object key, Object attr)
Add an attribute to an instruction handle.
protected void
addHandle()
Overridden in BranchHandle
void
addTargeter(InstructionTargeter t)
Denote this handle is being referenced by t.
Object
getAttribute(Object key)
Get attribute of an instruction handle.
Collection
getAttributes()
Instruction
getInstruction()
InstructionHandle
getNext()
int
getPosition()
InstructionHandle
getPrev()
InstructionTargeter[]
getTargeters()
boolean
hasTargeters()
void
removeAllTargeters()
Remove all targeters, if any.
void
removeAttribute(Object key)
Delete an attribute of an instruction handle.
void
removeTargeter(InstructionTargeter t)
Denote this handle isn't referenced anymore by t.
void
setInstruction(Instruction i)
Replace current instruction contained in this handle.
Instruction
swapInstruction(Instruction i)
Temporarily swap the current instruction, without disturbing anything.
String
toString()
String
toString(boolean verbose)
protected int
updatePosition(int offset, int max_offset)
Called by InstructionList.setPositions when setting the position for every instruction.

Field Details

i_position

protected int i_position

Constructor Details

InstructionHandle

protected InstructionHandle(Instruction i)

Method Details

accept

public void accept(Visitor v)
Convenience method, simply calls accept() on the contained instruction.
Parameters:
v - Visitor object

addAttribute

public void addAttribute(Object key,
                         Object attr)
Add an attribute to an instruction handle.
Parameters:
key - the key object to store/retrieve the attribute
attr - the attribute to associate with this handle

addHandle

protected void addHandle()
Overridden in BranchHandle

addTargeter

public void addTargeter(InstructionTargeter t)
Denote this handle is being referenced by t.

getAttribute

public Object getAttribute(Object key)
Get attribute of an instruction handle.
Parameters:
key - the key object to store/retrieve the attribute

getAttributes

public Collection getAttributes()
Returns:
all attributes associated with this handle

getInstruction

public final Instruction getInstruction()

getNext

public final InstructionHandle getNext()

getPosition

public int getPosition()
Returns:
the position, i.e., the byte code offset of the contained instruction. This is accurate only after InstructionList.setPositions() has been called.

getPrev

public final InstructionHandle getPrev()

getTargeters

public InstructionTargeter[] getTargeters()
Returns:
null, if there are no targeters

hasTargeters

public boolean hasTargeters()

removeAllTargeters

public void removeAllTargeters()
Remove all targeters, if any.

removeAttribute

public void removeAttribute(Object key)
Delete an attribute of an instruction handle.
Parameters:
key - the key object to retrieve the attribute

removeTargeter

public void removeTargeter(InstructionTargeter t)
Denote this handle isn't referenced anymore by t.

setInstruction

public void setInstruction(Instruction i)
Replace current instruction contained in this handle. Old instruction is disposed using Instruction.dispose().

swapInstruction

public Instruction swapInstruction(Instruction i)
Temporarily swap the current instruction, without disturbing anything. Meant to be used by a debugger, implementing breakpoints. Current instruction is returned.

toString

public String toString()
Returns:
a string representation of the contained instruction.

toString

public String toString(boolean verbose)
Returns:
a (verbose) string representation of the contained 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