joeq.Compiler.BytecodeAnalysis
Class Bytecodes.InstructionList

java.lang.Object
  extended by joeq.Compiler.BytecodeAnalysis.Bytecodes.InstructionList
All Implemented Interfaces:
Serializable
Enclosing interface:
Bytecodes

public static class Bytecodes.InstructionList
extends Object
implements Serializable

See Also:
Serialized Form

Constructor Summary
Bytecodes.InstructionList()
          Create (empty) instruction list.
Bytecodes.InstructionList(Bytecodes.BranchInstruction i)
          Create instruction list containing one instruction.
Bytecodes.InstructionList(Bytecodes.CompoundInstruction c)
          Initialize list with (nonnull) compound instruction.
Bytecodes.InstructionList(Bytecodes.Instruction i)
          Create instruction list containing one instruction.
Bytecodes.InstructionList(jq_ConstantPool cp, byte[] code)
          Initialize instruction list from byte array.
Bytecodes.InstructionList(jq_Method m)
           
 
Method Summary
 void accept(Bytecodes.Visitor v)
          Convenience method, simply calls accept() on the contained instructions.
 void addObserver(Bytecodes.InstructionListObserver o)
          Add observer for this object.
 Bytecodes.BranchHandle append(Bytecodes.BranchInstruction i)
          Append a branch instruction to the end of this list.
 Bytecodes.InstructionHandle append(Bytecodes.CompoundInstruction c)
          Append a compound instruction.
 Bytecodes.InstructionHandle append(Bytecodes.Instruction i)
          Append an instruction to the end of this list.
 Bytecodes.InstructionHandle append(Bytecodes.Instruction i, Bytecodes.CompoundInstruction c)
          Append a compound instruction, after instruction i.
 Bytecodes.InstructionHandle append(Bytecodes.Instruction i, Bytecodes.Instruction j)
          Append a single instruction j after another instruction i, which must be in this list of course!
 Bytecodes.InstructionHandle append(Bytecodes.Instruction i, Bytecodes.InstructionList il)
          Append another list after instruction i contained in this list.
 Bytecodes.BranchHandle append(Bytecodes.InstructionHandle ih, Bytecodes.BranchInstruction i)
          Append an instruction after instruction (handle) ih contained in this list.
 Bytecodes.InstructionHandle append(Bytecodes.InstructionHandle ih, Bytecodes.CompoundInstruction c)
          Append a compound instruction.
 Bytecodes.InstructionHandle append(Bytecodes.InstructionHandle ih, Bytecodes.Instruction i)
          Append an instruction after instruction (handle) ih contained in this list.
 Bytecodes.InstructionHandle append(Bytecodes.InstructionHandle ih, Bytecodes.InstructionList il)
          Append another list after instruction (handle) ih contained in this list.
 Bytecodes.InstructionHandle append(Bytecodes.InstructionList il)
          Append another list to this one.
 boolean contains(Bytecodes.Instruction i)
           
 boolean contains(Bytecodes.InstructionHandle i)
           
 Bytecodes.InstructionList copy()
           
 void delete(Bytecodes.Instruction i)
          Remove instruction from this list.
 void delete(Bytecodes.Instruction from, Bytecodes.Instruction to)
          Remove instructions from instruction `from' to instruction `to' contained in this list.
 void delete(Bytecodes.InstructionHandle ih)
          Remove instruction from this list.
 void delete(Bytecodes.InstructionHandle from, Bytecodes.InstructionHandle to)
          Remove instructions from instruction `from' to instruction `to' contained in this list.
 void dispose()
          Delete contents of list.
 Enumeration elements()
           
 Bytecodes.InstructionHandle findHandle(int pos)
          Get instruction handle for instruction at byte code position pos.
