java.lang.Object
org.spongepowered.asm.mixin.injection.code.Injector
Direct Known Subclasses:
CallbackInjector, InvokeInjector, ModifyVariableInjector

public abstract class Injector extends Object
Base class for bytecode injectors
  • Field Details

    • logger

      protected static final ILogger logger
      Log more things
    • info

      protected InjectionInfo info
      Injection info
    • annotationType

      protected final String annotationType
      Annotation type, for use in informational errors
    • classNode

      protected final org.objectweb.asm.tree.ClassNode classNode
      Class node
    • methodNode

      protected final org.objectweb.asm.tree.MethodNode methodNode
      Callback method
    • methodArgs

      protected final org.objectweb.asm.Type[] methodArgs
      Arguments of the handler method
    • returnType

      protected final org.objectweb.asm.Type returnType
      Return type of the handler method
    • isStatic

      protected final boolean isStatic
      True if the callback method is static
    • isInterface

      protected final boolean isInterface
      True if the callback method is in an interface
  • Constructor Details

    • Injector

      public Injector(InjectionInfo info, String annotationType)
      Make a new CallbackInjector for the supplied InjectionInfo
      Parameters:
      info - Information about this injection
  • Method Details

    • toString

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

      public final List<InjectionNodes.InjectionNode> find(InjectorTarget injectorTarget, List<InjectionPoint> injectionPoints)
      ...
      Parameters:
      injectorTarget - Target method to inject into
      injectionPoints - InjectionPoint instances which will identify target insns in the target method
      Returns:
      discovered injection points
    • addTargetNode

      protected void addTargetNode(InjectorTarget injectorTarget, List<InjectionNodes.InjectionNode> myNodes, org.objectweb.asm.tree.AbstractInsnNode node, Set<InjectionPoint> nominators)
    • preInject

      public final void preInject(Target target, List<InjectionNodes.InjectionNode> nodes)
      Performs pre-injection checks and tasks on the specified target
      Parameters:
      target - target potentially being injected into
      nodes - selected target nodes
    • inject

      public final void inject(Target target, List<InjectionNodes.InjectionNode> nodes)
      Performs the injection on the specified target
      Parameters:
      target - target to inject into
      nodes - selected nodes
    • addTargetNode

      protected void addTargetNode(InjectorTarget target, Map<Integer,Injector.TargetNode> targetNodes, InjectionPoint injectionPoint, org.objectweb.asm.tree.AbstractInsnNode insn)
    • findTargetNodes

      protected boolean findTargetNodes(InjectorTarget target, InjectionPoint injectionPoint, Collection<org.objectweb.asm.tree.AbstractInsnNode> nodes)
    • sanityCheck

      protected void sanityCheck(Target target, List<InjectionPoint> injectionPoints)
    • checkTargetModifiers

      protected final void checkTargetModifiers(Target target, boolean exactMatch)
      Check that the static modifier of the target method matches the handler
      Parameters:
      target - Target to check
      exactMatch - True if static must match, false to only check if an instance handler is targetting a static method
    • checkTargetForNode

      protected void checkTargetForNode(Target target, InjectionNodes.InjectionNode node, InjectionPoint.RestrictTargetLevel targetLevel)
      The normal staticness check is not location-aware, in that it merely enforces static modifiers of handlers to match their targets. For injecting into constructors however (which are ostensibly instance methods) calls which are injected before the call to super() cannot access this and must therefore be declared as static.
      Parameters:
      target - Target method
      node - Injection location
    • preInject

      protected void preInject(Target target, InjectionNodes.InjectionNode node)
    • inject

      protected abstract void inject(Target target, InjectionNodes.InjectionNode node)
    • postInject

      protected void postInject(Target target, InjectionNodes.InjectionNode node)
    • invokeHandler

      protected org.objectweb.asm.tree.AbstractInsnNode invokeHandler(org.objectweb.asm.tree.InsnList insns)
      Invoke the handler method
      Parameters:
      insns - Instruction list to inject into
      Returns:
      injected insn node
    • invokeHandler

      protected org.objectweb.asm.tree.AbstractInsnNode invokeHandler(org.objectweb.asm.tree.InsnList insns, org.objectweb.asm.tree.MethodNode handler)
      Invoke a handler method
      Parameters:
      insns - Instruction list to inject into
      handler - Actual method to invoke (may be different if using a surrogate)
      Returns:
      injected insn node
    • invokeHandlerWithArgs

      protected org.objectweb.asm.tree.AbstractInsnNode invokeHandlerWithArgs(org.objectweb.asm.Type[] args, org.objectweb.asm.tree.InsnList insns, int[] argMap)
      Parameters:
      args - handler arguments
      insns - InsnList to inject insns into
      argMap - Mapping of args to local variables
      Returns:
      injected insn node
    • invokeHandlerWithArgs

      protected org.objectweb.asm.tree.AbstractInsnNode invokeHandlerWithArgs(org.objectweb.asm.Type[] args, org.objectweb.asm.tree.InsnList insns, int[] argMap, int startArg, int endArg)
      Parameters:
      args - handler arguments
      insns - InsnList to inject insns into
      argMap - Mapping of args to local variables
      startArg - Starting arg to consume
      endArg - Ending arg to consume
      Returns:
      injected insn node
    • storeArgs

      protected int[] storeArgs(Target target, org.objectweb.asm.Type[] args, org.objectweb.asm.tree.InsnList insns, int start)
      Store args on the stack starting at the end and working back to position specified by start, return the generated argMap
      Parameters:
      target - target method
      args - argument types
      insns - instruction list to generate insns into
      start - Starting index
      Returns:
      the generated argmap
    • storeArgs

      protected int[] storeArgs(Target target, org.objectweb.asm.Type[] args, org.objectweb.asm.tree.InsnList insns, int start, org.objectweb.asm.tree.LabelNode from, org.objectweb.asm.tree.LabelNode to)
      Store args on the stack starting at the end and working back to position specified by start, return the generated argMap
      Parameters:
      target - target method
      args - argument types
      insns - instruction list to generate insns into
      start - Starting index
      from - The label marking the start of the region for the locals
      to - The label marking the end of the region for the stored locals
      Returns:
      the generated argmap
    • storeArgs

      protected void storeArgs(Target target, org.objectweb.asm.Type[] args, org.objectweb.asm.tree.InsnList insns, int[] argMap, int start, int end)
      Store args on the stack to their positions allocated based on argMap
      Parameters:
      args - argument types
      insns - instruction list to generate insns into
      argMap - generated argmap containing local indices for all args
      start - Starting index
      end - Ending index
    • storeArgs

      protected void storeArgs(Target target, org.objectweb.asm.Type[] args, org.objectweb.asm.tree.InsnList insns, int[] argMap, int start, int end, org.objectweb.asm.tree.LabelNode from, org.objectweb.asm.tree.LabelNode to)
      Store args on the stack to their positions allocated based on argMap
      Parameters:
      args - argument types
      insns - instruction list to generate insns into
      argMap - generated argmap containing local indices for all args
      start - Starting index
      end - Ending index
      from - The label marking the start of the region for the locals
      to - The label marking the end of the region for the stored locals
    • pushArgs

      protected void pushArgs(org.objectweb.asm.Type[] args, org.objectweb.asm.tree.InsnList insns, int[] argMap, int start, int end)
      Load args onto the stack from their positions allocated in argMap
      Parameters:
      args - argument types
      insns - instruction list to generate insns into
      argMap - generated argmap containing local indices for all args
      start - Starting index
      end - Ending index
    • pushArgs

      protected void pushArgs(org.objectweb.asm.Type[] args, org.objectweb.asm.tree.InsnList insns, int[] argMap, int start, int end, Target.Extension extension)
      Load args onto the stack from their positions allocated in argMap
      Parameters:
      args - argument types
      insns - instruction list to generate insns into
      argMap - generated argmap containing local indices for all args
      start - Starting index
      end - Ending index
    • validateParams

      protected final void validateParams(Injector.InjectorData injector, org.objectweb.asm.Type returnType, org.objectweb.asm.Type... args)
      Collects all the logic from old validateParams/checkDescriptor so that we can consistently apply coercion logic to method params, and also provide more detailed errors when something doesn't line up.

      The supplied return type and argument list will be verified first. Any arguments on the handler beyond the base arguments consume arguments from the target. The flag allowCoerceArgs on the redirect instance determines whether coercion is supported for the base args and return type, coercion is always allowed for captured target args.

      Following validation, the captureTargetArgs and coerceReturnType values will be set on the bundle and the calling injector function should adjust itself accordingly.

      Parameters:
      injector - Data bundle for the injector
      returnType - Return type for the handler, must not be null
      args - Array of handler args, must not be null
    • checkCoerce

      protected final boolean checkCoerce(int index, org.objectweb.asm.Type toType, String description, boolean allowCoercion)
      Called inside validateParams(org.spongepowered.asm.mixin.injection.code.Injector.InjectorData, org.objectweb.asm.Type, org.objectweb.asm.Type...) but can also be used directly. This method checks whether the supplied type is compatible with the specified handler argument, apply coercion logic where necessary.
      Parameters:
      index - Handler argument index, pass in a negative value (by convention -1) to specify handler return type
      toType - Desired type based on the injector contract
      description - human-readable description of the handler method, used in raised exception
      allowCoercion - True if coercion logic can be applied to this argument, false to only allow a precise match
      Returns:
      false if the argument matched perfectly, true if coercion is required for the argument
    • throwException

      protected void throwException(org.objectweb.asm.tree.InsnList insns, Target.Extension extraStack, String exceptionType, String message)
      Throw an exception. The exception class must have a string which takes a string argument
      Parameters:
      insns - Insn list to inject into
      exceptionType - Type of exception to throw (binary name)
      message - Message to pass to the exception constructor
    • canCoerce

      public static boolean canCoerce(org.objectweb.asm.Type from, org.objectweb.asm.Type to)
      Returns whether the from type can be coerced to the to type.
      Parameters:
      from - type to coerce from
      to - type to coerce to
      Returns:
      true if from can be coerced to to
    • canCoerce

      public static boolean canCoerce(String from, String to)
      Returns whether the from type can be coerced to the to type.
      Parameters:
      from - type to coerce from
      to - type to coerce to
      Returns:
      true if from can be coerced to to
    • canCoerce

      public static boolean canCoerce(char from, char to)
      Returns whether the from type can be coerced to the to type.
      Parameters:
      from - type to coerce from
      to - type to coerce to
      Returns:
      true if from can be coerced to to