Allow Redirect injectors to capture method args as well as invoke args

This commit is contained in:
Mumfrey committed 2015-01-24 01:16:29 +00:00
1 parent dcd44b91d2
commit 9defcd6245
4 files changed
+84 -7

No files matched your search

@@ -30,8 +30,44 @@ import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* Specifies that this mixin method should redirect the specified
* {@link #method} call to the method decorated with this annotation.
* <p>Specifies that this mixin method should redirect the specified
* {@link #method} call to the method decorated with this annotation.</p>
*
* <p>The handler method signature must match the hooked method precisely
* <b>but</b> prepended with an arg of the owning object's type to accept the
* object instance the method was going to be invoked on. For example when
* hooking the following call:</p>
*
* <blockquote><pre>
* public void baz(int someInt, String someString) {
* int abc = 0;
* int def = 1;
* Foo someObject = new Foo();
*
* // Hooking this method
* boolean xyz = someObject.bar(abc, def);
* }</pre>
* </blockquote>
*
* <p>The signature of the redirected method should be:</p>
*
* <blockquote>
* <pre>public boolean barProxy(Foo someObject, int abc, int def)</pre>
* </blockquote>
*
* <p>For obvious reasons this does not apply for static methods, for static
* methods it is sufficient that the signature simply match the hooked method.
* </p>
*
* <p>It is also possible to capture the arguments of the target method in
* addition to the arguments being passed to the method call (for example in
* the code above this would be the <em>someInt</em> and <em>someString</em>
* arguments) by appending the arguments to the method signature:</p>
*
* <blockquote>
* <pre>public boolean barProxy(Foo someObject, int abc, int def,
* int someInt, String someString)</pre>
* </blockquote>
*/
@Target({ ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
@@ -37,7 +37,6 @@ import org.spongepowered.asm.mixin.injection.InvalidInjectionException;
import org.spongepowered.asm.mixin.injection.code.Injector;
import org.spongepowered.asm.mixin.injection.struct.InjectionInfo;
import org.spongepowered.asm.mixin.injection.struct.Target;
import org.spongepowered.asm.util.ASMHelper;
/**
* Base class for injectors which inject at method invokes
@@ -74,7 +73,7 @@ public abstract class InvokeInjector extends Injector {
*/
@Override
protected void sanityCheck(Target target, List<InjectionPoint> injectionPoints) {
if (ASMHelper.methodIsStatic(target.method) != this.isStatic) {
if (target.isStatic != this.isStatic) {
throw new InvalidInjectionException("'static' modifier of callback method does not match target in " + this.methodNode.name);
}
}
@@ -102,6 +101,15 @@ public abstract class InvokeInjector extends Injector {
*/
protected abstract void inject(Target target, MethodInsnNode node);
/**
* @param args handler arguments
* @param insns InsnList to inject insns into
* @param argMap Mapping of args to local variables
*/
protected void invokeHandlerWithArgs(Type[] args, InsnList insns, int[] argMap) {
this.invokeHandlerWithArgs(args, insns, argMap, 0, args.length);
}
/**
* @param args handler arguments
* @param insns InsnList to inject insns into
@@ -35,6 +35,7 @@ import org.spongepowered.asm.mixin.injection.struct.Target;
import org.spongepowered.asm.util.ASMHelper;
import com.google.common.collect.ObjectArrays;
import com.google.common.primitives.Ints;
/**
* <p>A bytecode injector which allows a method call to be redirected to the
@@ -76,6 +77,7 @@ public class RedirectInjector extends InvokeInjector {
*/
@Override
protected void inject(Target target, MethodInsnNode node) {
boolean injectTargetParams = false;
boolean targetIsStatic = node.getOpcode() == Opcodes.INVOKESTATIC;
Type ownerType = Type.getType("L" + node.owner + ";");
Type returnType = Type.getReturnType(node.desc);
@@ -84,14 +86,23 @@ public class RedirectInjector extends InvokeInjector {
String desc = Injector.printArgs(stackVars) + returnType;
if (!desc.equals(this.methodNode.desc)) {
throw new InvalidInjectionException("@Redirect handler method has an invalid signature "
+ ", expected " + desc + " found " + this.methodNode.desc);
String alternateDesc = Injector.printArgs(ObjectArrays.concat(stackVars, target.arguments, Type.class)) + returnType;
if (alternateDesc.equals(this.methodNode.desc)) {
injectTargetParams = true;
} else {
throw new InvalidInjectionException("@Redirect handler method has an invalid signature "
+ ", expected " + desc + " found " + this.methodNode.desc);
}
}
InsnList insns = new InsnList();
int extraLocals = ASMHelper.getArgsSize(stackVars) + 1;
int[] argMap = this.storeArgs(target, stackVars, insns, 0);
this.invokeHandlerWithArgs(stackVars, insns, argMap, 0, stackVars.length);
if (injectTargetParams) {
extraLocals += ASMHelper.getArgsSize(target.arguments);
argMap = Ints.concat(argMap, target.argIndices);
}
this.invokeHandlerWithArgs(this.methodArgs, insns, argMap);
target.method.instructions.insertBefore(node, insns);
target.method.instructions.remove(node);
@@ -27,6 +27,7 @@ package org.spongepowered.asm.mixin.injection.struct;
import org.objectweb.asm.Type;
import org.objectweb.asm.tree.MethodNode;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import org.spongepowered.asm.util.ASMHelper;
/**
@@ -40,11 +41,21 @@ public class Target {
*/
public final MethodNode method;
/**
* True if the method is static
*/
public final boolean isStatic;
/**
* Method arguments
*/
public final Type[] arguments;
/**
* Method argument slots
*/
public final int[] argIndices;
/**
* Return type computed from the method descriptor
*/
@@ -77,7 +88,9 @@ public class Target {
*/
public Target(MethodNode method) {
this.method = method;
this.isStatic = ASMHelper.methodIsStatic(method);
this.arguments = Type.getArgumentTypes(method.desc);
this.argIndices = this.calcArgIndices(this.isStatic ? 0 : 1);
this.returnType = Type.getReturnType(method.desc);
this.maxStack = method.maxStack;
@@ -86,6 +99,15 @@ public class Target {
this.callbackDescriptor = String.format("(%sL%s;)V", method.desc.substring(1, method.desc.indexOf(')')), this.callbackInfoClass);
}
private int[] calcArgIndices(int local) {
int[] argIndices = new int[this.arguments.length];
for (int arg = 0; arg < this.arguments.length; arg++) {
argIndices[arg] = local;
local += this.arguments[arg].getSize();
}
return argIndices;
}
/**
* Get the callback descriptor
*