static Bytecodes.InstructionHandle findHandle(List ihs, int[] pos, int target)
          Find the target instruction (handle) that corresponds to the given target position (byte code offset).
 byte[] getByteCode()
          When everything is finished, use this method to convert the instruction list into an array of bytes.
 Bytecodes.InstructionHandle getEnd()
           
 List getInstructionHandles()
           
 int[] getInstructionPositions()
          Get positions (offsets) of all instructions in the list.
 List getInstructions(jq_ConstantPool cp)
           
 int getLength()
           
 Bytecodes.InstructionHandle getStart()
           
 Bytecodes.BranchHandle insert(Bytecodes.BranchInstruction i)
          Insert a branch instruction at start of this list.
 Bytecodes.InstructionHandle insert(Bytecodes.CompoundInstruction c)
          Insert a compound instruction.
 Bytecodes.InstructionHandle insert(Bytecodes.Instruction i)
          Insert an instruction at start of this list.
 Bytecodes.InstructionHandle insert(Bytecodes.Instruction i, Bytecodes.CompoundInstruction c)
          Insert a compound instruction before instruction i.
 Bytecodes.InstructionHandle insert(Bytecodes.Instruction i, Bytecodes.Instruction j)
          Insert a single instruction j before another instruction i, which must be in this list of course!
 Bytecodes.InstructionHandle insert(Bytecodes.Instruction i, Bytecodes.InstructionList il)
          Insert another list before Instruction i contained in this list.
 Bytecodes.BranchHandle insert(Bytecodes.InstructionHandle ih, Bytecodes.BranchInstruction i)
          Insert an instruction before instruction (handle) ih contained in this list.
 Bytecodes.InstructionHandle insert(Bytecodes.InstructionHandle ih, Bytecodes.CompoundInstruction c)
          Insert a compound instruction.
 Bytecodes.InstructionHandle insert(Bytecodes.InstructionHandle ih, Bytecodes.Instruction i)
          Insert an instruction before instruction (handle) ih contained in this list.
 Bytecodes.InstructionHandle insert(Bytecodes.InstructionHandle ih, Bytecodes.InstructionList il)
          Insert another list before Instruction handle ih contained in this list.
 Bytecodes.InstructionHandle insert(Bytecodes.InstructionList il)
          Insert another list.
 boolean isEmpty()
          Test for empty list.
 ListIterator iterator()
           
 void move(Bytecodes.InstructionHandle ih, Bytecodes.InstructionHandle target)
          Move a single instruction (handle) to a new location.
 void move(Bytecodes.InstructionHandle start, Bytecodes.InstructionHandle end, Bytecodes.InstructionHandle target)
          Take all instructions (handles) from "start" to "end" and append them after the new location "target".
 void redirectBranches(Bytecodes.InstructionHandle old_target, Bytecodes.InstructionHandle new_target)
          Redirect all references from old_target to new_target, i.e., update targets of branch instructions.
 void redirectExceptionHandlers(Bytecodes.CodeException[] exceptions, Bytecodes.InstructionHandle old_target, Bytecodes.InstructionHandle new_target)
          Redirect all references of exception handlers from old_target to new_target.
 void removeObserver(Bytecodes.InstructionListObserver o)
          Remove observer for this object.
 void setPositions()
           
 void setPositions(boolean check)
          Give all instructions their position number (offset in byte stream), i.e., make the list ready to be dumped.
 int size()
           
 String toString()
           
 String toString(boolean verbose)
           
 void update()
          Call notify() method on all observers.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

Bytecodes.InstructionList

public Bytecodes.InstructionList()
Create (empty) instruction list.


Bytecodes.InstructionList

public Bytecodes.InstructionList(Bytecodes.Instruction i)
Create instruction list containing one instruction.

Parameters:
i - initial instruction

Bytecodes.InstructionList

public Bytecodes.InstructionList(Bytecodes.BranchInstruction i)
Create instruction list containing one instruction.

Parameters:
i - initial instruction

Bytecodes.InstructionList

public Bytecodes.InstructionList(Bytecodes.CompoundInstruction c)
Initialize list with (nonnull) compound instruction. Consumes argument list, i.e., it becomes empty.

Parameters:
c - compound instruction (list)

Bytecodes.InstructionList

public Bytecodes.InstructionList(jq_Method m)

Bytecodes.InstructionList

public Bytecodes.InstructionList(jq_ConstantPool cp,
                                 byte[] code)
Initialize instruction list from byte array.

Parameters:
code - byte array containing the instructions
Method Detail

isEmpty

public boolean isEmpty()
Test for empty list.


findHandle

