Package org.apache.bcel.generic
Class InstructionList
java.lang.Object
org.apache.bcel.generic.InstructionList
- All Implemented Interfaces:
Iterable<InstructionHandle>
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.
- See Also:
-
Constructor Summary
ConstructorDescriptionCreate (empty) instruction list.InstructionList
(byte[] code) Initialize instruction list from byte array.Create instruction list containing one instruction.Initialize list with (nonnull) compound instruction.Create instruction list containing one instruction. -
Method Summary
Modifier and TypeMethodDescriptionvoid
Add observer for this object.Append a branch instruction to the end of this list.Append a compound instruction.Append an instruction to the end of this list.Append an instruction after instruction (handle) ih contained in this list.Append a compound instruction.append
(InstructionHandle ih, Instruction i) Append an instruction after instruction (handle) ih contained in this list.append
(InstructionHandle ih, InstructionList il) Append another list after instruction (handle) ih contained in this list.Append another list to this one.Append a compound instruction, after instruction i.append
(Instruction i, Instruction j) Append a single instruction j after another instruction i, which must be in this list of course!append
(Instruction i, InstructionList il) Append another list after instruction i contained in this list.boolean
boolean
copy()
void
Remove instruction from this list.void
Remove instruction from this list.void
delete
(InstructionHandle from, InstructionHandle to) Remove instructions from instruction 'from' to instruction 'to' contained in this list.void
delete
(Instruction from, Instruction to) Remove instructions from instruction 'from' to instruction 'to' contained in this list.void
dispose()
Delete contents of list.findHandle
(int pos) Gets instruction handle for instruction at byte code position pos.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).byte[]
When everything is finished, use this method to convert the instruction list into an array of bytes.getEnd()
int[]
Gets positions (offsets) of all instructions in the list.int
getStart()
Insert a branch instruction at start of this list.Insert a compound instruction.Insert an instruction at start of this list.Insert an instruction before instruction (handle) ih contained in this list.Insert a compound instruction.insert
(InstructionHandle ih, Instruction i) Insert an instruction before instruction (handle) ih contained in this list.insert
(InstructionHandle ih, InstructionList il) Insert another list before Instruction handle ih contained in this list.Insert another list.Insert a compound instruction before instruction i.insert
(Instruction i, Instruction j) Insert a single instruction j before another instruction i, which must be in this list of course!insert
(Instruction i, InstructionList il) Insert another list before Instruction i contained in this list.boolean
isEmpty()
Test for empty list.iterator()
void
move
(InstructionHandle ih, InstructionHandle target) Move a single instruction (handle) to a new location.void
move
(InstructionHandle start, InstructionHandle end, InstructionHandle target) Take all instructions (handles) from "start" to "end" and append them after the new location "target".void
redirectBranches
(InstructionHandle oldTarget, InstructionHandle newTarget) Redirect all references from oldTarget to newTarget, i.e., update targets of branch instructions.void
redirectExceptionHandlers
(CodeExceptionGen[] exceptions, InstructionHandle oldTarget, InstructionHandle newTarget) Redirect all references of exception handlers from oldTarget to newTarget.void
redirectLocalVariables
(LocalVariableGen[] lg, InstructionHandle oldTarget, InstructionHandle newTarget) Redirect all references of local variables from oldTarget to newTarget.void
Remove observer for this object.void
replaceConstantPool
(ConstantPoolGen oldCp, ConstantPoolGen newCp) Replace all references to the old constant pool with references to the new constant poolvoid
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()
toString()
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
Methods inherited from interface java.lang.Iterable
forEach, spliterator
-
Constructor Details
-
InstructionList
public InstructionList()Create (empty) instruction list. -
InstructionList
Create instruction list containing one instruction.- Parameters:
i
- initial instruction
-
InstructionList
Initialize instruction list from byte array.- Parameters:
code
- byte array containing the instructions
-
InstructionList
Initialize list with (nonnull) compound instruction. Consumes argument list, i.e., it becomes empty.- Parameters:
c
- compound instruction (list)
-
InstructionList
Create instruction list containing one instruction.- Parameters:
i
- initial instruction
-
-
Method Details
-
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).- Parameters:
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- Returns:
- target position's instruction handle if available
-
addObserver
Add observer for this object. -
append
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
Append a compound instruction.- Parameters:
c
- The composite instruction (containing an InstructionList)- Returns:
- instruction handle of the first appended instruction
-
append
Append an instruction to the end of this list.- Parameters:
i
- instruction to append- Returns:
- instruction handle of the appended instruction
-
append
Append a compound instruction, after instruction i.- Parameters:
i
- Instruction in listc
- The composite instruction (containing an InstructionList)- Returns:
- instruction handle of the first appended instruction
-
append
Append a single instruction j after another instruction i, which must be in this list of course!- Parameters:
i
- Instruction in listj
- Instruction to append after i in list- Returns:
- instruction handle of the first appended instruction
-
append
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 listil
- Instruction list to append to this one- Returns:
- instruction handle pointing to the first appended instruction
-
append
Append an instruction after instruction (handle) ih contained in this list.- Parameters:
ih
- where to append the instruction listi
- Instruction to append- Returns:
- instruction handle pointing to the first appended instruction
-
append
Append a compound instruction.- Parameters:
ih
- where to append the instruction listc
- The composite instruction (containing an InstructionList)- Returns:
- instruction handle of the first appended instruction
-
append
Append an instruction after instruction (handle) ih contained in this list.- Parameters:
ih
- where to append the instruction listi
- Instruction to append- Returns:
- instruction handle pointing to the first appended instruction
-
append
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 listil
- Instruction list to append to this one- Returns:
- instruction handle pointing to the first appended instruction
-
append
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
-
contains
-
contains
-
copy
- Returns:
- complete, i.e., deep copy of this list
-
delete
Remove instruction from this list. The corresponding Instruction handles must not be reused!- Parameters:
i
- instruction to remove- Throws:
TargetLostException
-
delete
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:
TargetLostException
-
delete
Remove instruction from this list. The corresponding Instruction handles must not be reused!- Parameters:
ih
- instruction (handle) to remove- Throws:
TargetLostException
-
delete
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:
TargetLostException
-
dispose
Delete contents of list. Provides better memory utilization, because the system then may reuse the instruction handles. This method is typically called right afterMethodGen.getMethod()
. -
findHandle
Gets 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
-
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
-
getEnd
- Returns:
- end of list
-
getInstructionHandles
- Returns:
- array containing all instructions (handles)
-
getInstructionPositions
Gets 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
-
getInstructions
- Returns:
- an array of instructions without target information for branch instructions.
-
getLength
- Returns:
- length of list (Number of instructions, not bytes)
-
getStart
- Returns:
- start of list
-
insert
Insert a branch instruction at start of this list.- Parameters:
i
- branch instruction to insert- Returns:
- branch instruction handle of the appended instruction
-
insert
Insert a compound instruction.- Parameters:
c
- The composite instruction (containing an InstructionList)- Returns:
- instruction handle of the first inserted instruction
-
insert
Insert an instruction at start of this list.- Parameters:
i
- instruction to insert- Returns:
- instruction handle of the inserted instruction
-
insert
Insert a compound instruction before instruction i.- Parameters:
i
- Instruction in listc
- The composite instruction (containing an InstructionList)- Returns:
- instruction handle of the first inserted instruction
-
insert
Insert a single instruction j before another instruction i, which must be in this list of course!- Parameters:
i
- Instruction in listj
- Instruction to insert before i in list- Returns:
- instruction handle of the first inserted instruction
-
insert
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 listil
- Instruction list to insert- Returns:
- instruction handle pointing to the first inserted instruction, i.e., il.getStart()
-
insert
Insert an instruction before instruction (handle) ih contained in this list.- Parameters:
ih
- where to insert to the instruction listi
- Instruction to insert- Returns:
- instruction handle of the first inserted instruction
-
insert
Insert a compound instruction.- Parameters:
ih
- where to insert the instruction listc
- The composite instruction (containing an InstructionList)- Returns:
- instruction handle of the first inserted instruction
-
insert
Insert an instruction before instruction (handle) ih contained in this list.- Parameters:
ih
- where to insert to the instruction listi
- Instruction to insert- Returns:
- instruction handle of the first inserted instruction
-
insert
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 listil
- Instruction list to insert- Returns:
- instruction handle of the first inserted instruction
-
insert
Insert another list.- Parameters:
il
- list to insert before start of this list- Returns:
- instruction handle of the first inserted instruction
-
isEmpty
Test for empty list. -
iterator
- Specified by:
iterator
in interfaceIterable<InstructionHandle>
- Returns:
- iterator that lists all instructions (handles)
-
move
Move a single instruction (handle) to a new location.- Parameters:
ih
- moved instructiontarget
- new location of moved instruction
-
move
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 blockend
- of moved blocktarget
- of moved block
-
redirectBranches
Redirect all references from oldTarget to newTarget, i.e., update targets of branch instructions.- Parameters:
oldTarget
- the old target instruction handlenewTarget
- the new target instruction handle
-
redirectExceptionHandlers
public void redirectExceptionHandlers(CodeExceptionGen[] exceptions, InstructionHandle oldTarget, InstructionHandle newTarget) Redirect all references of exception handlers from oldTarget to newTarget.- Parameters:
exceptions
- array of exception handlersoldTarget
- the old target instruction handlenewTarget
- the new target instruction handle- See Also:
-
redirectLocalVariables
public void redirectLocalVariables(LocalVariableGen[] lg, InstructionHandle oldTarget, InstructionHandle newTarget) Redirect all references of local variables from oldTarget to newTarget.- Parameters:
lg
- array of local variablesoldTarget
- the old target instruction handlenewTarget
- the new target instruction handle- See Also:
-
removeObserver
Remove observer for this object. -
replaceConstantPool
Replace all references to the old constant pool with references to the new constant pool -
setPositions
-
setPositions
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
-
size
- Returns:
- length of list (Number of instructions, not bytes)
-
toString
-
toString
- Parameters:
verbose
- toggle output format- Returns:
- String containing all instructions in this list.
-
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.
-