diff --git a/gradle.properties b/gradle.properties index 6ebb165b..912e9531 100644 --- a/gradle.properties +++ b/gradle.properties @@ -4,5 +4,5 @@ packaging=jar description=Mixin url=https://www.spongepowered.org organization=SpongePowered -buildVersion=0.4.19 +buildVersion=0.5 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 42c9ba98..66921579 100644 --- a/src/main/java/org/spongepowered/asm/launch/MixinBootstrap.java +++ b/src/main/java/org/spongepowered/asm/launch/MixinBootstrap.java @@ -60,7 +60,7 @@ public abstract class MixinBootstrap { /** * Subsystem version */ - public static final String VERSION = "0.4.19"; + public static final String VERSION = "0.5"; /** * Blackboard key where the subsystem version will be stored to indicate diff --git a/src/main/java/org/spongepowered/asm/mixin/injection/InjectionNodes.java b/src/main/java/org/spongepowered/asm/mixin/injection/InjectionNodes.java new file mode 100644 index 00000000..53229645 --- /dev/null +++ b/src/main/java/org/spongepowered/asm/mixin/injection/InjectionNodes.java @@ -0,0 +1,142 @@ +/* + * This file is part of Mixin, licensed under the MIT License (MIT). + * + * Copyright (c) SpongePowered + * Copyright (c) contributors + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +package org.spongepowered.asm.mixin.injection; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.Map; + +import org.spongepowered.asm.lib.tree.AbstractInsnNode; +import org.spongepowered.asm.util.ASMHelper; + +public class InjectionNodes extends ArrayList { + + private static final long serialVersionUID = 1L; + + public static class InjectionNode implements Comparable { + + private final AbstractInsnNode originalTarget; + + private AbstractInsnNode currentTarget; + + private Map decorations; + + public InjectionNode(AbstractInsnNode target) { + this.currentTarget = this.originalTarget = target; + } + + public AbstractInsnNode getOriginalTarget() { + return this.originalTarget; + } + + public AbstractInsnNode getCurrentTarget() { + return this.currentTarget; + } + + public void replace(AbstractInsnNode currentTarget) { + this.currentTarget = currentTarget; + } + + public void remove() { + this.currentTarget = null; + } + + public boolean matches(AbstractInsnNode node) { + return this.originalTarget == node || this.currentTarget == node; + } + + public boolean isReplaced() { + return this.originalTarget != this.currentTarget; + } + + public boolean isRemoved() { + return this.currentTarget == null; + } + + public void decorate(K key, V value) { + if (this.decorations == null) { + this.decorations = new HashMap(); + } + this.decorations.put(key, value); + } + + public boolean hasDecoration(K key) { + return this.decorations != null && this.decorations.get(key) != null; + } + + @SuppressWarnings("unchecked") + public V getDecoration(K key) { + return (V) (this.decorations == null ? null : this.decorations.get(key)); + } + + @Override + public int compareTo(InjectionNode other) { + return other == null ? Integer.MAX_VALUE : this.hashCode() - other.hashCode(); + } + + @Override + public String toString() { + return String.format("InjectionNode[%s]", ASMHelper.getNodeDescriptionForDebug(this.currentTarget)); + } + + } + + public InjectionNode add(AbstractInsnNode node) { + InjectionNode injectionNode = this.get(node); + if (injectionNode == null) { + injectionNode = new InjectionNode(node); + this.add(injectionNode); + } + return injectionNode; + } + + public InjectionNode get(AbstractInsnNode node) { + for (InjectionNode injectionNode : this) { + if (injectionNode.matches(node)) { + return injectionNode; + } + } + return null; + } + + public boolean contains(AbstractInsnNode node) { + return this.get(node) != null; + } + + public void replace(AbstractInsnNode oldNode, AbstractInsnNode newNode) { + InjectionNode injectionNode = this.get(oldNode); + if (injectionNode != null) { + injectionNode.replace(newNode); + } + } + + public void remove(AbstractInsnNode oldNode) { + InjectionNode injectionNode = this.get(oldNode); + if (injectionNode != null) { + injectionNode.remove(); + } + } + +} diff --git a/src/main/java/org/spongepowered/asm/mixin/injection/callback/CallbackInjector.java b/src/main/java/org/spongepowered/asm/mixin/injection/callback/CallbackInjector.java index ae89da2e..2a71e7cc 100644 --- a/src/main/java/org/spongepowered/asm/mixin/injection/callback/CallbackInjector.java +++ b/src/main/java/org/spongepowered/asm/mixin/injection/callback/CallbackInjector.java @@ -27,13 +27,12 @@ package org.spongepowered.asm.mixin.injection.callback; import java.util.ArrayList; import java.util.List; -import org.apache.logging.log4j.LogManager; -import org.apache.logging.log4j.Logger; import org.spongepowered.asm.lib.Opcodes; import org.spongepowered.asm.lib.Type; import org.spongepowered.asm.lib.tree.*; import org.spongepowered.asm.mixin.injection.Coerce; import org.spongepowered.asm.mixin.injection.InjectionError; +import org.spongepowered.asm.mixin.injection.InjectionNodes.InjectionNode; import org.spongepowered.asm.mixin.injection.InjectionPoint; import org.spongepowered.asm.mixin.injection.InvalidInjectionException; import org.spongepowered.asm.mixin.injection.Surrogate; @@ -148,10 +147,10 @@ public class CallbackInjector extends Injector { */ private boolean captureArgs = true; - Callback(MethodNode handler, Target target, final AbstractInsnNode node, final LocalVariableNode[] locals, boolean captureLocals) { + Callback(MethodNode handler, Target target, final InjectionNode node, final LocalVariableNode[] locals, boolean captureLocals) { this.handler = handler; this.target = target; - this.node = node; + this.node = node.getCurrentTarget(); this.locals = locals; this.localTypes = locals != null ? new Type[locals.length] : null; this.frameSize = ASMHelper.getFirstNonArgLocalIndex(target.arguments, !CallbackInjector.this.isStatic()); @@ -277,8 +276,6 @@ public class CallbackInjector extends Injector { } } - private final Logger logger = LogManager.getLogger("mixin"); - /** * True if cancellable */ @@ -332,11 +329,19 @@ public class CallbackInjector extends Injector { * org.objectweb.asm.tree.AbstractInsnNode) */ @Override - protected void inject(Target target, AbstractInsnNode node) { + protected void inject(Target target, InjectionNode node) { LocalVariableNode[] locals = null; + if (node.isReplaced()) { + System.err.printf("Injector target was replaced!!!\n"); + System.err.printf("ORIGINAL: "); + ASMHelper.printNode(node.getOriginalTarget()); + System.err.printf(" CURRENT: "); + ASMHelper.printNode(node.getCurrentTarget()); + } + if (this.localCapture.isCaptureLocals() || this.localCapture.isPrintLocals()) { - locals = Locals.getLocalsAt(this.classNode, target.method, node); + locals = Locals.getLocalsAt(this.classNode, target.method, node.getCurrentTarget()); } this.inject(new Callback(this.methodNode, target, node, locals, this.localCapture.isCaptureLocals())); @@ -379,14 +384,14 @@ public class CallbackInjector extends Injector { switch (this.localCapture) { case CAPTURE_FAILEXCEPTION: - this.logger.error("Injection error: {}", message); + Injector.logger.error("Injection error: {}", message); callbackMethod = this.generateErrorMethod(callback, "org/spongepowered/asm/mixin/injection/InjectionError", message); break; case CAPTURE_FAILSOFT: - this.logger.warn("Injection warning: {}", message); + Injector.logger.warn("Injection warning: {}", message); return; default: - this.logger.error("Critical injection failure: {}", message); + Injector.logger.error("Critical injection failure: {}", message); throw new InjectionError(message); } } 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 c52d789e..27933b4c 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 @@ -30,6 +30,8 @@ import java.util.HashSet; import java.util.List; import java.util.Set; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; import org.spongepowered.asm.lib.Opcodes; import org.spongepowered.asm.lib.Type; import org.spongepowered.asm.lib.tree.AbstractInsnNode; @@ -37,6 +39,8 @@ 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.mixin.MixinEnvironment.Option; +import org.spongepowered.asm.mixin.injection.InjectionNodes.InjectionNode; import org.spongepowered.asm.mixin.injection.InjectionPoint; import org.spongepowered.asm.mixin.injection.InvalidInjectionException; import org.spongepowered.asm.mixin.injection.struct.InjectionInfo; @@ -50,6 +54,11 @@ import com.google.common.base.Joiner; */ public abstract class Injector { + /** + * Log more things + */ + protected static final Logger logger = LogManager.getLogger("mixin"); + /** * Injection info */ @@ -110,16 +119,35 @@ public abstract class Injector { } /** - * Inject into the specified method at the specified injection points + * ... * * @param target Target method to inject into * @param injectionPoints InjectionPoint instances which will identify * target insns in the target method + * @return discovered injection points */ - public final void injectInto(Target target, List injectionPoints) { + public final List find(Target target, List injectionPoints) { this.sanityCheck(target, injectionPoints); - + + List myNodes = new ArrayList(); for (AbstractInsnNode node : this.findTargetNodes(target.method, injectionPoints)) { + this.addTargetNode(target, myNodes, node); + } + return myNodes; + } + + protected void addTargetNode(Target target, List myNodes, AbstractInsnNode node) { + myNodes.add(target.injectionNodes.add(node)); + } + + public final void inject(Target target, List nodes) { + for (InjectionNode node : nodes) { + if (node.isRemoved()) { + if (this.info.getContext().getEnvironment().getOption(Option.DEBUG_VERBOSE)) { + Injector.logger.warn("Target node for {} was removed by a previous injector in {}", this.info, target); + } + continue; + } this.inject(target, node); } } @@ -161,15 +189,15 @@ public abstract class Injector { } } - protected abstract void inject(Target target, AbstractInsnNode node); + protected abstract void inject(Target target, InjectionNode node); /** * Invoke the handler method * * @param insns Instruction list to inject into */ - protected void invokeHandler(InsnList insns) { - this.invokeHandler(insns, this.methodNode); + protected AbstractInsnNode invokeHandler(InsnList insns) { + return this.invokeHandler(insns, this.methodNode); } /** @@ -178,12 +206,15 @@ public abstract class Injector { * @param insns Instruction list to inject into * @param handler Actual method to invoke (may be different if using a * surrogate) + * @return */ - protected void invokeHandler(InsnList insns, MethodNode handler) { + protected AbstractInsnNode invokeHandler(InsnList insns, MethodNode handler) { boolean isPrivate = (handler.access & Opcodes.ACC_PRIVATE) != 0; int invokeOpcode = this.isStatic ? Opcodes.INVOKESTATIC : isPrivate ? Opcodes.INVOKESPECIAL : Opcodes.INVOKEVIRTUAL; - insns.add(new MethodInsnNode(invokeOpcode, this.classNode.name, handler.name, handler.desc, false)); + MethodInsnNode insn = new MethodInsnNode(invokeOpcode, this.classNode.name, handler.name, handler.desc, false); + insns.add(insn); this.info.addCallbackInvocation(handler); + return insn; } protected static String printArgs(Type[] args) { 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 e28d3785..6281003d 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 @@ -32,6 +32,7 @@ import org.spongepowered.asm.lib.tree.AbstractInsnNode; import org.spongepowered.asm.lib.tree.InsnList; import org.spongepowered.asm.lib.tree.MethodInsnNode; import org.spongepowered.asm.lib.tree.VarInsnNode; +import org.spongepowered.asm.mixin.injection.InjectionNodes.InjectionNode; import org.spongepowered.asm.mixin.injection.InjectionPoint; import org.spongepowered.asm.mixin.injection.InvalidInjectionException; import org.spongepowered.asm.mixin.injection.code.Injector; @@ -74,13 +75,13 @@ public abstract class InvokeInjector extends Injector { * org.objectweb.asm.tree.AbstractInsnNode) */ @Override - protected void inject(Target target, AbstractInsnNode node) { - if (!(node instanceof MethodInsnNode)) { + protected void inject(Target target, InjectionNode node) { + if (!(node.getCurrentTarget() instanceof MethodInsnNode)) { throw new InvalidInjectionException(this.info, this.annotationType + " annotation on is targetting a non-method insn in " + target + " in " + this); } - this.injectAtInvoke(target, (MethodInsnNode)node); + this.injectAtInvoke(target, node); } /** @@ -89,15 +90,15 @@ public abstract class InvokeInjector extends Injector { * @param target Target to inject into * @param node Discovered instruction node */ - protected abstract void injectAtInvoke(Target target, MethodInsnNode node); + protected abstract void injectAtInvoke(Target target, InjectionNode 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); + protected AbstractInsnNode invokeHandlerWithArgs(Type[] args, InsnList insns, int[] argMap) { + return this.invokeHandlerWithArgs(args, insns, argMap, 0, args.length); } /** @@ -107,12 +108,12 @@ public abstract class InvokeInjector extends Injector { * @param startArg Starting arg to consume * @param endArg Ending arg to consume */ - protected void invokeHandlerWithArgs(Type[] args, InsnList insns, int[] argMap, int startArg, int endArg) { + protected AbstractInsnNode invokeHandlerWithArgs(Type[] args, InsnList insns, int[] argMap, int startArg, int endArg) { if (!this.isStatic) { insns.add(new VarInsnNode(Opcodes.ALOAD, 0)); } this.pushArgs(args, insns, argMap, startArg, endArg); - this.invokeHandler(insns); + return this.invokeHandler(insns); } /** diff --git a/src/main/java/org/spongepowered/asm/mixin/injection/invoke/ModifyArgInjector.java b/src/main/java/org/spongepowered/asm/mixin/injection/invoke/ModifyArgInjector.java index b67b2afa..419b9b72 100644 --- a/src/main/java/org/spongepowered/asm/mixin/injection/invoke/ModifyArgInjector.java +++ b/src/main/java/org/spongepowered/asm/mixin/injection/invoke/ModifyArgInjector.java @@ -32,6 +32,7 @@ import org.spongepowered.asm.lib.tree.InsnList; import org.spongepowered.asm.lib.tree.MethodInsnNode; import org.spongepowered.asm.mixin.injection.InjectionPoint; import org.spongepowered.asm.mixin.injection.InvalidInjectionException; +import org.spongepowered.asm.mixin.injection.InjectionNodes.InjectionNode; import org.spongepowered.asm.mixin.injection.code.Injector; import org.spongepowered.asm.mixin.injection.struct.InjectionInfo; import org.spongepowered.asm.mixin.injection.struct.Target; @@ -86,8 +87,9 @@ public class ModifyArgInjector extends InvokeInjector { * Do the injection */ @Override - protected void injectAtInvoke(Target target, MethodInsnNode node) { - Type[] args = Type.getArgumentTypes(node.desc); + protected void injectAtInvoke(Target target, InjectionNode node) { + MethodInsnNode methodNode = (MethodInsnNode)node.getCurrentTarget(); + Type[] args = Type.getArgumentTypes(methodNode.desc); int argIndex = this.findArgIndex(target, args); InsnList insns = new InsnList(); int extraLocals = 0; @@ -98,7 +100,7 @@ public class ModifyArgInjector extends InvokeInjector { extraLocals = this.injectMultiArgHandler(target, args, argIndex, insns); } - target.insns.insertBefore(node, insns); + target.insns.insertBefore(methodNode, insns); target.addToLocals(extraLocals); target.addToStack(2 - (extraLocals - 1)); } 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 40882861..1114b912 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,6 +24,8 @@ */ package org.spongepowered.asm.mixin.injection.invoke; +import java.util.List; + import org.spongepowered.asm.lib.Opcodes; import org.spongepowered.asm.lib.Type; import org.spongepowered.asm.lib.tree.AbstractInsnNode; @@ -32,6 +34,8 @@ 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.mixin.Final; +import org.spongepowered.asm.mixin.injection.InjectionNodes.InjectionNode; import org.spongepowered.asm.mixin.injection.InvalidInjectionException; import org.spongepowered.asm.mixin.injection.Redirect; import org.spongepowered.asm.mixin.injection.code.Injector; @@ -75,44 +79,89 @@ import com.google.common.primitives.Ints; */ public class RedirectInjector extends InvokeInjector { + private final int priority; + + private final boolean isFinal; + /** * @param info Injection info */ public RedirectInjector(InjectionInfo info) { super(info, "@Redirect"); + this.priority = info.getContext().getPriority(); + this.isFinal = ASMHelper.getVisibleAnnotation(this.methodNode, Final.class) != null; + } + + @Override + protected void addTargetNode(Target target, List myNodes, AbstractInsnNode insn) { + InjectionNode node = target.injectionNodes.get(insn); + + if (node != null ) { + boolean isRedirect = node.getDecoration("redir:ect") == Boolean.TRUE; + RedirectInjector redirector = node.getDecoration("redir:owner"); + Boolean isFinal = node.getDecoration("redir:final"); + Integer priority = node.getDecoration("redir:priority"); + + if (isRedirect && redirector != this) { + if (priority >= this.priority) { + Injector.logger.warn("{} conflict. Skipping {} with priority {}, already redirected by {} with priority {}", + this.annotationType, this.info, this.priority, node.getDecoration("redir:name"), node.getDecoration("redir:priority")); + return; + } else if (isFinal != null && isFinal.booleanValue()) { + throw new InvalidInjectionException(this.info, this.annotationType + " conflict: " + this + + " failed because target was already remapped by " + node.getDecoration("redir:name")); + } + } + } + + node = target.injectionNodes.add(insn); + node.decorate("redir:ect", Boolean.TRUE); + node.decorate("redir:owner", this); + node.decorate("redir:priority", Integer.valueOf(this.priority)); + node.decorate("redir:final", Boolean.valueOf(this.isFinal)); + node.decorate("redir:name", this.info.toString()); + node.decorate("redir:desc", this.methodNode.desc); + + myNodes.add(node); } - /* (non-Javadoc) - * @see org.spongepowered.asm.mixin.injection.callback.BytecodeInjector - * #inject(org.spongepowered.asm.mixin.injection.callback.Target, - * org.objectweb.asm.tree.AbstractInsnNode) - */ @Override - protected void inject(Target target, AbstractInsnNode node) { - if (node instanceof MethodInsnNode) { - this.injectAtInvoke(target, (MethodInsnNode)node); + protected void inject(Target target, InjectionNode node) { + if (node.getDecoration("redir:owner") != this) { + Injector.logger.warn("{} conflict. Skipping {} with priority {}, already redirected by {} with priority {}", + this.annotationType, this.info, this.priority, node.getDecoration("redir:name"), node.getDecoration("redir:priority")); + return; + } + + if (node.isReplaced()) { + throw new UnsupportedOperationException("Redirector target failure for " + this.info); + } + + if (node.getCurrentTarget() instanceof MethodInsnNode) { + this.injectAtInvoke(target, node); return; } - if (node instanceof FieldInsnNode) { - this.injectAtFieldAccess(target, (FieldInsnNode)node); + if (node.getCurrentTarget() instanceof FieldInsnNode) { + this.injectAtFieldAccess(target, node); return; } throw new InvalidInjectionException(this.info, this.annotationType + " annotation on is targetting an invalid insn in " + target + " in " + this); } - + /** * Redirect a method invocation */ @Override - protected void injectAtInvoke(Target target, MethodInsnNode node) { + protected void injectAtInvoke(Target target, InjectionNode node) { + MethodInsnNode methodNode = (MethodInsnNode)node.getCurrentTarget(); boolean injectTargetParams = false; - boolean targetIsStatic = node.getOpcode() == Opcodes.INVOKESTATIC; - Type ownerType = Type.getType("L" + node.owner + ";"); - Type returnType = Type.getReturnType(node.desc); - Type[] args = Type.getArgumentTypes(node.desc); + boolean targetIsStatic = methodNode.getOpcode() == Opcodes.INVOKESTATIC; + Type ownerType = Type.getType("L" + methodNode.owner + ";"); + Type returnType = Type.getReturnType(methodNode.desc); + Type[] args = Type.getArgumentTypes(methodNode.desc); Type[] stackVars = targetIsStatic ? args : ObjectArrays.concat(ownerType, args); String desc = Injector.printArgs(stackVars) + returnType; @@ -136,10 +185,8 @@ public class RedirectInjector extends InvokeInjector { extraStack += argSize; argMap = Ints.concat(argMap, target.argIndices); } - this.invokeHandlerWithArgs(this.methodArgs, insns, argMap); - - target.insns.insertBefore(node, insns); - target.insns.remove(node); + AbstractInsnNode insn = this.invokeHandlerWithArgs(this.methodArgs, insns, argMap); + target.replaceNode(methodNode, insn, insns); target.addToLocals(extraLocals); target.addToStack(extraStack); } @@ -147,22 +194,23 @@ public class RedirectInjector extends InvokeInjector { /** * Redirect a field get or set operation */ - private void injectAtFieldAccess(Target target, FieldInsnNode node) { - boolean staticField = node.getOpcode() == Opcodes.GETSTATIC || node.getOpcode() == Opcodes.PUTSTATIC; - Type ownerType = Type.getType("L" + node.owner + ";"); - Type fieldType = Type.getType(node.desc); + private void injectAtFieldAccess(Target target, InjectionNode node) { + FieldInsnNode fieldNode = (FieldInsnNode)node.getCurrentTarget(); + boolean staticField = fieldNode.getOpcode() == Opcodes.GETSTATIC || fieldNode.getOpcode() == Opcodes.PUTSTATIC; + Type ownerType = Type.getType("L" + fieldNode.owner + ";"); + Type fieldType = Type.getType(fieldNode.desc); + AbstractInsnNode invoke = null; InsnList insns = new InsnList(); - if (node.getOpcode() == Opcodes.GETSTATIC || node.getOpcode() == Opcodes.GETFIELD) { - this.injectAtGetField(insns, target, node, staticField, ownerType, fieldType); - } else if (node.getOpcode() == Opcodes.PUTSTATIC || node.getOpcode() == Opcodes.PUTFIELD) { - this.injectAtPutField(insns, target, node, staticField, ownerType, fieldType); + if (fieldNode.getOpcode() == Opcodes.GETSTATIC || fieldNode.getOpcode() == Opcodes.GETFIELD) { + invoke = this.injectAtGetField(insns, target, fieldNode, staticField, ownerType, fieldType); + } else if (fieldNode.getOpcode() == Opcodes.PUTSTATIC || fieldNode.getOpcode() == Opcodes.PUTFIELD) { + invoke = this.injectAtPutField(insns, target, fieldNode, staticField, ownerType, fieldType); } else { - throw new InvalidInjectionException(this.info, "Unspported opcode " + node.getOpcode() + " on FieldInsnNode for " + this.info); + throw new InvalidInjectionException(this.info, "Unspported opcode " + fieldNode.getOpcode() + " on FieldInsnNode for " + this.info); } - target.insns.insertBefore(node, insns); - target.insns.remove(node); + target.replaceNode(fieldNode, invoke, insns); } /** @@ -171,7 +219,7 @@ public class RedirectInjector extends InvokeInjector { * possible scenarios based on the possible combinations of static on the * handler and the field itself. */ - private void injectAtGetField(InsnList insns, Target target, FieldInsnNode node, boolean staticField, Type owner, Type fieldType) { + private AbstractInsnNode injectAtGetField(InsnList insns, Target target, FieldInsnNode node, boolean staticField, Type owner, Type fieldType) { String handlerDesc = staticField ? ASMHelper.generateDescriptor(fieldType) : ASMHelper.generateDescriptor(fieldType, owner); boolean withArgs = this.checkDescriptor(handlerDesc, target, "getter"); @@ -187,8 +235,8 @@ public class RedirectInjector extends InvokeInjector { target.addToStack(ASMHelper.getArgsSize(target.arguments)); } - this.invokeHandler(insns); target.addToStack(this.isStatic ? 0 : 1); + return this.invokeHandler(insns); } /** @@ -197,7 +245,7 @@ public class RedirectInjector extends InvokeInjector { * possible scenarios based on the possible combinations of static on the * handler and the field itself. */ - private void injectAtPutField(InsnList insns, Target target, FieldInsnNode node, boolean staticField, Type owner, Type fieldType) { + private AbstractInsnNode injectAtPutField(InsnList insns, Target target, FieldInsnNode node, boolean staticField, Type owner, Type fieldType) { String handlerDesc = staticField ? ASMHelper.generateDescriptor(null, fieldType) : ASMHelper.generateDescriptor(null, owner, fieldType); boolean withArgs = this.checkDescriptor(handlerDesc, target, "setter"); @@ -219,8 +267,8 @@ public class RedirectInjector extends InvokeInjector { target.addToStack(ASMHelper.getArgsSize(target.arguments)); } - this.invokeHandler(insns); target.addToStack(!this.isStatic && !staticField ? 1 : 0); + return this.invokeHandler(insns); } /** diff --git a/src/main/java/org/spongepowered/asm/mixin/injection/modify/ModifyVariableInjector.java b/src/main/java/org/spongepowered/asm/mixin/injection/modify/ModifyVariableInjector.java index 20c6b6be..743c98fe 100644 --- a/src/main/java/org/spongepowered/asm/mixin/injection/modify/ModifyVariableInjector.java +++ b/src/main/java/org/spongepowered/asm/mixin/injection/modify/ModifyVariableInjector.java @@ -33,6 +33,7 @@ import org.spongepowered.asm.lib.tree.AbstractInsnNode; import org.spongepowered.asm.lib.tree.InsnList; import org.spongepowered.asm.lib.tree.MethodNode; import org.spongepowered.asm.lib.tree.VarInsnNode; +import org.spongepowered.asm.mixin.injection.InjectionNodes.InjectionNode; import org.spongepowered.asm.mixin.injection.InjectionPoint; import org.spongepowered.asm.mixin.injection.InvalidInjectionException; import org.spongepowered.asm.mixin.injection.ModifyVariable; @@ -142,8 +143,12 @@ public class ModifyVariableInjector extends Injector { * Do the injection */ @Override - protected void inject(Target target, AbstractInsnNode node) { - Context context = new Context(this.returnType, this.discriminator.isArgsOnly(), target, node); + protected void inject(Target target, InjectionNode node) { + if (node.isReplaced()) { + throw new InvalidInjectionException(this.info, "Variable modifier target for " + this + " was removed by another injector"); + } + + Context context = new Context(this.returnType, this.discriminator.isArgsOnly(), target, node.getCurrentTarget()); if (this.print) { this.printLocals(context); @@ -158,7 +163,7 @@ public class ModifyVariableInjector extends Injector { throw new InvalidInjectionException(this.info, "Implicit variable modifier injection failed in " + this, ex); } - target.insns.insertBefore(node, context.insns); + target.insns.insertBefore(context.node, context.insns); target.addToStack(this.isStatic ? 1 : 2); } diff --git a/src/main/java/org/spongepowered/asm/mixin/injection/struct/InjectionInfo.java b/src/main/java/org/spongepowered/asm/mixin/injection/struct/InjectionInfo.java index 6874e714..8ed5a2b1 100644 --- a/src/main/java/org/spongepowered/asm/mixin/injection/struct/InjectionInfo.java +++ b/src/main/java/org/spongepowered/asm/mixin/injection/struct/InjectionInfo.java @@ -28,7 +28,10 @@ import java.util.ArrayDeque; import java.util.ArrayList; import java.util.Collection; import java.util.Deque; +import java.util.LinkedHashMap; import java.util.List; +import java.util.Map; +import java.util.Map.Entry; import org.spongepowered.asm.lib.Opcodes; import org.spongepowered.asm.lib.tree.AnnotationNode; @@ -38,6 +41,7 @@ import org.spongepowered.asm.mixin.MixinEnvironment; import org.spongepowered.asm.mixin.MixinEnvironment.Option; import org.spongepowered.asm.mixin.injection.Inject; import org.spongepowered.asm.mixin.injection.InjectionError; +import org.spongepowered.asm.mixin.injection.InjectionNodes.InjectionNode; import org.spongepowered.asm.mixin.injection.InjectionPoint; import org.spongepowered.asm.mixin.injection.InjectorGroupInfo; import org.spongepowered.asm.mixin.injection.InvalidInjectionException; @@ -91,6 +95,11 @@ public abstract class InjectionInfo { */ protected final List injectionPoints = new ArrayList(); + /** + * Map of lists of nodes enumerated by calling {@link #prepare} + */ + protected final Map> targetNodes = new LinkedHashMap>(); + /** * Bytecode injector */ @@ -216,12 +225,21 @@ public abstract class InjectionInfo { } /** - * Perform the injection + * Discover injection points */ - public void inject() { + public void prepare() { while (this.targets.size() > 0) { Target target = this.mixin.getTargetMethod(this.targets.removeFirst()); - this.injector.injectInto(target, this.injectionPoints); + this.targetNodes.put(target, this.injector.find(target, this.injectionPoints)); + } + } + + /** + * Perform injections + */ + public void inject() { + for (Entry> entry : this.targetNodes.entrySet()) { + this.injector.inject(entry.getKey(), entry.getValue()); } } 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 f387b8cf..88a9c429 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 @@ -25,9 +25,11 @@ package org.spongepowered.asm.mixin.injection.struct; 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.MethodNode; +import org.spongepowered.asm.mixin.injection.InjectionNodes; import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; import org.spongepowered.asm.util.ASMHelper; @@ -35,7 +37,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 { +public class Target implements Comparable { public final ClassNode classNode; @@ -69,16 +71,6 @@ public class Target { */ public final Type returnType; - /** - * Method's (original) MAXS - */ - private final int maxStack; - - /** - * Method's original max locals - */ - private final int maxLocals; - /** * Callback method descriptor based on this target */ @@ -88,6 +80,21 @@ public class Target { * Callback info class */ public final String callbackInfoClass; + + /** + * Nodes targetted by injectors + */ + public final InjectionNodes injectionNodes = new InjectionNodes(); + + /** + * Method's (original) MAXS + */ + private final int maxStack; + + /** + * Method's original max locals + */ + private final int maxLocals; /** * Make a new Target for the supplied method @@ -272,4 +279,63 @@ public class Target { public String toString() { return String.format("%s::%s%s", this.classNode.name, this.method.name, this.method.desc); } + + @Override + public int compareTo(Target o) { + if (o == null) { + return Integer.MAX_VALUE; + } + return this.toString().compareTo(o.toString()); + } + + /** + * Replace an instruction in this target with the specified instruction and + * mark the node as replaced for other injectors + * + * @param location Instruction to replace + * @param insn Instruction to replace with + */ + public void replaceNode(AbstractInsnNode location, AbstractInsnNode insn) { + this.insns.insertBefore(location, insn); + this.insns.remove(location); + this.injectionNodes.replace(location, insn); + } + + /** + * Replace an instruction in this target with the specified instructions and + * mark the node as replaced with the specified champion node from the list. + * + * @param location Instruction to replace + * @param champion Instruction which notionally replaces the original insn + * @param insns Instructions to actually insert (must contain champion) + */ + public void replaceNode(AbstractInsnNode location, AbstractInsnNode champion, InsnList insns) { + this.insns.insertBefore(location, insns); + this.insns.remove(location); + this.injectionNodes.replace(location, champion); + } + + /** + * Replace an instruction in this target with the specified instructions and + * mark the original node as removed + * + * @param location Instruction to replace + * @param insns Instructions to replace with + */ + public void replaceNode(AbstractInsnNode location, InsnList insns) { + this.insns.insertBefore(location, insns); + this.removeNode(location); + } + + /** + * Remove the specified instruction from the target and mark it as removed + * for injections + * + * @param insn instruction to remove + */ + public void removeNode(AbstractInsnNode insn) { + this.insns.remove(insn); + this.injectionNodes.remove(insn); + } + } diff --git a/src/main/java/org/spongepowered/asm/mixin/transformer/MixinApplicator.java b/src/main/java/org/spongepowered/asm/mixin/transformer/MixinApplicator.java index bcd33795..f46fb8d8 100644 --- a/src/main/java/org/spongepowered/asm/mixin/transformer/MixinApplicator.java +++ b/src/main/java/org/spongepowered/asm/mixin/transformer/MixinApplicator.java @@ -710,7 +710,7 @@ public class MixinApplicator { * @param mixin */ protected void applyInjections(MixinTargetContext mixin) { - List injected = new ArrayList(); + List injectors = new ArrayList(); for (MethodNode method : this.targetClass.methods) { InjectionInfo injectInfo = InjectionInfo.parse(mixin, method); @@ -719,14 +719,18 @@ public class MixinApplicator { } if (injectInfo.isValid()) { - injectInfo.inject(); - injected.add(injectInfo); + injectInfo.prepare(); + injectors.add(injectInfo); } method.visibleAnnotations.remove(injectInfo.getAnnotation()); } - for (InjectionInfo injectInfo : injected) { + for (InjectionInfo injectInfo : injectors) { + injectInfo.inject(); + } + + for (InjectionInfo injectInfo : injectors) { injectInfo.postInject(); } } diff --git a/src/main/java/org/spongepowered/asm/mixin/transformer/MixinTargetContext.java b/src/main/java/org/spongepowered/asm/mixin/transformer/MixinTargetContext.java index f5cfe672..4531c885 100644 --- a/src/main/java/org/spongepowered/asm/mixin/transformer/MixinTargetContext.java +++ b/src/main/java/org/spongepowered/asm/mixin/transformer/MixinTargetContext.java @@ -166,6 +166,15 @@ public class MixinTargetContext implements IReferenceMapperContext { public String toString() { return this.mixin.toString(); } + + /** + * Get the environment of the owning mixin config + * + * @return mixin parent environment + */ + public MixinEnvironment getEnvironment() { + return this.mixin.getParent().getEnvironment(); + } /** * Get the mixin tree diff --git a/src/main/java/org/spongepowered/asm/util/ASMHelper.java b/src/main/java/org/spongepowered/asm/util/ASMHelper.java index 917e4600..7d08b138 100644 --- a/src/main/java/org/spongepowered/asm/util/ASMHelper.java +++ b/src/main/java/org/spongepowered/asm/util/ASMHelper.java @@ -362,32 +362,36 @@ public class ASMHelper { * @param node Node to print */ public static void printNode(AbstractInsnNode node) { - System.err.printf("%-14s ", node.getClass().getSimpleName().replace("Node", "")); - if (node instanceof LabelNode) { - System.err.printf("[%s]", ((LabelNode)node).getLabel()); - } else if (node instanceof JumpInsnNode) { - System.err.printf("[%s] [%s]", ASMHelper.getOpcodeName(node), ((JumpInsnNode)node).label.getLabel()); - } else if (node instanceof VarInsnNode) { - System.err.printf("[%s] %d", ASMHelper.getOpcodeName(node), ((VarInsnNode)node).var); - } else if (node instanceof MethodInsnNode) { - MethodInsnNode mth = (MethodInsnNode)node; - System.err.printf("[%s] %s %s %s", ASMHelper.getOpcodeName(node), mth.owner, mth.name, mth.desc); - } else if (node instanceof FieldInsnNode) { - FieldInsnNode fld = (FieldInsnNode)node; - System.err.printf("[%s] %s %s %s", ASMHelper.getOpcodeName(node), fld.owner, fld.name, fld.desc); - } else if (node instanceof LineNumberNode) { - LineNumberNode ln = (LineNumberNode)node; - System.err.printf("LINE=%d LABEL=[%s]", ln.line, ln.start.getLabel()); - } else if (node instanceof LdcInsnNode) { - System.err.print(((LdcInsnNode)node).cst); - } else if (node instanceof IntInsnNode) { - System.err.print(((IntInsnNode)node).operand); - } else { - System.err.printf("[%s] ", ASMHelper.getOpcodeName(node)); - } - System.err.print("\n"); + System.err.printf("%s\n", ASMHelper.getNodeDescriptionForDebug(node)); } + public static String getNodeDescriptionForDebug(AbstractInsnNode node) { + String out = String.format("%-14s ", node.getClass().getSimpleName().replace("Node", "")); + if (node instanceof LabelNode) { + out += String.format("[%s]", ((LabelNode)node).getLabel()); + } else if (node instanceof JumpInsnNode) { + out += String.format("[%s] [%s]", ASMHelper.getOpcodeName(node), ((JumpInsnNode)node).label.getLabel()); + } else if (node instanceof VarInsnNode) { + out += String.format("[%s] %d", ASMHelper.getOpcodeName(node), ((VarInsnNode)node).var); + } else if (node instanceof MethodInsnNode) { + MethodInsnNode mth = (MethodInsnNode)node; + out += String.format("[%s] %s %s %s", ASMHelper.getOpcodeName(node), mth.owner, mth.name, mth.desc); + } else if (node instanceof FieldInsnNode) { + FieldInsnNode fld = (FieldInsnNode)node; + out += String.format("[%s] %s %s %s", ASMHelper.getOpcodeName(node), fld.owner, fld.name, fld.desc); + } else if (node instanceof LineNumberNode) { + LineNumberNode ln = (LineNumberNode)node; + out += String.format("LINE=%d LABEL=[%s]", ln.line, ln.start.getLabel()); + } else if (node instanceof LdcInsnNode) { + out += (((LdcInsnNode)node).cst); + } else if (node instanceof IntInsnNode) { + out += (((IntInsnNode)node).operand); + } else { + out += String.format("[%s] ", ASMHelper.getOpcodeName(node)); + } + return out; + } + /** * Uses reflection to find an approximate constant name match for the * supplied node's opcode