public static Bytecodes.InstructionHandle findHandle(List ihs,
                                                     int[] pos,
                                                     int target)
Find the target instruction (handle) that corresponds to the given target position (byte code offset).

Parameters:
ihs - array of instruction handles, i.e. il.getInstructionHandles()
pos - array of positions corresponding to ihs, i.e. il.getInstructionPositions()
target - target position to search for
Returns:
target position's instruction handle if available

findHandle

public Bytecodes.InstructionHandle findHandle(int pos)
Get instruction handle for instruction at byte code position pos. This only works properly, if the list is freshly initialized from a byte array or setPositions() has been called before this method.

Parameters:
pos - byte code position to search for
Returns:
target position's instruction handle if available

append

public Bytecodes.InstructionHandle append(Bytecodes.InstructionHandle ih,
                                          Bytecodes.InstructionList il)
Append another list after instruction (handle) ih contained in this list. Consumes argument list, i.e., it becomes empty.

Parameters:
ih - where to append the instruction list
il - Instruction list to append to this one
Returns:
instruction handle pointing to the first appended instruction

append

public Bytecodes.InstructionHandle append(Bytecodes.Instruction i,
                                          Bytecodes.InstructionList il)
Append another list after instruction i contained in this list. Consumes argument list, i.e., it becomes empty.

Parameters:
i - where to append the instruction list
il - Instruction list to append to this one
Returns:
instruction handle pointing to the first appended instruction

append

public Bytecodes.InstructionHandle append(Bytecodes.InstructionList il)
Append another list to this one. Consumes argument list, i.e., it becomes empty.

Parameters:
il - list to append to end of this list
Returns:
instruction handle of the first appended instruction

append

public Bytecodes.InstructionHandle append(Bytecodes.Instruction i)
Append an instruction to the end of this list.

Parameters:
i - instruction to append
Returns:
instruction handle of the appended instruction

append

public Bytecodes.BranchHandle append(Bytecodes.BranchInstruction i)
Append a branch instruction to the end of this list.

Parameters:
i - branch instruction to append
Returns:
branch instruction handle of the appended instruction

append

public Bytecodes.InstructionHandle append(Bytecodes.Instruction i,
                                          Bytecodes.Instruction j)
Append a single instruction j after another instruction i, which must be in this list of course!

Parameters:
i - Instruction in list
j - Instruction to append after i in list
Returns:
instruction handle of the first appended instruction

append

public Bytecodes.InstructionHandle append(Bytecodes.Instruction i,
                                          Bytecodes.CompoundInstruction c)
Append a compound instruction, after instruction i.

Parameters:
i - Instruction in list
c - The composite instruction (containing an InstructionList)
Returns:
instruction handle of the first appended instruction

append

public Bytecodes.InstructionHandle append(Bytecodes.CompoundInstruction c)
Append a compound instruction.

Parameters:
c - The composite instruction (containing an InstructionList)
Returns:
instruction handle of the first appended instruction

append

public Bytecodes.InstructionHandle append(Bytecodes.InstructionHandle ih,
                                          Bytecodes.CompoundInstruction c)
Append a compound instruction.

Parameters:
ih - where to append the instruction list
c - The composite instruction (containing an InstructionList)
Returns:
instruction handle of the first appended instruction

append

public Bytecodes.InstructionHandle append(Bytecodes.InstructionHandle ih,
                                          Bytecodes.Instruction i)
Append an instruction after instruction (handle) ih contained in this list.

Parameters:
ih - where to append the instruction list
i - Instruction to append
Returns:
instruction handle pointing to the first appended instruction

append

public Bytecodes.BranchHandle append(Bytecodes.InstructionHandle ih,
                                     Bytecodes.BranchInstruction i)
Append an instruction after instruction (handle) ih contained in this list.

Parameters:
ih - where to append the instruction list
i - Instruction to append
Returns:
instruction handle pointing to the first appended instruction

insert

public Bytecodes.InstructionHandle insert(Bytecodes.InstructionHandle ih,
                                          Bytecodes.InstructionList il)
Insert another list before Instruction handle ih contained in this list. Consumes argument list, i.e., it becomes empty.

