diff --git a/gradle.properties b/gradle.properties index 5b9ab77d..deaf8880 100644 --- a/gradle.properties +++ b/gradle.properties @@ -4,5 +4,5 @@ packaging=jar description=Mixin url=https://www.spongepowered.org organization=SpongePowered -buildVersion=0.5.16 +buildVersion=0.5.17 buildType=SNAPSHOT diff --git a/src/main/java/org/spongepowered/asm/launch/MixinBootstrap.java b/src/main/java/org/spongepowered/asm/launch/MixinBootstrap.java index 23e1b8e9..869c67d5 100644 --- a/src/main/java/org/spongepowered/asm/launch/MixinBootstrap.java +++ b/src/main/java/org/spongepowered/asm/launch/MixinBootstrap.java @@ -62,7 +62,7 @@ public abstract class MixinBootstrap { /** * Subsystem version */ - public static final String VERSION = "0.5.16"; + public static final String VERSION = "0.5.17"; // Consts private static final String LAUNCH_PACKAGE = "org.spongepowered.asm.launch."; 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 8ffe9ced..411b1853 100644 --- a/src/main/java/org/spongepowered/asm/mixin/injection/Redirect.java +++ b/src/main/java/org/spongepowered/asm/mixin/injection/Redirect.java @@ -36,7 +36,8 @@ import org.spongepowered.asm.util.ConstraintParser.Constraint; /** *

Specifies that this mixin method should redirect the specified - * method call or field access to the method decorated with this annotation.

+ * method call, field access or object construction (via the new + * keyword) to the method decorated with this annotation.

* *

Method Redirect Mode

* @@ -111,6 +112,28 @@ import org.spongepowered.asm.util.ConstraintParser.Constraint; * 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.

+ * + *

Constructor Redirect Mode

+ * + *

The handler method signature must match the constructor being redirected + * and the return type must match the type of object being constructed. For + * example to redirect the following constructor call:

+ * + *
+ *   public void baz(int someInt, String someString) {
+ *     // Hooking this constructor
+ *     Foo someObject = new Foo("bar");
+ *   }
+ *
+ * + *

The signature of the handler method should be:

+ * + *
+ *
public Foo constructFoo(String arg1)
+ *
+ * + *

Note that like other redirectors, it is possible to capture the target + * method's arguments by appending them to the handler method's signature.

