This class is a container for a list of
Instruction objects. Instructions can
be appended, inserted, moved, deleted, etc.. Instructions are being
wrapped into
InstructionHandles objects that
are returned upon append/insert operations. They give the user
(read only) access to the list structure, such that it can be traversed and
manipulated in a controlled way.
A list is finally dumped to a byte code array with
getByteCode.
append
public BranchHandle append(BranchInstruction i)
Append a branch instruction to the end of this list.
i
- branch instruction to append
- branch instruction handle of the appended instruction
append
public InstructionHandle append(CompoundInstruction c)
Append a compound instruction.
c
- The composite instruction (containing an InstructionList)
- instruction handle of the first appended instruction
append
public InstructionHandle append(Instruction i)
Append an instruction to the end of this list.
i
- instruction to append
- instruction handle of the appended instruction
append
public InstructionHandle append(Instruction i,
CompoundInstruction c)
Append a compound instruction, after instruction i.
i
- Instruction in listc
- The composite instruction (containing an InstructionList)
- instruction handle of the first appended instruction
append
public InstructionHandle append(Instruction i,
Instruction j)
Append a single instruction j after another instruction i, which
must be in this list of course!
i
- Instruction in listj
- Instruction to append after i in list
- instruction handle of the first appended instruction
append
public InstructionHandle append(Instruction i,
InstructionList il)
Append another list after instruction i contained in this list.
Consumes argument list, i.e., it becomes empty.
i
- where to append the instruction listil
- Instruction list to append to this one
- instruction handle pointing to the first appended instruction
append
public BranchHandle append(InstructionHandle ih,
BranchInstruction i)
Append an instruction after instruction (handle) ih contained in this list.
ih
- where to append the instruction listi
- Instruction to append
- instruction handle pointing to the first appended instruction
append
public InstructionHandle append(InstructionHandle ih,
CompoundInstruction c)
Append a compound instruction.
ih
- where to append the instruction listc
- The composite instruction (containing an InstructionList)
- instruction handle of the first appended instruction
append
public InstructionHandle append(InstructionHandle ih,
Instruction i)
Append an instruction after instruction (handle) ih contained in this list.
ih
- where to append the instruction listi
- Instruction to append
- instruction handle pointing to the first appended instruction
append
public InstructionHandle append(InstructionHandle ih,
InstructionList il)
Append another list after instruction (handle) ih contained in this list.
Consumes argument list, i.e., it becomes empty.
ih
- where to append the instruction listil
- Instruction list to append to this one
- instruction handle pointing to the first appended instruction
append
public InstructionHandle append(InstructionList il)
Append another list to this one.
Consumes argument list, i.e., it becomes empty.
il
- list to append to end of this list
- instruction handle of the first appended instruction
delete
public void delete(Instruction i)
throws TargetLostException
Remove instruction from this list. The corresponding Instruction
handles must not be reused!
i
- instruction to remove
delete
public void delete(Instruction from,
Instruction to)
throws 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!
from
- where to start deleting (inclusive)to
- where to end deleting (inclusive)
delete
public void delete(InstructionHandle ih)
throws TargetLostException
Remove instruction from this list. The corresponding Instruction
handles must not be reused!
ih
- instruction (handle) to remove
delete
public void delete(InstructionHandle from,
InstructionHandle to)
throws 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!
from
- where to start deleting (inclusive)to
- where to end deleting (inclusive)
dispose
public void dispose()
Delete contents of list. Provides besser memory utilization,
because the system then may reuse the instruction handles. This
method is typically called right after
MethodGen.getMethod().
findHandle
public 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.
pos
- byte code position to search for
- target position's instruction handle if available
findHandle
public static InstructionHandle findHandle(InstructionHandle[] ihs,
int[] pos,
int count,
int target)
Find the target instruction (handle) that corresponds to the given target
position (byte code offset).
ihs
- array of instruction handles, i.e. il.getInstructionHandles()pos
- array of positions corresponding to ihs, i.e. il.getInstructionPositions()count
- length of arraystarget
- target position to search for
- target position's instruction handle if available
getByteCode
public byte[] getByteCode()
When everything is finished, use this method to convert the instruction
list into an array of bytes.
- the byte code ready to be dumped
getInstructionHandles
public InstructionHandle[] getInstructionHandles()
- 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.
- array containing all instruction's offset in byte code
getInstructions
public Instruction[] getInstructions()
- an array of instructions without target information for branch instructions.
getLength
public int getLength()
- length of list (Number of instructions, not bytes)
insert
public BranchHandle insert(BranchInstruction i)
Insert a branch instruction at start of this list.
i
- branch instruction to insert
- branch instruction handle of the appended instruction
insert
public InstructionHandle insert(CompoundInstruction c)
Insert a compound instruction.
c
- The composite instruction (containing an InstructionList)
- instruction handle of the first inserted instruction
insert
public InstructionHandle insert(Instruction i)
Insert an instruction at start of this list.
i
- instruction to insert
- instruction handle of the inserted instruction
insert
public InstructionHandle insert(Instruction i,
CompoundInstruction c)
Insert a compound instruction before instruction i.
i
- Instruction in listc
- The composite instruction (containing an InstructionList)
- instruction handle of the first inserted instruction
insert
public InstructionHandle insert(Instruction i,
Instruction j)
Insert a single instruction j before another instruction i, which
must be in this list of course!
i
- Instruction in listj
- Instruction to insert before i in list
- instruction handle of the first inserted instruction
insert
public InstructionHandle insert(Instruction i,
InstructionList il)
Insert another list before Instruction i contained in this list.
Consumes argument list, i.e., it becomes empty.
i
- where to append the instruction listil
- Instruction list to insert
- instruction handle pointing to the first inserted instruction,
i.e., il.getStart()
insert
public BranchHandle insert(InstructionHandle ih,
BranchInstruction i)
Insert an instruction before instruction (handle) ih contained in this list.
ih
- where to insert to the instruction listi
- Instruction to insert
- instruction handle of the first inserted instruction
insert
public InstructionHandle insert(InstructionHandle ih,
CompoundInstruction c)
Insert a compound instruction.
ih
- where to insert the instruction listc
- The composite instruction (containing an InstructionList)
- instruction handle of the first inserted instruction
insert
public InstructionHandle insert(InstructionHandle ih,
Instruction i)
Insert an instruction before instruction (handle) ih contained in this list.
ih
- where to insert to the instruction listi
- Instruction to insert
- instruction handle of the first inserted instruction
insert
public InstructionHandle insert(InstructionHandle ih,
InstructionList il)
Insert another list before Instruction handle ih contained in this list.
Consumes argument list, i.e., it becomes empty.
il
- Instruction list to insert
- instruction handle of the first inserted instruction
insert
public InstructionHandle insert(InstructionList il)
Insert another list.
il
- list to insert before start of this list
- instruction handle of the first inserted instruction
isEmpty
public boolean isEmpty()
Test for empty list.
iterator
public Iterator iterator()
- Enumeration that lists all instructions (handles)
move
public void move(InstructionHandle ih,
InstructionHandle target)
Move a single instruction (handle) to a new location.
ih
- moved instructiontarget
- new location of moved instruction
move
public void move(InstructionHandle start,
InstructionHandle end,
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.
start
- of moved blockend
- of moved blocktarget
- of moved block
redirectBranches
public void redirectBranches(InstructionHandle old_target,
InstructionHandle new_target)
Redirect all references from old_target to new_target, i.e., update targets
of branch instructions.
old_target
- the old target instruction handlenew_target
- the new target instruction handle
redirectExceptionHandlers
public void redirectExceptionHandlers(CodeExceptionGen[] exceptions,
InstructionHandle old_target,
InstructionHandle new_target)
Redirect all references of exception handlers from old_target to new_target.
exceptions
- array of exception handlersold_target
- the old target instruction handlenew_target
- the new target instruction handle
redirectLocalVariables
public void redirectLocalVariables(LocalVariableGen[] lg,
InstructionHandle old_target,
InstructionHandle new_target)
Redirect all references of local variables from old_target to new_target.
lg
- array of local variablesold_target
- the old target instruction handlenew_target
- the new target instruction handle
replaceConstantPool
public void replaceConstantPool(ConstantPoolGen old_cp,
ConstantPoolGen new_cp)
Replace all references to the old constant pool with references to the new
constant pool
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.
check
- Perform sanity checks, e.g. if all targeted instructions really belong
to this list
size
public int size()
- length of list (Number of instructions, not bytes)
toString
public String toString()
toString
public String toString(boolean verbose)
verbose
- toggle output format
- String containing all instructions in this list.
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.