From 9defcd6245c783d3a685c7bc8530b59a31fc928f Mon Sep 17 00:00:00 2001 From: Mumfrey Date: Sat, 24 Jan 2015 01:16:29 +0000 Subject: [PATCH] Allow Redirect injectors to capture method args as well as invoke args --- .../asm/mixin/injection/Redirect.java | 40 ++++++++++++++++++- .../injection/invoke/InvokeInjector.java | 12 +++++- .../injection/invoke/RedirectInjector.java | 17 ++++++-- .../asm/mixin/injection/struct/Target.java | 22 ++++++++++ 4 files changed, 84 insertions(+), 7 deletions(-) diff --git a/src/main/java/org/spongepowered/asm/mixin/injection/Redirect.java b/src/main/java/org/spongepowered/asm/mixin/injection/Redirect.java index 31ba0acc..a834d971 100644 --- a/src/main/java/org/spongepowered/asm/mixin/injection/Redirect.java +++ b/src/main/java/org/spongepowered/asm/mixin/injection/Redirect.java @@ -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. + *

Specifies that this mixin method should redirect the specified + * {@link #method} call to the method decorated with this annotation.

+ * + *

The handler method signature must match the hooked method precisely + * but 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:

+ * + *
+ *   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);
+ *   }
+ *
+ * + *

The signature of the redirected method should be:

+ * + *
+ *
public boolean barProxy(Foo someObject, int abc, int def)
+ *
+ * + *

For obvious reasons this does not apply for static methods, for static + * methods it is sufficient that the signature simply match the hooked method. + *

+ * + *

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 someInt and someString + * arguments) by appending the arguments to the method signature:

+ * + *
+ *
public boolean barProxy(Foo someObject, int abc, int def,
+ *   int someInt, String someString)
+ *
*/ @Target({ ElementType.METHOD }) @Retention(RetentionPolicy.RUNTIME) diff --git a/src/main/java/org/spongepowered/asm/mixin/injection/invoke/InvokeInjector.java b/src/main/java/org/spongepowered/asm/mixin/injection/invoke/InvokeInjector.java index ae579c3d..45e0802b 100644 --- a/src/main/java/org/spongepowered/asm/mixin/injection/invoke/InvokeInjector.java +++ b/src/main/java/org/spongepowered/asm/mixin/injection/invoke/InvokeInjector.java @@ -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 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 diff --git a/src/main/java/org/spongepowered/asm/mixin/injection/invoke/RedirectInjector.java b/src/main/java/org/spongepowered/asm/mixin/injection/invoke/RedirectInjector.java index e5a8ef6e..f38ea73f 100644 --- a/src/main/java/org/spongepowered/asm/mixin/injection/invoke/RedirectInjector.java +++ b/src/main/java/org/spongepowered/asm/mixin/injection/invoke/RedirectInjector.java @@ -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; /** *

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); diff --git a/src/main/java/org/spongepowered/asm/mixin/injection/struct/Target.java b/src/main/java/org/spongepowered/asm/mixin/injection/struct/Target.java index abd7a56a..91ea6d7f 100644 --- a/src/main/java/org/spongepowered/asm/mixin/injection/struct/Target.java +++ b/src/main/java/org/spongepowered/asm/mixin/injection/struct/Target.java @@ -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 *