*/ @Target({ ElementType.METHOD }) @Retention(RetentionPolicy.RUNTIME) diff --git a/src/main/java/org/spongepowered/asm/mixin/injection/code/Injector.java b/src/main/java/org/spongepowered/asm/mixin/injection/code/Injector.java index 51b6cedc..baf3d774 100644 --- a/src/main/java/org/spongepowered/asm/mixin/injection/code/Injector.java +++ b/src/main/java/org/spongepowered/asm/mixin/injection/code/Injector.java @@ -37,8 +37,11 @@ import org.spongepowered.asm.lib.Type; import org.spongepowered.asm.lib.tree.AbstractInsnNode; import org.spongepowered.asm.lib.tree.ClassNode; import org.spongepowered.asm.lib.tree.InsnList; +import org.spongepowered.asm.lib.tree.InsnNode; +import org.spongepowered.asm.lib.tree.LdcInsnNode; import org.spongepowered.asm.lib.tree.MethodInsnNode; import org.spongepowered.asm.lib.tree.MethodNode; +import org.spongepowered.asm.lib.tree.TypeInsnNode; import org.spongepowered.asm.mixin.MixinEnvironment.Option; import org.spongepowered.asm.mixin.injection.InjectionNodes.InjectionNode; import org.spongepowered.asm.mixin.injection.InjectionPoint; @@ -218,6 +221,22 @@ public abstract class Injector { return insn; } + /** + * Throw an exception. The exception class must have a string which takes a + * string argument + * + * @param insns Insn list to inject into + * @param exceptionType Type of exception to throw (binary name) + * @param message Message to pass to the exception constructor + */ + protected void throwException(InsnList insns, String exceptionType, String message) { + insns.add(new TypeInsnNode(Opcodes.NEW, exceptionType)); + insns.add(new InsnNode(Opcodes.DUP)); + insns.add(new LdcInsnNode(message)); + insns.add(new MethodInsnNode(Opcodes.INVOKESPECIAL, exceptionType, "", "(Ljava/lang/String;)V", false)); + insns.add(new InsnNode(Opcodes.ATHROW)); + } + protected static String printArgs(Type[] args) { return "(" + Joiner.on("").join(args) + ")"; } 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 49bab166..273b1491 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 @@ -24,16 +24,12 @@ */ package org.spongepowered.asm.mixin.injection.invoke; +import java.util.Iterator; import java.util.List; import org.spongepowered.asm.lib.Opcodes; import org.spongepowered.asm.lib.Type; -import org.spongepowered.asm.lib.tree.AbstractInsnNode; -import org.spongepowered.asm.lib.tree.FieldInsnNode; -import org.spongepowered.asm.lib.tree.InsnList; -import org.spongepowered.asm.lib.tree.InsnNode; -import org.spongepowered.asm.lib.tree.MethodInsnNode; -import org.spongepowered.asm.lib.tree.VarInsnNode; +import org.spongepowered.asm.lib.tree.*; import org.spongepowered.asm.mixin.Final; import org.spongepowered.asm.mixin.injection.InjectionNodes.InjectionNode; import org.spongepowered.asm.mixin.injection.Redirect; @@ -42,18 +38,19 @@ import org.spongepowered.asm.mixin.injection.struct.InjectionInfo; import org.spongepowered.asm.mixin.injection.struct.Target; import org.spongepowered.asm.mixin.injection.throwables.InvalidInjectionException; import org.spongepowered.asm.util.ASMHelper; +import org.spongepowered.asm.util.Constants; import com.google.common.base.Joiner; import com.google.common.collect.ObjectArrays; import com.google.common.primitives.Ints; /** - *

A bytecode injector which allows a method call or field access to be - * redirected to the annotated handler method. For method redirects, 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:

+ *

A bytecode injector which allows a method call, field access or + * new object creation to be redirected to the annotated handler + * method. For method redirects, 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 upon. For example when hooking the following call:

* *
  *   int abc = 0;
@@ -76,6 +73,10 @@ import com.google.common.primitives.Ints;
  * 
  * 

For field redirections, see the details in {@link Redirect} for the * required signature of the handler method.

+ * + *

For constructor redirections, the signature of the handler method should + * match the constructor itself, return type should be of the type of object + * being created.

*/ public class RedirectInjector extends InvokeInjector { @@ -124,6 +125,15 @@ public class RedirectInjector extends InvokeInjector { this.meta = new Meta(priority, isFinal, this.info.toString(), this.methodNode.desc); } + /* (non-Javadoc) + * @see org.spongepowered.asm.mixin.injection.invoke.InvokeInjector + * #checkTarget(org.spongepowered.asm.mixin.injection.struct.Target) + */ + @Override + protected void checkTarget(Target target) { + // Overridden so we can do this check later + } + @Override protected void addTargetNode(Target target, List myNodes, AbstractInsnNode insn) { InjectionNode node = target.injectionNodes.get(insn); @@ -157,15 +167,25 @@ public class RedirectInjector extends InvokeInjector { } if (node.getCurrentTarget() instanceof MethodInsnNode) { + super.checkTarget(target); this.injectAtInvoke(target, node); return; } if (node.getCurrentTarget() instanceof FieldInsnNode) { + super.checkTarget(target); this.injectAtFieldAccess(target, node); return; } + if (node.getCurrentTarget() instanceof TypeInsnNode && node.getCurrentTarget().getOpcode() == Opcodes.NEW) { + if (!this.isStatic && target.isStatic) { + throw new InvalidInjectionException(this.info, "non-static callback method " + this + " has a static target which is not supported"); + } + this.injectAtConstructor(target, node); + return; + } + throw new InvalidInjectionException(this.info, this.annotationType + " annotation on is targetting an invalid insn in " + target + " in " + this); } @@ -303,7 +323,7 @@ public class RedirectInjector extends InvokeInjector { /** * Check that the handler descriptor matches the calculated descriptor for - * the field access being redirected. + * the access being redirected. */ protected boolean checkDescriptor(String desc, Target target, String type) { if (this.methodNode.desc.equals(desc)) { @@ -320,4 +340,67 @@ public class RedirectInjector extends InvokeInjector { + " has an invalid signature. Expected " + desc + " but found " + this.methodNode.desc); } + protected void injectAtConstructor(Target target, InjectionNode node) { + final TypeInsnNode newNode = (TypeInsnNode)node.getCurrentTarget(); + final AbstractInsnNode dupNode = target.get(target.indexOf(newNode) + 1); + final MethodInsnNode initNode = this.findInitNode(target, newNode); + + if (initNode == null) { + throw new InvalidInjectionException(this.info, this.annotationType + " ctor invocation was not found in " + target); + } + + // True if the result of the object construction is being assigned + boolean isAssigned = dupNode.getOpcode() == Opcodes.DUP; + String desc = initNode.desc.replace(")V", ")L" + newNode.desc + ";"); + boolean withArgs = this.checkDescriptor(desc, target, "constructor"); + + if (isAssigned) { + target.removeNode(dupNode); + } + + if (this.isStatic) { + target.removeNode(newNode); + } else { + target.replaceNode(newNode, new VarInsnNode(Opcodes.ALOAD, 0)); + } + + InsnList insns = new InsnList(); + if (withArgs) { + this.pushArgs(target.arguments, insns, target.argIndices, 0, target.arguments.length); + target.addToStack(ASMHelper.getArgsSize(target.arguments)); + } + + this.invokeHandler(insns); + + if (isAssigned) { + // Do a null-check following the redirect to ensure that the handler + // didn't return null. Since NEW cannot return null, this would break + // the contract of the target method! + LabelNode nullCheckSucceeded = new LabelNode(); + insns.add(new InsnNode(Opcodes.DUP)); + insns.add(new JumpInsnNode(Opcodes.IFNONNULL, nullCheckSucceeded)); + this.throwException(insns, "java/lang/NullPointerException", this.annotationType + " constructor handler " + this + + " returned null for " + newNode.desc.replace('/', '.')); + insns.add(nullCheckSucceeded); + } else { + // Result is not assigned, so just pop it from the operand stack + insns.add(new InsnNode(Opcodes.POP)); + } + + target.replaceNode(initNode, insns); + } + + protected MethodInsnNode findInitNode(Target target, TypeInsnNode newNode) { + int indexOf = target.indexOf(newNode); + for (Iterator iter = target.insns.iterator(indexOf); iter.hasNext();) { + AbstractInsnNode insn = iter.next(); + if (insn instanceof MethodInsnNode && insn.getOpcode() == Opcodes.INVOKESPECIAL) { + MethodInsnNode methodNode = (MethodInsnNode)insn; + if (Constants.CTOR.equals(methodNode.name) && methodNode.owner.equals(newNode.desc)) { + return methodNode; + } + } + } + return null; + } } 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 21c6e0dd..d895ee2c 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 @@ -24,6 +24,8 @@ */ package org.spongepowered.asm.mixin.injection.struct; +import java.util.Iterator; + import org.spongepowered.asm.lib.Type; import org.spongepowered.asm.lib.tree.AbstractInsnNode; import org.spongepowered.asm.lib.tree.ClassNode; @@ -37,7 +39,7 @@ import org.spongepowered.asm.util.ASMHelper; * Information about the current injection target, mainly just convenience * rather than passing a bunch of values around. */ -public class Target implements Comparable { +public class Target implements Comparable, Iterable { public final ClassNode classNode; @@ -287,7 +289,20 @@ public class Target implements Comparable { } return this.toString().compareTo(o.toString()); } + + public int indexOf(AbstractInsnNode insn) { + return this.insns.indexOf(insn); + } + public AbstractInsnNode get(int index) { + return this.insns.get(index); + } + + @Override + public Iterator iterator() { + return this.insns.iterator(); + } + /** * Replace an instruction in this target with the specified instruction and * mark the node as replaced for other injectors