Parameters:
ih - where to append the instruction list
il - instruction list to insert
Returns:
instruction handle of the first inserted instruction

insert

public Bytecodes.InstructionHandle insert(Bytecodes.InstructionList il)
Insert another list.

Parameters:
il - list to insert before start of this list
Returns:
instruction handle of the first inserted instruction

insert

public Bytecodes.InstructionHandle insert(Bytecodes.Instruction i,
                                          Bytecodes.InstructionList il)
Insert another list before Instruction i contained in this list. Consumes argument list, i.e., it becomes empty.

Parameters:
i - where to append the instruction list
il - Instruction list to insert
Returns:
instruction handle pointing to the first inserted instruction, i.e., il.getStart()

insert

public Bytecodes.InstructionHandle insert(Bytecodes.Instruction i)
Insert an instruction at start of this list.

Parameters:
i - instruction to insert
Returns:
instruction handle of the inserted instruction

insert

public Bytecodes.BranchHandle insert(Bytecodes.BranchInstruction i)
Insert a branch instruction at start of this list.

Parameters:
i - branch instruction to insert
Returns:
branch instruction handle of the appended instruction

insert

public Bytecodes.InstructionHandle insert(Bytecodes.Instruction i,
                                          Bytecodes.Instruction j)
Insert a single instruction j before another instruction i, which must be in this list of course!

Parameters:
i - Instruction in list
j - Instruction to insert before i in list
Returns:
instruction handle of the first inserted instruction

insert

public Bytecodes.InstructionHandle insert(Bytecodes.Instruction i,
                                          Bytecodes.CompoundInstruction c)
Insert a compound instruction before instruction i.

Parameters:
i - Instruction in list
c - The composite instruction (containing an InstructionList)
Returns:
instruction handle of the first inserted instruction

insert

public Bytecodes.InstructionHandle insert(Bytecodes.CompoundInstruction c)
Insert a compound instruction.

Parameters:
c - The composite instruction (containing an InstructionList)
Returns:
instruction handle of the first inserted instruction

insert

public Bytecodes.InstructionHandle insert(Bytecodes.InstructionHandle ih,
                                          Bytecodes.Instruction i)
Insert an instruction before instruction (handle) ih contained in this list.

Parameters:
ih - where to insert to the instruction list
i - Instruction to insert
Returns:
instruction handle of the first inserted instruction

insert

public Bytecodes.InstructionHandle insert(Bytecodes.InstructionHandle ih,
                                          Bytecodes.CompoundInstruction c)
Insert a compound instruction.

Parameters:
ih - where to insert the instruction list
c - The composite instruction (containing an InstructionList)
Returns:
instruction handle of the first inserted instruction

insert

public Bytecodes.BranchHandle insert(Bytecodes.InstructionHandle ih,
                                     Bytecodes.BranchInstruction i)
Insert an instruction before instruction (handle) ih contained in this list.

Parameters:
ih - where to insert to the instruction list
i - Instruction to insert
Returns:
instruction handle of the first inserted instruction

move

public void move(Bytecodes.InstructionHandle start,
                 Bytecodes.InstructionHandle end,
                 Bytecodes.InstructionHandle target)
Take all instructions (handles) from "start" to "end" and append them after the new location "target". Of course, "end" must be after "start" and target must not be located withing this range. If you want to move something to the start of the list use null as value for target.
Any instruction targeters pointing to handles within the block, keep their targets.

Parameters:
start - of moved block
end - of moved block
target - of moved block

move

public void move(Bytecodes.InstructionHandle ih,
                 Bytecodes.InstructionHandle target)
Move a single instruction (handle) to a new location.

Parameters:
ih - moved instruction
target - new location of moved instruction

delete

public void delete(Bytecodes.InstructionHandle ih)
            throws Bytecodes.TargetLostException
Remove instruction from this list. The corresponding Instruction handles must not be reused!

Parameters:
ih - instruction (handle) to remove
Throws:
Bytecodes.TargetLostException

delete

public void delete(Bytecodes.Instruction i)
            throws Bytecodes.TargetLostException
Remove instruction from this list. The corresponding Instruction handles must not be reused!

Parameters:
i - instruction to remove
Throws:
Bytecodes.TargetLostException

