Class GenericGenerators

java.lang.Object
com.offbynull.coroutines.instrumenter.generators.GenericGenerators

public final class GenericGenerators extends Object
Utility class to generate common/generic bytecode instructions.
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static final class 
    Generates instruction lists based on conditions.
    static interface 
    Instruction list generator.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    private static final Method
     
  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
    private
     
  • Method Summary

    Modifier and Type
    Method
    Description
    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.
    static org.objectweb.asm.tree.InsnList
    addLabel(org.objectweb.asm.tree.LabelNode labelNode)
    Generates instructions for a label.
    static org.objectweb.asm.tree.InsnList
    call(Method method, org.objectweb.asm.tree.InsnList... args)
    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
    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
    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
    Generates instruction to push a null on to the stack.
    static org.objectweb.asm.tree.InsnList
    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
    Copies a local variable on to the stack.
    static org.objectweb.asm.tree.InsnList
    merge(Object... insns)
    Combines multiple instructions in to a single instruction list.
    mergeIf(boolean condition, Supplier<Object[]> insnsSupplier)
    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 pop count 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
    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
    Generates instructions to throw an exception of type RuntimeException 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)
     

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • SYSTEM_ARRAY_COPY_METHOD

      private static final Method 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 is null
      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 is null
      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 calling cloneInsnList(insnList, Collections.emptySet()).
      Parameters:
      insnList - instruction list to clone
      Returns:
      instruction list with cloned instructions
      Throws:
      NullPointerException - if any argument is null
    • 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 in globalLabels.
      Parameters:
      insnList - instruction list to clone
      globalLabels - set of labels that should not be remapped
      Returns:
      instruction list with cloned instructions
      Throws:
      NullPointerException - if any argument is null
    • merge

      public static org.objectweb.asm.tree.InsnList merge(Object... insns)
      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 is null or contains null
    • 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 generated
      insnsSupplier - supplier that generates the instructions to merge (only generated if condition == true)
      Returns:
      merged instructions if condition is true, empty instructions list otherwise
      Throws:
      NullPointerException - if any argument is null or contains null
    • 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 is null
    • 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 is null
    • 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 - if num < 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 pop count 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

      public static org.objectweb.asm.tree.InsnList loadStringConst(String s)
      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 is null
    • 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

      public static org.objectweb.asm.tree.InsnList loadVar(VariableTable.Variable variable)
      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 is null
      IllegalArgumentException - if variable has been released
    • saveVar

      public static org.objectweb.asm.tree.InsnList saveVar(VariableTable.Variable variable)
      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 is null
      IllegalArgumentException - if variable has been released
    • loadStaticField

      public static org.objectweb.asm.tree.InsnList loadStaticField(Field field)
      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 is null
      IllegalArgumentException - if field 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 is null
    • 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 is null
    • 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 stack
      rhs - 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 is null
    • 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 call
      args - 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 is null or array contains null
      IllegalArgumentException - if the length of args doesn't match the number of parameters in constructor
    • 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 stack
      rhs - right hand side instruction list -- must leave an int on the stack
      action - action to perform if results of lhs and rhs are equal
      Returns:
      instructions instruction list to perform some action if two ints are equal
      Throws:
      NullPointerException - if any argument is null
    • 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 stack
      rhs - right hand side instruction list -- must leave an object on the stack
      action - action to perform if results of lhs and rhs are equal
      Returns:
      instructions instruction list to perform some action if two objects are equal
      Throws:
      NullPointerException - if any argument is null
    • 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 stack
      rhs - right hand side instruction list -- must leave an object on the stack
      action - action to perform if results of lhs and rhs are not equal
      Returns:
      instructions instruction list to perform some action if two objects are not equal
      Throws:
      NullPointerException - if any argument is null
    • 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 loop
      arrayLenVar - parameter used to keep track of array length
      array - object array instruction list -- must leave an array on the stack
      action - 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 is null
    • 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 call
      args - 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 is null or array contains null
      IllegalArgumentException - if the length of args doesn't match the number of parameters in method
    • 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 that method belongs to (this is the internal Java name where dots are replaced with slashes)
      method - method node that describes the method to call
      args - 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 is null or array contains null
      IllegalArgumentException - if the length of args doesn't match the number of parameters in method
    • 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 in
      firstArrayVar - variable of first object array
      secondArrayVar - 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 of secondArrayVar
      Throws:
      NullPointerException - if any argument is null
      IllegalArgumentException - if variables have the same index, or if variables have been released, or if variables are of wrong type
    • throwRuntimeException

      public static org.objectweb.asm.tree.InsnList throwRuntimeException(String message)
      Generates instructions to throw an exception of type RuntimeException with a constant message.
      Parameters:
      message - message of exception
      Returns:
      instructions to throw an exception
      Throws:
      NullPointerException - if any argument is null
    • 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 a Throwable type.
      Returns:
      instructions to throw an exception
      Throws:
      NullPointerException - if any argument is null
    • 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 stack
      defaultInsnList - instructions to execute on default statement -- must leave the stack unchanged
      caseStartIdx - the number which the case statements start at
      caseInsnLists - instructions to execute on each case statement -- must leave the stack unchanged
      Returns:
      instructions for a table switch
      Throws:
      NullPointerException - if any argument is null or contains null
      IllegalArgumentException - if any numeric argument is < 0, or if caseInsnLists 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 information
      exceptionType - exception type to catch (null means catch any exception)
      tryInsnList - instructions to execute for try block
      catchInsnList - instructions to execute for catch block
      Returns:
      instructions for a try catch block
      Throws:
      NullPointerException - if any argument other than exceptionType is null or contains null
      IllegalArgumentException - if exceptionType is not an object type (technically must inherit from Throwable, 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 for
      returnValueInsnList - 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 is null
      IllegalArgumentException - if returnType's sort is of Type.METHOD
    • validateLocalIndicies

      private static void validateLocalIndicies(int... indicies)