FileDocCategorySizeDatePackage
InstructionHandle.javaAPI DocJava SE 6 API9349Tue Jun 10 00:22:20 BST 2008com.sun.org.apache.bcel.internal.generic

InstructionHandle

public class InstructionHandle extends Object implements 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.2.1 2005/07/31 23:45:01 jeffsuttor Exp $
author
M. Dahm
see
Instruction
see
BranchHandle
see
InstructionList

Fields Summary
InstructionHandle
next
InstructionHandle
prev
Instruction
instruction
protected int
i_position
private HashSet
targeters
private HashMap
attributes
private static InstructionHandle
ih_list
Constructors Summary
protected InstructionHandle(Instruction i)

    setInstruction(i);
  
Methods Summary
public voidaccept(com.sun.org.apache.bcel.internal.generic.Visitor v)
Convenience method, simply calls accept() on the contained instruction.

param
v Visitor object

    instruction.accept(v);
  
public voidaddAttribute(java.lang.Object key, java.lang.Object attr)
Add an attribute to an instruction handle.

param
key the key object to store/retrieve the attribute
param
attr the attribute to associate with this handle

    if(attributes == null)
      attributes = new HashMap(3);
    
    attributes.put(key, attr);
  
protected voidaddHandle()
Overridden in BranchHandle

    next    = ih_list;
    ih_list = this;
  
public voidaddTargeter(com.sun.org.apache.bcel.internal.generic.InstructionTargeter t)
Denote this handle is being referenced by t.

    if(targeters == null)
      targeters = new HashSet();

    //if(!targeters.contains(t))
    targeters.add(t);
  
voiddispose()
Delete contents, i.e., remove user access and make handle reusable.

    next = prev = null;
    instruction.dispose();
    instruction = null;
    i_position = -1;
    attributes = null;
    removeAllTargeters();
    addHandle();
  
public java.lang.ObjectgetAttribute(java.lang.Object key)
Get attribute of an instruction handle.

param
key the key object to store/retrieve the attribute

    if(attributes != null)
      return attributes.get(key);

    return null;
  
public java.util.CollectiongetAttributes()

return
all attributes associated with this handle

    return attributes.values();
  
public final com.sun.org.apache.bcel.internal.generic.InstructiongetInstruction()

 return instruction; 
static final com.sun.org.apache.bcel.internal.generic.InstructionHandlegetInstructionHandle(com.sun.org.apache.bcel.internal.generic.Instruction i)
Factory method.

 // List of reusable handles

       
       
    if(ih_list == null)
      return new InstructionHandle(i);
    else {
      InstructionHandle ih = ih_list;
      ih_list = ih.next;

      ih.setInstruction(i);

      return ih;
    }
  
public final com.sun.org.apache.bcel.internal.generic.InstructionHandlegetNext()


              return next; 
public intgetPosition()

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

 return i_position; 
public final com.sun.org.apache.bcel.internal.generic.InstructionHandlegetPrev()

 return prev; 
public com.sun.org.apache.bcel.internal.generic.InstructionTargeter[]getTargeters()

return
null, if there are no targeters

    if(!hasTargeters())
      return null;
    
    InstructionTargeter[] t = new InstructionTargeter[targeters.size()];
    targeters.toArray(t);
    return t;
  
public booleanhasTargeters()

    return (targeters != null) && (targeters.size() > 0);
  
public voidremoveAllTargeters()
Remove all targeters, if any.

    if(targeters != null)
      targeters.clear();
  
public voidremoveAttribute(java.lang.Object key)
Delete an attribute of an instruction handle.

param
key the key object to retrieve the attribute

    if(attributes != null)
      attributes.remove(key);
  
public voidremoveTargeter(com.sun.org.apache.bcel.internal.generic.InstructionTargeter t)
Denote this handle isn't referenced anymore by t.

    targeters.remove(t);
  
public voidsetInstruction(com.sun.org.apache.bcel.internal.generic.Instruction i)
Replace current instruction contained in this handle. Old instruction is disposed using Instruction.dispose().

 // Overridden in BranchHandle
    if(i == null)
      throw new ClassGenException("Assigning null to handle");

    if((this.getClass() != BranchHandle.class) && (i instanceof BranchInstruction))
      throw new ClassGenException("Assigning branch instruction " + i + " to plain handle");

    if(instruction != null)
      instruction.dispose();

    instruction = i;
  
voidsetPosition(int pos)
Set the position, i.e., the byte code offset of the contained instruction.

 i_position = pos; 
public com.sun.org.apache.bcel.internal.generic.InstructionswapInstruction(com.sun.org.apache.bcel.internal.generic.Instruction i)
Temporarily swap the current instruction, without disturbing anything. Meant to be used by a debugger, implementing breakpoints. Current instruction is returned.

    Instruction oldInstruction = instruction;
    instruction = i;
    return oldInstruction;
  
public java.lang.StringtoString(boolean verbose)

return
a (verbose) string representation of the contained instruction.

    return Utility.format(i_position, 4, false, ' ") + ": " + instruction.toString(verbose);
  
public java.lang.StringtoString()

return
a string representation of the contained instruction.

    return toString(true);
  
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

    i_position += offset;
    return 0;