diff --git a/gradle.properties b/gradle.properties index fea3bc0f..a3ccbff6 100644 --- a/gradle.properties +++ b/gradle.properties @@ -4,5 +4,5 @@ packaging=jar description=Mixin url=https://www.spongepowered.org organization=SpongePowered -buildVersion=0.6.9 +buildVersion=0.6.10 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 041a9d50..69c9700c 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.6.9"; + public static final String VERSION = "0.6.10"; // 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 6d9bfdd3..b7bcdc5d 100644 --- a/src/main/java/org/spongepowered/asm/mixin/injection/Redirect.java +++ b/src/main/java/org/spongepowered/asm/mixin/injection/Redirect.java @@ -47,15 +47,14 @@ import org.spongepowered.asm.util.ConstraintParser.Constraint; * 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); - * }+ ** *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:
@@ -75,7 +74,7 @@ import org.spongepowered.asm.util.ConstraintParser.Constraint; * *** *public boolean barProxy(Foo someObject, int abc, int def, - * int someInt, String someString)+ * int someInt, String someString) *Field Access Redirect Mode
@@ -121,12 +120,11 @@ import org.spongepowered.asm.util.ConstraintParser.Constraint; * possible to redirect access to individual array elements. Consider the * following example: * - *- * private String[] strings = { "foo", "bar", "baz" }; - * - * public void print(int index) { - * System.err.println(this.strings[index]); - * }+ ** *private String[] strings = { "foo", "bar", "baz" }; + * + *public void print(int index) { + * System.err.println(this.strings[index]); + *}*It may be desirable to redirect the access to this array element. To do @@ -162,11 +160,10 @@ import org.spongepowered.asm.util.ConstraintParser.Constraint; * 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"); - * }+ ** *public void baz(int someInt, String someString) { + * // Hooking this constructor + * Foo someObject = new Foo("bar"); + *}*The signature of the handler method should be:
@@ -177,6 +174,34 @@ import org.spongepowered.asm.util.ConstraintParser.Constraint; * *Note that like other redirectors, it is possible to capture the target * method's arguments by appending them to the handler method's signature.
+ * + *A note on static modifiers for handler methods
+ * + *In general, when declaring a redirect handler the static modifier + * of the handler method must always match the target method. The exception to + * this rule is application of @Redirect to instructions which are + * effectively before the call to super() in a constructor.
+ * + *Consider the following code:
+ * + *+ * + *class Foo : Bar { + * Foo(int arg) { + * super(Foo.isEven(arg)); + * } + * + * static int isEven(int arg) { + * return arg % 2 == 0; + * } + *}+ *In this example, the inline call to Foo.isEven takes place + * before the call to super() and (per Java rules) this method + * must be static (because the instance (this) is unavailable until the + * initialiser is complete. Thus if you wish to @Redirect the call + * to isEven then the handler method must also be static. The + * injection subsystem will raise an exception for non-static handlers in this + * situation to indicate that the staticness of the handler is incorrect.
*/ @Target({ ElementType.METHOD }) @Retention(RetentionPolicy.RUNTIME) 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 dc984469..9e4f779b 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 @@ -25,7 +25,6 @@ package org.spongepowered.asm.mixin.injection.invoke; import java.util.HashMap; -import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Set; @@ -51,9 +50,8 @@ import org.spongepowered.asm.mixin.injection.points.BeforeNew; 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.Bytecode; import org.spongepowered.asm.util.Annotations; -import org.spongepowered.asm.util.Constants; +import org.spongepowered.asm.util.Bytecode; import com.google.common.base.Joiner; import com.google.common.collect.ObjectArrays; @@ -163,7 +161,7 @@ public class RedirectInjector extends InvokeInjector { */ @Override protected void checkTarget(Target target) { - // Overridden so we can do this check later + // Overridden so we can do this check later in a location-aware manner } @Override @@ -227,13 +225,13 @@ public class RedirectInjector extends InvokeInjector { } if (node.getCurrentTarget() instanceof MethodInsnNode) { - super.checkTarget(target); + this.checkTargetForNode(target, node); this.injectAtInvoke(target, node); return; } if (node.getCurrentTarget() instanceof FieldInsnNode) { - super.checkTarget(target); + this.checkTargetForNode(target, node); this.injectAtFieldAccess(target, node); return; } @@ -250,6 +248,32 @@ public class RedirectInjector extends InvokeInjector { + target + " in " + this); } + /** + * 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 redirected before the call to + * super() cannot access this and must therefore be declared + * as static. + * + * @param target Target method + * @param node Injection location + */ + private void checkTargetForNode(Target target, InjectionNode node) { + if (target.isCtor) { + MethodInsnNode superCall = target.findSuperInitNode(); + int superCallIndex = target.indexOf(superCall); + int targetIndex = target.indexOf(node.getCurrentTarget()); + if (targetIndex < superCallIndex) { + if (!this.isStatic) { + throw new InvalidInjectionException(this.info, "@Redirect of pre-super invocation must be static in " + this); + } + return; + } + } + super.checkTarget(target); + } + protected boolean preInject(InjectionNode node) { Meta other = node.getDecoration(Meta.KEY); if (other.getOwner() != this) { @@ -513,7 +537,7 @@ public class RedirectInjector extends InvokeInjector { final TypeInsnNode newNode = (TypeInsnNode)node.getCurrentTarget(); final AbstractInsnNode dupNode = target.get(target.indexOf(newNode) + 1); - final MethodInsnNode initNode = this.findInitNode(target, newNode); + final MethodInsnNode initNode = target.findInitNodeFor(newNode); if (initNode == null) { if (!wildcard) { @@ -572,20 +596,6 @@ public class RedirectInjector extends InvokeInjector { target.replaceNode(initNode, insns); meta.injected++; } - - protected MethodInsnNode findInitNode(Target target, TypeInsnNode newNode) { - int indexOf = target.indexOf(newNode); - for (Iteratoriter = 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; - } private static Type[] getArrayArgs(Type fieldType, Type... extra) { int dimensions = fieldType.getDimensions() + 1; 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 26cd1c51..c60b57b4 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 @@ -26,14 +26,19 @@ package org.spongepowered.asm.mixin.injection.struct; import java.util.Iterator; +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.ClassNode; import org.spongepowered.asm.lib.tree.InsnList; +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.injection.InjectionNodes; import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; +import org.spongepowered.asm.mixin.transformer.ClassInfo; import org.spongepowered.asm.util.Bytecode; +import org.spongepowered.asm.util.Constants; /** * Information about the current injection target, mainly just convenience @@ -61,6 +66,11 @@ public class Target implements Comparable , Iterable { */ public final boolean isStatic; + /** + * True if the method is a constructor + */ + public final boolean isCtor; + /** * Method arguments */ @@ -111,6 +121,7 @@ public class Target implements Comparable , Iterable { this.method = method; this.insns = method.instructions; this.isStatic = Bytecode.methodIsStatic(method); + this.isCtor = method.name.equals(Constants.CTOR); this.arguments = Type.getArgumentTypes(method.desc); this.argIndices = this.calcArgIndices(this.isStatic ? 0 : 1); @@ -337,6 +348,42 @@ public class Target implements Comparable , Iterable { return this.insns.iterator(); } + /** + * Find the first <init> invocation after the specified + * NEW insn + * + * @param newNode NEW insn + * @return INVOKESPECIAL opcode of ctor, or null if not found + */ + public MethodInsnNode findInitNodeFor(TypeInsnNode newNode) { + int start = this.indexOf(newNode); + for (Iterator iter = this.insns.iterator(start); 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; + } + + /** + * Find the call to super() 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. + * + * @return Call to super() or null if not found + */ + public MethodInsnNode findSuperInitNode() { + if (!this.isCtor) { + return null; + } + + return Bytecode.findSuperInit(this.method, ClassInfo.forName(this.classNode.name).getSuperName()); + } + /** * Replace an instruction in this target with the specified instruction and * mark the node as replaced for other injectors diff --git a/src/main/java/org/spongepowered/asm/mixin/transformer/MixinApplicatorStandard.java b/src/main/java/org/spongepowered/asm/mixin/transformer/MixinApplicatorStandard.java index d6e8c799..b2bd93d6 100644 --- a/src/main/java/org/spongepowered/asm/mixin/transformer/MixinApplicatorStandard.java +++ b/src/main/java/org/spongepowered/asm/mixin/transformer/MixinApplicatorStandard.java @@ -588,7 +588,7 @@ class MixinApplicatorStandard { MethodNode target = this.findTargetMethod(method); if (target != null) { - AbstractInsnNode returnNode = MixinApplicatorStandard.findInsn(target, Opcodes.RETURN); + AbstractInsnNode returnNode = Bytecode.findInsn(target, Opcodes.RETURN); if (returnNode != null) { Iterator injectIter = method.instructions.iterator(); @@ -1059,24 +1059,6 @@ class MixinApplicatorStandard { return true; } - /** - * Find the first insn node with a matching opcode in the specified method - * - * @param method method to search - * @param opcode opcode to search for - * @return found node or null if not found - */ - protected static AbstractInsnNode findInsn(MethodNode method, int opcode) { - Iterator findReturnIter = method.instructions.iterator(); - while (findReturnIter.hasNext()) { - AbstractInsnNode insn = findReturnIter.next(); - if (insn.getOpcode() == opcode) { - return insn; - } - } - return null; - } - /** * Returns true if the supplied method contains any line number information * diff --git a/src/main/java/org/spongepowered/asm/util/Bytecode.java b/src/main/java/org/spongepowered/asm/util/Bytecode.java index f531b16f..a29ec4bd 100644 --- a/src/main/java/org/spongepowered/asm/util/Bytecode.java +++ b/src/main/java/org/spongepowered/asm/util/Bytecode.java @@ -165,6 +165,57 @@ public final class Bytecode { return null; } + /** + * Find the first insn node with a matching opcode in the specified method + * + * @param method method to search + * @param opcode opcode to search for + * @return found node or null if not found + */ + public static AbstractInsnNode findInsn(MethodNode method, int opcode) { + Iterator findReturnIter = method.instructions.iterator(); + while (findReturnIter.hasNext()) { + AbstractInsnNode insn = findReturnIter.next(); + if (insn.getOpcode() == opcode) { + return insn; + } + } + return null; + } + + /** + * Find the call to super() 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. + * + * @param method ctor to scan + * @param superName name of superclass + * @return Call to super() or null if not found + */ + public static MethodInsnNode findSuperInit(MethodNode method, String superName) { + if (!Constants.CTOR.equals(method.name)) { + return null; + } + + int news = 0; + for (Iterator iter = method.instructions.iterator(); iter.hasNext();) { + AbstractInsnNode insn = iter.next(); + if (insn instanceof TypeInsnNode && insn.getOpcode() == Opcodes.NEW) { + news++; + } else if (insn instanceof MethodInsnNode && insn.getOpcode() == Opcodes.INVOKESPECIAL) { + MethodInsnNode methodNode = (MethodInsnNode)insn; + if (Constants.CTOR.equals(methodNode.name)) { + if (news > 0) { + news--; + } else if (methodNode.owner.equals(superName)) { + return methodNode; + } + } + } + } + return null; + } + /** * Runs textifier on the specified class node and dumps the output to the * specified output stream