java.lang.Object
org.spongepowered.asm.mixin.injection.struct.Target
All Implemented Interfaces:
Comparable<Target>, Iterable<org.objectweb.asm.tree.AbstractInsnNode>

public class Target extends Object implements Comparable<Target>, Iterable<org.objectweb.asm.tree.AbstractInsnNode>
Information about the current injection target, mainly just convenience rather than passing a bunch of values around.
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    class 
    A stateful extension of stack or locals space for a target method which can be applied.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    final org.objectweb.asm.Type[]
    Method arguments
    final org.objectweb.asm.tree.ClassNode
    Target class node
    final org.objectweb.asm.tree.InsnList
    Method instructions
    final boolean
    True if the method is a constructor
    final boolean
    True if the method is static
    final org.objectweb.asm.tree.MethodNode
    Target method
    final org.objectweb.asm.Type
    Return type computed from the method descriptor
  • Constructor Summary

    Constructors
    Constructor
    Description
    Target(org.objectweb.asm.tree.ClassNode classNode, org.objectweb.asm.tree.MethodNode method)
    Make a new Target for the supplied method
  • Method Summary

    Modifier and Type
    Method
    Description
    addInjectionNode(org.objectweb.asm.tree.AbstractInsnNode node)
    Add an injection node to this target if it does not already exist, returns the existing node if it exists
    void
    addLocalVariable(int index, String name, String desc)
    Add an entry to the target LVT
    void
    addLocalVariable(int index, String name, String desc, org.objectweb.asm.tree.LabelNode from, org.objectweb.asm.tree.LabelNode to)
    Add an entry to the target LVT between the specified start and end labels
    int
    Allocate a new local variable for the method
    int
    allocateLocals(int locals)
    Allocate a number of new local variables for this method, returns the first local variable index of the allocated range.
    int
     
    Creates a new locals extension.
    Creates a new stack extension.
    Find the call to super() or this() in a constructor.
    org.objectweb.asm.tree.MethodInsnNode
    findInitNodeFor(org.objectweb.asm.tree.TypeInsnNode newNode)
    Find the first <init> invocation after the specified NEW insn
    int[]
    generateArgMap(org.objectweb.asm.Type[] args, int start)
    Generate an array containing local indexes for the specified args, returns an array of identical size to the supplied array with an allocated local index in each corresponding position
    org.objectweb.asm.tree.AbstractInsnNode
    get(int index)
    Return the instruction at the specified index
    int[]
    Get the argument indices for this target, calculated on first use
    getCallbackDescriptor(boolean captureLocals, org.objectweb.asm.Type[] locals, org.objectweb.asm.Type[] argumentTypes, int startIndex, int extra)
    Get the callback descriptor
    getCallbackDescriptor(org.objectweb.asm.Type[] locals, org.objectweb.asm.Type[] argumentTypes)
    Get the callback descriptor
    Get the CallbackInfo class used for this target, based on the target return type
    int
    Get the current max locals of the method
    int
    Get the current max stack of the method
    getInjectionNode(org.objectweb.asm.tree.AbstractInsnNode node)
    Get an injection node from this collection if it already exists, returns null if the node is not tracked
    int
    Get the original max locals of the method
    int
    Get the original max stack of the method
    Get "simple" callback descriptor (descriptor with only CallbackInfo)
    int
    indexOf(org.objectweb.asm.tree.AbstractInsnNode insn)
    Return the index of the specified instruction in this instruction list
    int
    Return the index of the specified instruction in this instruction list
    void
    insertBefore(org.objectweb.asm.tree.AbstractInsnNode location, org.objectweb.asm.tree.InsnList insns)
    Insert the supplied instructions before the specified instruction
    void
    insertBefore(InjectionNodes.InjectionNode location, org.objectweb.asm.tree.InsnList insns)
    Insert the supplied instructions before the specified instruction
    Iterator<org.objectweb.asm.tree.AbstractInsnNode>
     
    void
    removeNode(org.objectweb.asm.tree.AbstractInsnNode insn)
    Remove the specified instruction from the target and mark it as removed for injections
    void
    replaceNode(org.objectweb.asm.tree.AbstractInsnNode location, org.objectweb.asm.tree.AbstractInsnNode insn)
    Replace an instruction in this target with the specified instruction and mark the node as replaced for other injectors
    void
    replaceNode(org.objectweb.asm.tree.AbstractInsnNode location, org.objectweb.asm.tree.AbstractInsnNode champion, org.objectweb.asm.tree.InsnList insns)
    Replace an instruction in this target with the specified instructions and mark the node as replaced with the specified champion node from the list.
    void
    replaceNode(org.objectweb.asm.tree.AbstractInsnNode location, org.objectweb.asm.tree.InsnList insns)
    Replace an instruction in this target with the specified instructions and mark the original node as removed
     
    void
    wrapNode(org.objectweb.asm.tree.AbstractInsnNode location, org.objectweb.asm.tree.AbstractInsnNode champion, org.objectweb.asm.tree.InsnList before, org.objectweb.asm.tree.InsnList after)
    Wrap instruction in this target with the specified instructions and mark the node as replaced with the specified champion node from the list.

    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
  • Field Details

    • classNode

      public final org.objectweb.asm.tree.ClassNode classNode
      Target class node
    • method

      public final org.objectweb.asm.tree.MethodNode method
      Target method
    • insns

      public final org.objectweb.asm.tree.InsnList insns
      Method instructions
    • isStatic

      public final boolean isStatic
      True if the method is static
    • isCtor

      public final boolean isCtor
      True if the method is a constructor
    • arguments

      public final org.objectweb.asm.Type[] arguments
      Method arguments
    • returnType

      public final org.objectweb.asm.Type returnType
      Return type computed from the method descriptor
  • Constructor Details

    • Target

      public Target(org.objectweb.asm.tree.ClassNode classNode, org.objectweb.asm.tree.MethodNode method)
      Make a new Target for the supplied method
      Parameters:
      method - target method
  • Method Details

    • addInjectionNode

      public InjectionNodes.InjectionNode addInjectionNode(org.objectweb.asm.tree.AbstractInsnNode node)
      Add an injection node to this target if it does not already exist, returns the existing node if it exists
      Parameters:
      node - Instruction node to add
      Returns:
      wrapper for the specified node
    • getInjectionNode

      public InjectionNodes.InjectionNode getInjectionNode(org.objectweb.asm.tree.AbstractInsnNode node)
      Get an injection node from this collection if it already exists, returns null if the node is not tracked
      Parameters:
      node - instruction node
      Returns:
      wrapper node or null if not tracked
    • getMaxLocals

      public int getMaxLocals()
      Get the original max locals of the method
      Returns:
      the original max locals value
    • getMaxStack

      public int getMaxStack()
      Get the original max stack of the method
      Returns:
      the original max stack value
    • getCurrentMaxLocals

      public int getCurrentMaxLocals()
      Get the current max locals of the method
      Returns:
      the current max local value
    • getCurrentMaxStack

      public int getCurrentMaxStack()
      Get the current max stack of the method
      Returns:
      the current max stack value
    • allocateLocal

      public int allocateLocal()
      Allocate a new local variable for the method

      Note that allocating a local differs from extending locals (see extendLocals()) in that the allocation immediately increases the target max locals and returns the new index of the local just allocated. Extending locals is used for temporary localised extensions to the locals table for the purposes of injectors.

      Returns:
      the allocated local index
    • allocateLocals

      public int allocateLocals(int locals)
      Allocate a number of new local variables for this method, returns the first local variable index of the allocated range.

      Note that allocating locals differs from extending locals (see extendLocals()) in that the allocation immediately increases the target max locals and returns the new index of the local just allocated. Extending locals is used for temporary localised extensions to the locals table for the purposes of injectors.

      Parameters:
      locals - number of locals to allocate
      Returns:
      the first local variable index of the allocated range
    • extendLocals

      public Target.Extension extendLocals()
      Creates a new locals extension. The returned object can be used to create extensions to the method locals for an injector to use. Extensions are not designed to overlap, and the largest extension for a method will determine the amount by which the locals table is extended.
      Returns:
      extension handle
    • extendStack

      public Target.Extension extendStack()
      Creates a new stack extension. The returned object can be used to create extensions to the method stack for an injector to use. Extensions are not designed to overlap, and the largest extension for a method will determine the amount by which the stack is extended.
      Returns:
      extension handle
    • generateArgMap

      public int[] generateArgMap(org.objectweb.asm.Type[] args, int start)
      Generate an array containing local indexes for the specified args, returns an array of identical size to the supplied array with an allocated local index in each corresponding position
      Parameters:
      args - Argument types
      start - starting index
      Returns:
      array containing a corresponding local arg index for each member of the supplied args array
    • getArgIndices

      public int[] getArgIndices()
      Get the argument indices for this target, calculated on first use
      Returns:
      argument indices for this target
    • getCallbackInfoClass

      public String getCallbackInfoClass()
      Get the CallbackInfo class used for this target, based on the target return type
      Returns:
      CallbackInfo class name
    • getSimpleCallbackDescriptor

      public String getSimpleCallbackDescriptor()
      Get "simple" callback descriptor (descriptor with only CallbackInfo)
      Returns:
      generated descriptor
    • getCallbackDescriptor

      public String getCallbackDescriptor(org.objectweb.asm.Type[] locals, org.objectweb.asm.Type[] argumentTypes)
      Get the callback descriptor
      Parameters:
      locals - Local variable types
      argumentTypes - Argument types
      Returns:
      generated descriptor
    • getCallbackDescriptor

      public String getCallbackDescriptor(boolean captureLocals, org.objectweb.asm.Type[] locals, org.objectweb.asm.Type[] argumentTypes, int startIndex, int extra)
      Get the callback descriptor
      Parameters:
      captureLocals - True if the callback is capturing locals
      locals - Local variable types
      argumentTypes - Argument types
      startIndex - local index to start at
      extra - extra locals to include
      Returns:
      generated descriptor
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • compareTo

      public int compareTo(Target o)
      Specified by:
      compareTo in interface Comparable<Target>
    • indexOf

      public int indexOf(InjectionNodes.InjectionNode node)
      Return the index of the specified instruction in this instruction list
      Parameters:
      node - instruction to locate, must exist in the target
      Returns:
      opcode index
    • indexOf

      public int indexOf(org.objectweb.asm.tree.AbstractInsnNode insn)
      Return the index of the specified instruction in this instruction list
      Parameters:
      insn - instruction to locate, must exist in the target
      Returns:
      opcode index
    • get

      public org.objectweb.asm.tree.AbstractInsnNode get(int index)
      Return the instruction at the specified index
      Parameters:
      index - opcode index
      Returns:
      requested instruction
    • iterator

      public Iterator<org.objectweb.asm.tree.AbstractInsnNode> iterator()
      Specified by:
      iterator in interface Iterable<org.objectweb.asm.tree.AbstractInsnNode>
    • findInitNodeFor

      public org.objectweb.asm.tree.MethodInsnNode findInitNodeFor(org.objectweb.asm.tree.TypeInsnNode newNode)
      Find the first <init> invocation after the specified NEW insn
      Parameters:
      newNode - NEW insn
      Returns:
      INVOKESPECIAL opcode of ctor, or null if not found
    • findDelegateInitNode

      public Bytecode.DelegateInitialiser findDelegateInitNode()
      Find the call to super() or this() in a constructor. This attempts to locate the first call to <init> which isn't an inline call to another object ctor being passed into the super invocation.
      Returns:
      Call to super(), this() or DelegateInitialiser.NONE if not found
    • insertBefore

      public void insertBefore(InjectionNodes.InjectionNode location, org.objectweb.asm.tree.InsnList insns)
      Insert the supplied instructions before the specified instruction
      Parameters:
      location - Instruction to insert before
      insns - Instructions to insert
    • insertBefore

      public void insertBefore(org.objectweb.asm.tree.AbstractInsnNode location, org.objectweb.asm.tree.InsnList insns)
      Insert the supplied instructions before the specified instruction
      Parameters:
      location - Instruction to insert before
      insns - Instructions to insert
    • replaceNode

      public void replaceNode(org.objectweb.asm.tree.AbstractInsnNode location, org.objectweb.asm.tree.AbstractInsnNode insn)
      Replace an instruction in this target with the specified instruction and mark the node as replaced for other injectors
      Parameters:
      location - Instruction to replace
      insn - Instruction to replace with
    • replaceNode

      public void replaceNode(org.objectweb.asm.tree.AbstractInsnNode location, org.objectweb.asm.tree.AbstractInsnNode champion, org.objectweb.asm.tree.InsnList insns)
      Replace an instruction in this target with the specified instructions and mark the node as replaced with the specified champion node from the list.
      Parameters:
      location - Instruction to replace
      champion - Instruction which notionally replaces the original insn
      insns - Instructions to actually insert (must contain champion)
    • wrapNode

      public void wrapNode(org.objectweb.asm.tree.AbstractInsnNode location, org.objectweb.asm.tree.AbstractInsnNode champion, org.objectweb.asm.tree.InsnList before, org.objectweb.asm.tree.InsnList after)
      Wrap instruction in this target with the specified instructions and mark the node as replaced with the specified champion node from the list.
      Parameters:
      location - Instruction to replace
      champion - Instruction which notionally replaces the original insn
      before - Instructions to actually insert (must contain champion)
      after - Instructions to insert after the specified location
    • replaceNode

      public void replaceNode(org.objectweb.asm.tree.AbstractInsnNode location, org.objectweb.asm.tree.InsnList insns)
      Replace an instruction in this target with the specified instructions and mark the original node as removed
      Parameters:
      location - Instruction to replace
      insns - Instructions to replace with
    • removeNode

      public void removeNode(org.objectweb.asm.tree.AbstractInsnNode insn)
      Remove the specified instruction from the target and mark it as removed for injections
      Parameters:
      insn - instruction to remove
    • addLocalVariable

      public void addLocalVariable(int index, String name, String desc)
      Add an entry to the target LVT
      Parameters:
      index - local variable index
      name - local variable name
      desc - local variable type
    • addLocalVariable

      public void addLocalVariable(int index, String name, String desc, org.objectweb.asm.tree.LabelNode from, org.objectweb.asm.tree.LabelNode to)
      Add an entry to the target LVT between the specified start and end labels
      Parameters:
      index - local variable index
      name - local variable name
      desc - local variable type
      from - start of range
      to - end of range