Class GenericGenerators
java.lang.Object
com.offbynull.coroutines.instrumenter.generators.GenericGenerators
Utility class to generate common/generic bytecode instructions.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final class
Generates instruction lists based on conditions.static interface
Instruction list generator. -
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic org.objectweb.asm.tree.InsnList
addIntegers
(org.objectweb.asm.tree.InsnList lhs, org.objectweb.asm.tree.InsnList rhs) Adds two integers together and puts the result on to the stack.static org.objectweb.asm.tree.InsnList
addLabel
(org.objectweb.asm.tree.LabelNode labelNode) Generates instructions for a label.static org.objectweb.asm.tree.InsnList
Calls a method with a set of arguments.static org.objectweb.asm.tree.InsnList
call
(String internalClassName, org.objectweb.asm.tree.MethodNode method, org.objectweb.asm.tree.InsnList... args) Calls a method with a set of arguments.static org.objectweb.asm.tree.InsnList
cloneInsnList
(org.objectweb.asm.tree.InsnList insnList) Clones an instruction list.static org.objectweb.asm.tree.InsnList
cloneInsnList
(org.objectweb.asm.tree.InsnList insnList, Set<org.objectweb.asm.tree.LabelNode> globalLabels) Clones an instruction list.static org.objectweb.asm.tree.InsnList
cloneInvokeNode
(org.objectweb.asm.tree.AbstractInsnNode insnNode) Clones an invokevirtual/invokespecial/invokeinterface/invokedynamic node and returns it as an instruction list.static org.objectweb.asm.tree.InsnList
cloneMonitorNode
(org.objectweb.asm.tree.AbstractInsnNode insnNode) Clones a monitorenter/monitorexit node and returns it as an instruction list.static org.objectweb.asm.tree.InsnList
combineObjectArrays
(VariableTable.Variable destArrayVar, VariableTable.Variable firstArrayVar, VariableTable.Variable secondArrayVar) Concatenates two object arrays together.static org.objectweb.asm.tree.InsnList
construct
(Constructor<?> constructor, org.objectweb.asm.tree.InsnList... args) Calls a constructor with a set of arguments.static org.objectweb.asm.tree.InsnList
createNewObjectArray
(org.objectweb.asm.tree.InsnList size) Creates a new object array on the stack.static org.objectweb.asm.tree.InsnList
empty()
Returns an empty instruction list.static org.objectweb.asm.tree.InsnList
forEach
(VariableTable.Variable counterVar, VariableTable.Variable arrayLenVar, org.objectweb.asm.tree.InsnList array, org.objectweb.asm.tree.InsnList action) For each element in an object array, performs an action.static org.objectweb.asm.tree.InsnList
ifIntegersEqual
(org.objectweb.asm.tree.InsnList lhs, org.objectweb.asm.tree.InsnList rhs, org.objectweb.asm.tree.InsnList action) Compares two integers and performs some action if the integers are equal.static org.objectweb.asm.tree.InsnList
ifObjectsEqual
(org.objectweb.asm.tree.InsnList lhs, org.objectweb.asm.tree.InsnList rhs, org.objectweb.asm.tree.InsnList action) Compares two objects and performs some action if the objects are the same (uses == to check if same, not the equals method).static org.objectweb.asm.tree.InsnList
ifObjectsNotEqual
(org.objectweb.asm.tree.InsnList lhs, org.objectweb.asm.tree.InsnList rhs, org.objectweb.asm.tree.InsnList action) Compares two objects and performs some action if the objects are NOT the same (uses != to check if not same).static org.objectweb.asm.tree.InsnList
jumpTo
(org.objectweb.asm.tree.LabelNode labelNode) Generates instructions for an unconditional jump to a label.static org.objectweb.asm.tree.InsnList
lineNumber
(int num) Generates instructions for line numbers.static org.objectweb.asm.tree.InsnList
loadArrayLength
(org.objectweb.asm.tree.InsnList arrayRef) Gets the size of an array and puts it on to the stack.static org.objectweb.asm.tree.InsnList
loadIntConst
(int i) Generates instructions to push an integer constant on to the stack.static org.objectweb.asm.tree.InsnList
loadNull()
Generates instruction to push a null on to the stack.static org.objectweb.asm.tree.InsnList
loadStaticField
(Field field) Load a static field on to the stack.static org.objectweb.asm.tree.InsnList
Generates instruction to push a string constant on to the stack.static org.objectweb.asm.tree.InsnList
loadVar
(VariableTable.Variable variable) Copies a local variable on to the stack.static org.objectweb.asm.tree.InsnList
Combines multiple instructions in to a single instruction list.Combines multiple instructions (or instruction lists) in to a single instruction list if some condition is met.static org.objectweb.asm.tree.InsnList
Generates a MONITORENTER instruction, which consumes an Object from the top of the stack.static org.objectweb.asm.tree.InsnList
Generates a MONITOREXIT instruction, which consumes an Object from the top of the stack.static org.objectweb.asm.tree.InsnList
pop()
Generates instructions to pop an item off the stack.static org.objectweb.asm.tree.InsnList
pop
(int count) Generates instructions to popcount
items off the stack.static org.objectweb.asm.tree.InsnList
returnValue
(org.objectweb.asm.Type returnType, org.objectweb.asm.tree.InsnList returnValueInsnList) Generates instructions that returns a value.static org.objectweb.asm.tree.InsnList
Generates instructions that returns nothing (void).static org.objectweb.asm.tree.InsnList
saveVar
(VariableTable.Variable variable) Pops the stack in to the the local variable table.static org.objectweb.asm.tree.InsnList
tableSwitch
(org.objectweb.asm.tree.InsnList indexInsnList, org.objectweb.asm.tree.InsnList defaultInsnList, int caseStartIdx, org.objectweb.asm.tree.InsnList... caseInsnLists) Generates instructions for a switch table.static org.objectweb.asm.tree.InsnList
throwRuntimeException
(String message) Generates instructions to throw an exception of typeRuntimeException
with a constant message.static org.objectweb.asm.tree.InsnList
Generates instructions to throw the exception type at top of stack.static org.objectweb.asm.tree.InsnList
tryCatchBlock
(org.objectweb.asm.tree.TryCatchBlockNode tryCatchBlockNode, org.objectweb.asm.Type exceptionType, org.objectweb.asm.tree.InsnList tryInsnList, org.objectweb.asm.tree.InsnList catchInsnList) Generates instructions for a try-catch block.private static void
validateLocalIndicies
(int... indicies)
-
Field Details
-
SYSTEM_ARRAY_COPY_METHOD
-
-
Constructor Details
-
GenericGenerators
private GenericGenerators()
-
-
Method Details
-
empty
public static org.objectweb.asm.tree.InsnList empty()Returns an empty instruction list.- Returns:
- empty instruction list
-
cloneInvokeNode
public static org.objectweb.asm.tree.InsnList cloneInvokeNode(org.objectweb.asm.tree.AbstractInsnNode insnNode) Clones an invokevirtual/invokespecial/invokeinterface/invokedynamic node and returns it as an instruction list.- Parameters:
insnNode
- instruction to clone- Returns:
- instruction list with cloned instruction
- Throws:
NullPointerException
- if any argument isnull
IllegalArgumentException
- if node isn't of invoke type
-
cloneMonitorNode
public static org.objectweb.asm.tree.InsnList cloneMonitorNode(org.objectweb.asm.tree.AbstractInsnNode insnNode) Clones a monitorenter/monitorexit node and returns it as an instruction list.- Parameters:
insnNode
- instruction to clone- Returns:
- instruction list with cloned instruction
- Throws:
NullPointerException
- if any argument isnull
IllegalArgumentException
- if node isn't of invoke type
-
cloneInsnList
public static org.objectweb.asm.tree.InsnList cloneInsnList(org.objectweb.asm.tree.InsnList insnList) Clones an instruction list. Equivalent to callingcloneInsnList(insnList, Collections.emptySet())
.- Parameters:
insnList
- instruction list to clone- Returns:
- instruction list with cloned instructions
- Throws:
NullPointerException
- if any argument isnull
-
cloneInsnList
public static org.objectweb.asm.tree.InsnList cloneInsnList(org.objectweb.asm.tree.InsnList insnList, Set<org.objectweb.asm.tree.LabelNode> globalLabels) Clones an instruction list. All labels are remapped unless otherwise specified inglobalLabels
.- Parameters:
insnList
- instruction list to cloneglobalLabels
- set of labels that should not be remapped- Returns:
- instruction list with cloned instructions
- Throws:
NullPointerException
- if any argument isnull
-
merge
Combines multiple instructions in to a single instruction list. You can include normal instructions (AbstractInsnNode
), instruction lists (InsnList
), or instruction generators (GenericGenerators.InstructionGenerator
).- Parameters:
insns
- instructions- Returns:
- merged instructions
- Throws:
NullPointerException
- if any argument isnull
or containsnull
-
mergeIf
public static GenericGenerators.ConditionalMerger mergeIf(boolean condition, Supplier<Object[]> insnsSupplier) Combines multiple instructions (or instruction lists) in to a single instruction list if some condition is met.- Parameters:
condition
- flag indicating if the instruction list should be generatedinsnsSupplier
- supplier that generates the instructions to merge (only generated ifcondition == true
)- Returns:
- merged instructions if
condition
istrue
, empty instructions list otherwise - Throws:
NullPointerException
- if any argument isnull
or containsnull
-
jumpTo
public static org.objectweb.asm.tree.InsnList jumpTo(org.objectweb.asm.tree.LabelNode labelNode) Generates instructions for an unconditional jump to a label.- Parameters:
labelNode
- label to jump to- Returns:
- instructions for an unconditional jump to
labelNode
- Throws:
NullPointerException
- if any argument isnull
-
addLabel
public static org.objectweb.asm.tree.InsnList addLabel(org.objectweb.asm.tree.LabelNode labelNode) Generates instructions for a label.- Parameters:
labelNode
- label to insert- Returns:
- instructions for a label
- Throws:
NullPointerException
- if any argument isnull
-
lineNumber
public static org.objectweb.asm.tree.InsnList lineNumber(int num) Generates instructions for line numbers. This is useful for debugging. For example, you can put a line number of 99999 or some other special number to denote that the code being executed is instrumented code. Then if a stacktrace happens, you'll know that if instrumented code was immediately involved.- Parameters:
num
- line number- Returns:
- instructions for a line number
- Throws:
IllegalArgumentException
- ifnum < 0
-
pop
public static org.objectweb.asm.tree.InsnList pop()Generates instructions to pop an item off the stack.- Returns:
- instructions for a pop
-
pop
public static org.objectweb.asm.tree.InsnList pop(int count) Generates instructions to popcount
items off the stack.- Parameters:
count
- number of items to pop- Returns:
- instructions for a pop
- Throws:
IllegalArgumentException
- if any numeric argument is negative
-
monitorEnter
public static org.objectweb.asm.tree.InsnList monitorEnter()Generates a MONITORENTER instruction, which consumes an Object from the top of the stack.- Returns:
- instructions for a pop
-
monitorExit
public static org.objectweb.asm.tree.InsnList monitorExit()Generates a MONITOREXIT instruction, which consumes an Object from the top of the stack.- Returns:
- instructions for a pop
-
loadIntConst
public static org.objectweb.asm.tree.InsnList loadIntConst(int i) Generates instructions to push an integer constant on to the stack.- Parameters:
i
- integer constant to push- Returns:
- instructions to push an integer constant
-
loadStringConst
Generates instruction to push a string constant on to the stack.- Parameters:
s
- string constant to push- Returns:
- instructions to push a string constant
- Throws:
NullPointerException
- if any argument isnull
-
loadNull
public static org.objectweb.asm.tree.InsnList loadNull()Generates instruction to push a null on to the stack.- Returns:
- instructions to push a null
-
loadVar
Copies a local variable on to the stack.- Parameters:
variable
- variable within the local variable table to load from- Returns:
- instructions to load a local variable on to the stack
- Throws:
NullPointerException
- if any argument isnull
IllegalArgumentException
- ifvariable
has been released
-
saveVar
Pops the stack in to the the local variable table. You may run in to problems if the item on top of the stack isn't of the same type as the variable it's being put in to.- Parameters:
variable
- variable within the local variable table to save to- Returns:
- instructions to pop an item off the top of the stack and save it to
variable
- Throws:
NullPointerException
- if any argument isnull
IllegalArgumentException
- ifvariable
has been released
-
loadStaticField
Load a static field on to the stack. If the static field is of a primitive type, that primitive value will be directly loaded on to the stack (the field won't be referenced). If the static field is an object type, the actual field will be loaded onto the stack.- Parameters:
field
- reflection reference to a static field- Returns:
- instructions to grab the object stored in the static field
field
- Throws:
NullPointerException
- if any argument isnull
IllegalArgumentException
- iffield
is not static
-
createNewObjectArray
public static org.objectweb.asm.tree.InsnList createNewObjectArray(org.objectweb.asm.tree.InsnList size) Creates a new object array on the stack.- Parameters:
size
- instructions to calculate the size of the new array -- must leave an int on the stack- Returns:
- instructions to create a new object array -- will leave the object array on the stack
- Throws:
NullPointerException
- if any argument isnull
-
loadArrayLength
public static org.objectweb.asm.tree.InsnList loadArrayLength(org.objectweb.asm.tree.InsnList arrayRef) Gets the size of an array and puts it on to the stack.- Parameters:
arrayRef
- instructions to get the array -- must leave an array on the stack- Returns:
- instructions to get the size of the array -- will size an int on the stack
- Throws:
NullPointerException
- if any argument isnull
-
addIntegers
public static org.objectweb.asm.tree.InsnList addIntegers(org.objectweb.asm.tree.InsnList lhs, org.objectweb.asm.tree.InsnList rhs) Adds two integers together and puts the result on to the stack.- Parameters:
lhs
- instructions to generate the first operand -- must leave an int on the stackrhs
- instructions to generate the second operand -- must leave an int on the stack- Returns:
- instructions to add the two numbers together -- will leave an int on the stack
- Throws:
NullPointerException
- if any argument isnull
-
construct
public static org.objectweb.asm.tree.InsnList construct(Constructor<?> constructor, org.objectweb.asm.tree.InsnList... args) Calls a constructor with a set of arguments. After execution the stack should have an extra item pushed on it: the object that was created by this constructor.- Parameters:
constructor
- constructor to callargs
- constructor argument instruction lists -- each instruction list must leave one item on the stack of the type expected by the constructor- Returns:
- instructions to invoke a constructor
- Throws:
NullPointerException
- if any argument isnull
or array containsnull
IllegalArgumentException
- if the length ofargs
doesn't match the number of parameters inconstructor
-
ifIntegersEqual
public static org.objectweb.asm.tree.InsnList ifIntegersEqual(org.objectweb.asm.tree.InsnList lhs, org.objectweb.asm.tree.InsnList rhs, org.objectweb.asm.tree.InsnList action) Compares two integers and performs some action if the integers are equal.- Parameters:
lhs
- left hand side instruction list -- must leave an int on the stackrhs
- right hand side instruction list -- must leave an int on the stackaction
- action to perform if results oflhs
andrhs
are equal- Returns:
- instructions instruction list to perform some action if two ints are equal
- Throws:
NullPointerException
- if any argument isnull
-
ifObjectsEqual
public static org.objectweb.asm.tree.InsnList ifObjectsEqual(org.objectweb.asm.tree.InsnList lhs, org.objectweb.asm.tree.InsnList rhs, org.objectweb.asm.tree.InsnList action) Compares two objects and performs some action if the objects are the same (uses == to check if same, not the equals method).- Parameters:
lhs
- left hand side instruction list -- must leave an object on the stackrhs
- right hand side instruction list -- must leave an object on the stackaction
- action to perform if results oflhs
andrhs
are equal- Returns:
- instructions instruction list to perform some action if two objects are equal
- Throws:
NullPointerException
- if any argument isnull
-
ifObjectsNotEqual
public static org.objectweb.asm.tree.InsnList ifObjectsNotEqual(org.objectweb.asm.tree.InsnList lhs, org.objectweb.asm.tree.InsnList rhs, org.objectweb.asm.tree.InsnList action) Compares two objects and performs some action if the objects are NOT the same (uses != to check if not same).- Parameters:
lhs
- left hand side instruction list -- must leave an object on the stackrhs
- right hand side instruction list -- must leave an object on the stackaction
- action to perform if results oflhs
andrhs
are not equal- Returns:
- instructions instruction list to perform some action if two objects are not equal
- Throws:
NullPointerException
- if any argument isnull
-
forEach
public static org.objectweb.asm.tree.InsnList forEach(VariableTable.Variable counterVar, VariableTable.Variable arrayLenVar, org.objectweb.asm.tree.InsnList array, org.objectweb.asm.tree.InsnList action) For each element in an object array, performs an action.- Parameters:
counterVar
- parameter used to keep track of count in looparrayLenVar
- parameter used to keep track of array lengtharray
- object array instruction list -- must leave an array on the stackaction
- action to perform on each element -- element will be at top of stack and must be consumed by these instructions- Returns:
- instructions instruction list to perform some action if two ints are equal
- Throws:
NullPointerException
- if any argument isnull
-
call
public static org.objectweb.asm.tree.InsnList call(Method method, org.objectweb.asm.tree.InsnList... args) Calls a method with a set of arguments. After execution the stack may have an extra item pushed on it: the object that was returned by this method (if any).- Parameters:
method
- method to callargs
- method argument instruction lists -- each instruction list must leave one item on the stack of the type expected by the method (note that if this is a non-static method, the first argument must always evaluate to the "this" pointer/reference)- Returns:
- instructions to invoke a method
- Throws:
NullPointerException
- if any argument isnull
or array containsnull
IllegalArgumentException
- if the length ofargs
doesn't match the number of parameters inmethod
-
call
public static org.objectweb.asm.tree.InsnList call(String internalClassName, org.objectweb.asm.tree.MethodNode method, org.objectweb.asm.tree.InsnList... args) Calls a method with a set of arguments. After execution the stack may have an extra item pushed on it: the object that was returned by this method (if any).- Parameters:
internalClassName
- name of class thatmethod
belongs to (this is the internal Java name where dots are replaced with slashes)method
- method node that describes the method to callargs
- method argument instruction lists -- each instruction list must leave one item on the stack of the type expected by the method (note that if this is a non-static method, the first argument must always evaluate to the "this" pointer/reference)- Returns:
- instructions to invoke a method
- Throws:
NullPointerException
- if any argument isnull
or array containsnull
IllegalArgumentException
- if the length ofargs
doesn't match the number of parameters inmethod
-
combineObjectArrays
public static org.objectweb.asm.tree.InsnList combineObjectArrays(VariableTable.Variable destArrayVar, VariableTable.Variable firstArrayVar, VariableTable.Variable secondArrayVar) Concatenates two object arrays together.- Parameters:
destArrayVar
- variable to put concatenated object array infirstArrayVar
- variable of first object arraysecondArrayVar
- variable of second object array- Returns:
- instructions to create a new object array where the first part of the array is the contents of
firstArrayVar
and the second part of the array is the contents ofsecondArrayVar
- Throws:
NullPointerException
- if any argument isnull
IllegalArgumentException
- if variables have the same index, or if variables have been released, or if variables are of wrong type
-
throwRuntimeException
Generates instructions to throw an exception of typeRuntimeException
with a constant message.- Parameters:
message
- message of exception- Returns:
- instructions to throw an exception
- Throws:
NullPointerException
- if any argument isnull
-
throwThrowable
public static org.objectweb.asm.tree.InsnList throwThrowable()Generates instructions to throw the exception type at top of stack. You may run in to problems if the item on top of the stack isn't aThrowable
type.- Returns:
- instructions to throw an exception
- Throws:
NullPointerException
- if any argument isnull
-
tableSwitch
public static org.objectweb.asm.tree.InsnList tableSwitch(org.objectweb.asm.tree.InsnList indexInsnList, org.objectweb.asm.tree.InsnList defaultInsnList, int caseStartIdx, org.objectweb.asm.tree.InsnList... caseInsnLists) Generates instructions for a switch table. This does not automatically generate jumps at the end of each default/case statement. It's your responsibility to either add the relevant jumps, throws, or returns at each default/case statement, otherwise the code will just fall through (which is likely not what you want).- Parameters:
indexInsnList
- instructions to calculate the index -- must leave an int on the stackdefaultInsnList
- instructions to execute on default statement -- must leave the stack unchangedcaseStartIdx
- the number which the case statements start atcaseInsnLists
- instructions to execute on each case statement -- must leave the stack unchanged- Returns:
- instructions for a table switch
- Throws:
NullPointerException
- if any argument isnull
or containsnull
IllegalArgumentException
- if any numeric argument is< 0
, or ifcaseInsnLists
is empty
-
tryCatchBlock
public static org.objectweb.asm.tree.InsnList tryCatchBlock(org.objectweb.asm.tree.TryCatchBlockNode tryCatchBlockNode, org.objectweb.asm.Type exceptionType, org.objectweb.asm.tree.InsnList tryInsnList, org.objectweb.asm.tree.InsnList catchInsnList) Generates instructions for a try-catch block.- Parameters:
tryCatchBlockNode
- try catch block node to populate to with label with relevant informationexceptionType
- exception type to catch (null
means catch any exception)tryInsnList
- instructions to execute for try blockcatchInsnList
- instructions to execute for catch block- Returns:
- instructions for a try catch block
- Throws:
NullPointerException
- if any argument other thanexceptionType
isnull
or containsnull
IllegalArgumentException
- ifexceptionType
is not an object type (technically must inherit fromThrowable
, but no way to check this)
-
returnVoid
public static org.objectweb.asm.tree.InsnList returnVoid()Generates instructions that returns nothing (void).- Returns:
- instructions to return a void
-
returnValue
public static org.objectweb.asm.tree.InsnList returnValue(org.objectweb.asm.Type returnType, org.objectweb.asm.tree.InsnList returnValueInsnList) Generates instructions that returns a value.- Parameters:
returnType
- return type of the method this generated bytecode is forreturnValueInsnList
- instructions that produce the return value (should leave it on the top of the stack)- Returns:
- instructions to return a value
- Throws:
NullPointerException
- if any argument isnull
IllegalArgumentException
- ifreturnType
's sort is ofType.METHOD
-
validateLocalIndicies
private static void validateLocalIndicies(int... indicies)
-