delete

public void delete(Bytecodes.InstructionHandle from,
                   Bytecodes.InstructionHandle to)
            throws Bytecodes.TargetLostException
Remove instructions from instruction `from' to instruction `to' contained in this list. The user must ensure that `from' is an instruction before `to', or risk havoc. The corresponding Instruction handles must not be reused!

Parameters:
from - where to start deleting (inclusive)
to - where to end deleting (inclusive)
Throws:
Bytecodes.TargetLostException

delete

public void delete(Bytecodes.Instruction from,
                   Bytecodes.Instruction to)
            throws Bytecodes.TargetLostException
Remove instructions from instruction `from' to instruction `to' contained in this list. The user must ensure that `from' is an instruction before `to', or risk havoc. The corresponding Instruction handles must not be reused!

Parameters:
from - where to start deleting (inclusive)
to - where to end deleting (inclusive)
Throws:
Bytecodes.TargetLostException

contains

public boolean contains(Bytecodes.InstructionHandle i)

contains

public boolean contains(Bytecodes.Instruction i)

setPositions

public void setPositions()

setPositions

public void setPositions(boolean check)
Give all instructions their position number (offset in byte stream), i.e., make the list ready to be dumped.

Parameters:
check - Perform sanity checks, e.g. if all targeted instructions really belong to this list

getByteCode

public byte[] getByteCode()
When everything is finished, use this method to convert the instruction list into an array of bytes.

Returns:
the byte code ready to be dumped

getInstructions

public List getInstructions(jq_ConstantPool cp)
Returns:
an array of instructions without target information for branch instructions.

toString

public String toString()
Overrides:
toString in class Object

toString

public String toString(boolean verbose)
Parameters:
verbose - toggle output format
Returns:
String containing all instructions in this list.

elements

public Enumeration elements()
Returns:
Enumeration that lists all instructions (handles)

iterator

public ListIterator iterator()
Returns:
Enumeration that lists all instructions (handles)

getInstructionHandles

public List getInstructionHandles()
Returns:
array containing all instructions (handles)

getInstructionPositions

public int[] getInstructionPositions()
Get positions (offsets) of all instructions in the list. This relies on that the list has been freshly created from an byte code array, or that setPositions() has been called. Otherwise this may be inaccurate.

Returns:
array containing all instruction's offset in byte code

copy

public Bytecodes.InstructionList copy()
Returns:
complete, i.e., deep copy of this list

dispose

public void dispose()
Delete contents of list. Provides better memory utilization, because the system then may reuse the instruction handles. This method is typically called right after MethodGen.getMethod().


getStart

public Bytecodes.InstructionHandle getStart()
Returns:
start of list

getEnd

public Bytecodes.InstructionHandle getEnd()
Returns:
end of list

getLength

public int getLength()
Returns:
length of list (Number of instructions, not bytes)

size

public int size()
Returns:
length of list (Number of instructions, not bytes)

redirectBranches

public void redirectBranches(Bytecodes.InstructionHandle old_target,
                             Bytecodes.InstructionHandle new_target)
Redirect all references from old_target to new_target, i.e., update targets of branch instructions.

Parameters:
old_target - the old target instruction handle
new_target - the new target instruction handle

redirectExceptionHandlers

public void redirectExceptionHandlers(Bytecodes.CodeException[] exceptions,
                                      Bytecodes.InstructionHandle old_target,
                                      Bytecodes.InstructionHandle new_target)
Redirect all references of exception handlers from old_target to new_target.

Parameters:
exceptions - array of exception handlers
old_target - the old target instruction handle
new_target - the new target instruction handle

addObserver

public void addObserver(Bytecodes.InstructionListObserver o)
Add observer for this object.


removeObserver

public void removeObserver(Bytecodes.InstructionListObserver o)
Remove observer for this object.


update

public void update()
Call notify() method on all observers. This method is not called automatically whenever the state has changed, but has to be called by the user after he has finished editing the object.


accept

public void accept(Bytecodes.Visitor v)
Convenience method, simply calls accept() on the contained instructions.

Parameters:
v - Visitor object


Copyright © 2001-2008 John Whaley. All Rights Reserved.