From f5536fb5eefa3e362000752418e57a8e55c903a7 Mon Sep 17 00:00:00 2001 From: Mumfrey Date: Thu, 23 Jan 2020 22:38:36 +0000 Subject: [PATCH] Improve logging implicit match failures for ModifyVariable, fixes #382 Also add some scaffolding for adding the same capability to other injectors --- .../injection/IInjectionPointContext.java | 3 +- .../asm/mixin/injection/InjectionPoint.java | 160 +++++++++++------- .../injection/modify/BeforeLoadLocal.java | 83 +++++---- .../modify/LocalVariableDiscriminator.java | 40 +++-- .../modify/ModifyVariableInjector.java | 33 ++-- .../mixin/injection/struct/InjectionInfo.java | 51 ++++-- .../injection/struct/InjectionPointData.java | 47 +++-- .../asm/mixin/struct/AnnotatedMethodInfo.java | 107 ++++++++++++ .../asm/mixin/struct/SpecialMethodInfo.java | 47 +---- .../spongepowered/asm/util/IMessageSink.java | 40 +++++ 10 files changed, 411 insertions(+), 200 deletions(-) create mode 100644 src/main/java/org/spongepowered/asm/mixin/struct/AnnotatedMethodInfo.java create mode 100644 src/main/java/org/spongepowered/asm/util/IMessageSink.java diff --git a/src/main/java/org/spongepowered/asm/mixin/injection/IInjectionPointContext.java b/src/main/java/org/spongepowered/asm/mixin/injection/IInjectionPointContext.java index e1dd1b7a..b98dcad7 100644 --- a/src/main/java/org/spongepowered/asm/mixin/injection/IInjectionPointContext.java +++ b/src/main/java/org/spongepowered/asm/mixin/injection/IInjectionPointContext.java @@ -27,12 +27,13 @@ package org.spongepowered.asm.mixin.injection; import org.objectweb.asm.tree.AnnotationNode; import org.objectweb.asm.tree.MethodNode; import org.spongepowered.asm.mixin.refmap.IMixinContext; +import org.spongepowered.asm.util.IMessageSink; /** * Context for an injection point, used to access the mixin as well as the owner * method and annotation for the injection point */ -public interface IInjectionPointContext { +public interface IInjectionPointContext extends IMessageSink { /** * Get the mixin context for this injection diff --git a/src/main/java/org/spongepowered/asm/mixin/injection/InjectionPoint.java b/src/main/java/org/spongepowered/asm/mixin/injection/InjectionPoint.java index 08fa108b..5d13cda3 100644 --- a/src/main/java/org/spongepowered/asm/mixin/injection/InjectionPoint.java +++ b/src/main/java/org/spongepowered/asm/mixin/injection/InjectionPoint.java @@ -52,9 +52,11 @@ import org.spongepowered.asm.mixin.injection.points.*; import org.spongepowered.asm.mixin.injection.struct.InjectionPointData; import org.spongepowered.asm.mixin.injection.throwables.InvalidInjectionException; import org.spongepowered.asm.mixin.refmap.IMixinContext; +import org.spongepowered.asm.mixin.struct.AnnotatedMethodInfo; import org.spongepowered.asm.mixin.transformer.MixinTargetContext; import org.spongepowered.asm.util.Annotations; import org.spongepowered.asm.util.Bytecode; +import org.spongepowered.asm.util.IMessageSink; import com.google.common.base.Joiner; import com.google.common.collect.ImmutableList; @@ -62,9 +64,9 @@ import com.google.common.collect.ImmutableList.Builder; /** *

Base class for injection point discovery classes. Each subclass describes - * a strategy for locating code injection points within a method, with the - * {@link #find} method populating a collection with insn nodes from the method - * which satisfy its strategy.

+ * a strategy for locating code injection points within an instruction list, + * with the {@link #find} method populating a collection with insn nodes from + * the supplied list which satisfy its strategy.

* *

This base class also contains composite strategy factory methods such as * {@link #and} and {@link #or} which allow strategies to be combined using @@ -214,21 +216,28 @@ public abstract class InjectionPoint { private final String slice; private final Selector selector; private final String id; + private final IMessageSink messageSink; + protected InjectionPoint() { this("", Selector.DEFAULT, null); } protected InjectionPoint(InjectionPointData data) { - this(data.getSlice(), data.getSelector(), data.getId()); + this(data.getSlice(), data.getSelector(), data.getId(), data.getMessageSink()); } public InjectionPoint(String slice, Selector selector, String id) { + this(slice, selector, id, null); + } + + public InjectionPoint(String slice, Selector selector, String id, IMessageSink messageSink) { this.slice = slice; this.selector = selector; this.id = id; + this.messageSink = messageSink; } - + public String getSlice() { return this.slice; } @@ -241,6 +250,20 @@ public abstract class InjectionPoint { return this.id; } + /** + * Notify method for subclasses to log when notable but non-fatal failures + * occur, for example allows subclasses to add notes when they return no + * results. + * + * @param format Message format + * @param args Format args + */ + protected void addMessage(String format, Object... args) { + if (this.messageSink != null) { + this.messageSink.addMessage(format, args); + } + } + /** * Runs a priority check in the context of this injection point. A priority * check should return true if the injection point is allowed to @@ -501,20 +524,6 @@ public abstract class InjectionPoint { return new InjectionPoint.Shift(point, count); } - /** - * Parse a collection of InjectionPoints from the supplied {@link At} - * annotations - * - * @param owner Data for the mixin containing the annotation, used to obtain - * the refmap, amongst other things - * @param ats {@link At} annotations to parse information from - * @return InjectionPoint parsed from the supplied data or null if parsing - * failed - */ - public static List parse(IInjectionPointContext owner, List ats) { - return InjectionPoint.parse(owner.getContext(), owner.getMethod(), owner.getAnnotation(), ats); - } - /** * Parse a collection of InjectionPoints from the supplied {@link At} * annotations @@ -528,9 +537,23 @@ public abstract class InjectionPoint { * failed */ public static List parse(IMixinContext context, MethodNode method, AnnotationNode parent, List ats) { + return InjectionPoint.parse(new AnnotatedMethodInfo(context, method, parent), ats); + } + + /** + * Parse a collection of InjectionPoints from the supplied {@link At} + * annotations + * + * @param context Data for the mixin containing the annotation, used to obtain + * the refmap, amongst other things + * @param ats {@link At} annotations to parse information from + * @return InjectionPoint parsed from the supplied data or null if parsing + * failed + */ + public static List parse(IInjectionPointContext context, List ats) { Builder injectionPoints = ImmutableList.builder(); for (AnnotationNode at : ats) { - InjectionPoint injectionPoint = InjectionPoint.parse(context, method, parent, at); + InjectionPoint injectionPoint = InjectionPoint.parse(context, at); if (injectionPoint != null) { injectionPoints.add(injectionPoint); } @@ -541,14 +564,14 @@ public abstract class InjectionPoint { /** * Parse an InjectionPoint from the supplied {@link At} annotation * - * @param owner Data for the mixin containing the annotation, used to obtain + * @param context Data for the mixin containing the annotation, used to obtain * the refmap, amongst other things * @param at {@link At} annotation to parse information from * @return InjectionPoint parsed from the supplied data or null if parsing * failed */ - public static InjectionPoint parse(IInjectionPointContext owner, At at) { - return InjectionPoint.parse(owner.getContext(), owner.getMethod(), owner.getAnnotation(), at.value(), at.shift(), at.by(), + public static InjectionPoint parse(IInjectionPointContext context, At at) { + return InjectionPoint.parse(context, at.value(), at.shift(), at.by(), Arrays.asList(at.args()), at.target(), at.slice(), at.ordinal(), at.opcode(), at.id()); } @@ -564,24 +587,10 @@ public abstract class InjectionPoint { * failed */ public static InjectionPoint parse(IMixinContext context, MethodNode method, AnnotationNode parent, At at) { - return InjectionPoint.parse(context, method, parent, at.value(), at.shift(), at.by(), Arrays.asList(at.args()), at.target(), at.slice(), - at.ordinal(), at.opcode(), at.id()); + return InjectionPoint.parse(new AnnotatedMethodInfo(context, method, parent), at.value(), at.shift(), at.by(), Arrays.asList(at.args()), + at.target(), at.slice(), at.ordinal(), at.opcode(), at.id()); } - /** - * Parse an InjectionPoint from the supplied {@link At} annotation supplied - * as an AnnotationNode instance - * - * @param owner Data for the mixin containing the annotation, used to obtain - * the refmap, amongst other things - * @param node {@link At} annotation to parse information from - * @return InjectionPoint parsed from the supplied data or null if parsing - * failed - */ - public static InjectionPoint parse(IInjectionPointContext owner, AnnotationNode node) { - return InjectionPoint.parse(owner.getContext(), owner.getMethod(), owner.getAnnotation(), node); - } - /** * Parse an InjectionPoint from the supplied {@link At} annotation supplied * as an AnnotationNode instance @@ -590,26 +599,40 @@ public abstract class InjectionPoint { * obtain the refmap, amongst other things * @param method The annotated handler method * @param parent The parent annotation which owns this {@link At} annotation - * @param node {@link At} annotation to parse information from + * @param at {@link At} annotation to parse information from * @return InjectionPoint parsed from the supplied data or null if parsing * failed */ - public static InjectionPoint parse(IMixinContext context, MethodNode method, AnnotationNode parent, AnnotationNode node) { - String at = Annotations.getValue(node, "value"); - List args = Annotations.>getValue(node, "args"); - String target = Annotations.getValue(node, "target", ""); - String slice = Annotations.getValue(node, "slice", ""); - At.Shift shift = Annotations.getValue(node, "shift", At.Shift.class, At.Shift.NONE); - int by = Annotations.getValue(node, "by", Integer.valueOf(0)); - int ordinal = Annotations.getValue(node, "ordinal", Integer.valueOf(-1)); - int opcode = Annotations.getValue(node, "opcode", Integer.valueOf(0)); - String id = Annotations.getValue(node, "id"); + public static InjectionPoint parse(IMixinContext context, MethodNode method, AnnotationNode parent, AnnotationNode at) { + return InjectionPoint.parse(new AnnotatedMethodInfo(context, method, parent), at); + } + + /** + * Parse an InjectionPoint from the supplied {@link At} annotation supplied + * as an AnnotationNode instance + * + * @param context Data for the mixin containing the annotation, used to obtain + * the refmap, amongst other things + * @param at {@link At} annotation to parse information from + * @return InjectionPoint parsed from the supplied data or null if parsing + * failed + */ + public static InjectionPoint parse(IInjectionPointContext context, AnnotationNode at) { + String value = Annotations.getValue(at, "value"); + List args = Annotations.>getValue(at, "args"); + String target = Annotations.getValue(at, "target", ""); + String slice = Annotations.getValue(at, "slice", ""); + At.Shift shift = Annotations.getValue(at, "shift", At.Shift.class, At.Shift.NONE); + int by = Annotations.getValue(at, "by", Integer.valueOf(0)); + int ordinal = Annotations.getValue(at, "ordinal", Integer.valueOf(-1)); + int opcode = Annotations.getValue(at, "opcode", Integer.valueOf(0)); + String id = Annotations.getValue(at, "id"); if (args == null) { args = ImmutableList.of(); } - return InjectionPoint.parse(context, method, parent, at, shift, by, args, target, slice, ordinal, opcode, id); + return InjectionPoint.parse(context, value, shift, by, args, target, slice, ordinal, opcode, id); } /** @@ -634,10 +657,33 @@ public abstract class InjectionPoint { */ public static InjectionPoint parse(IMixinContext context, MethodNode method, AnnotationNode parent, String at, At.Shift shift, int by, List args, String target, String slice, int ordinal, int opcode, String id) { - InjectionPointData data = new InjectionPointData(context, method, parent, at, args, target, slice, ordinal, opcode, id); - Class ipClass = findClass(context, data); - InjectionPoint point = InjectionPoint.create(context, data, ipClass); - return InjectionPoint.shift(context, method, parent, point, shift, by); + return InjectionPoint.parse(new AnnotatedMethodInfo(context, method, parent), at, shift, by, args, target, slice, ordinal, opcode, id); + } + + /** + * Parse and instantiate an InjectionPoint from the supplied information. + * Returns null if an InjectionPoint could not be created. + * + * @param context The injection point context which owns this {@link At} + * annotation + * @param at Injection point specifier + * @param shift Shift type to apply + * @param by Amount of shift to apply for the BY shift type + * @param args Named parameters + * @param target Target for supported injection points + * @param slice Slice id for injectors which support multiple slices + * @param ordinal Ordinal offset for supported injection points + * @param opcode Bytecode opcode for supported injection points + * @param id Injection point id from annotation + * @return InjectionPoint parsed from the supplied data or null if parsing + * failed + */ + public static InjectionPoint parse(IInjectionPointContext context, String at, At.Shift shift, int by, + List args, String target, String slice, int ordinal, int opcode, String id) { + InjectionPointData data = new InjectionPointData(context, at, args, target, slice, ordinal, opcode, id); + Class ipClass = findClass(context.getContext(), data); + InjectionPoint point = InjectionPoint.create(context.getContext(), data, ipClass); + return InjectionPoint.shift(context, point, shift, by); } @SuppressWarnings("unchecked") @@ -678,7 +724,7 @@ public abstract class InjectionPoint { return point; } - private static InjectionPoint shift(IMixinContext context, MethodNode method, AnnotationNode parent, InjectionPoint point, + private static InjectionPoint shift(IInjectionPointContext context, InjectionPoint point, At.Shift shift, int by) { if (point != null) { @@ -687,7 +733,7 @@ public abstract class InjectionPoint { } else if (shift == At.Shift.AFTER) { return InjectionPoint.after(point); } else if (shift == At.Shift.BY) { - InjectionPoint.validateByValue(context, method, parent, point, by); + InjectionPoint.validateByValue(context.getContext(), context.getMethod(), context.getAnnotation(), point, by); return InjectionPoint.shift(point, by); } } diff --git a/src/main/java/org/spongepowered/asm/mixin/injection/modify/BeforeLoadLocal.java b/src/main/java/org/spongepowered/asm/mixin/injection/modify/BeforeLoadLocal.java index 5b31a5c2..1d2156ff 100644 --- a/src/main/java/org/spongepowered/asm/mixin/injection/modify/BeforeLoadLocal.java +++ b/src/main/java/org/spongepowered/asm/mixin/injection/modify/BeforeLoadLocal.java @@ -33,7 +33,9 @@ import org.objectweb.asm.tree.AbstractInsnNode; import org.objectweb.asm.tree.VarInsnNode; import org.spongepowered.asm.mixin.injection.InjectionPoint.AtCode; import org.spongepowered.asm.mixin.injection.ModifyVariable; -import org.spongepowered.asm.mixin.injection.modify.ModifyVariableInjector.ContextualInjectionPoint; +import org.spongepowered.asm.mixin.injection.modify.LocalVariableDiscriminator.Context; +import org.spongepowered.asm.mixin.injection.modify.ModifyVariableInjector.LocalVariableInjectionPoint; +import org.spongepowered.asm.mixin.injection.struct.InjectionInfo; import org.spongepowered.asm.mixin.injection.struct.InjectionPointData; import org.spongepowered.asm.mixin.injection.struct.Target; @@ -73,27 +75,24 @@ import org.spongepowered.asm.mixin.injection.struct.Target; * */ @AtCode("LOAD") -public class BeforeLoadLocal extends ContextualInjectionPoint { +public class BeforeLoadLocal extends LocalVariableInjectionPoint { /** * Keeps track of state within {@link #find} */ - static class SearchState { + class SearchState { + + private static final int INVALID_IMPLICIT = -2; /** * Print LVT search, be permissive */ private final boolean print; - - /** - * The target ordinal from the injection point - */ - private final int targetOrdinal; - + /** * The current ordinal */ - private int ordinal = 0; + private int currentOrdinal = 0; /** * Flag to defer a {@link check} to the next opcode, to honour the after @@ -111,9 +110,8 @@ public class BeforeLoadLocal extends ContextualInjectionPoint { */ private VarInsnNode varNode; - SearchState(int targetOrdinal, boolean print) { - this.targetOrdinal = targetOrdinal; - this.print = print; + SearchState() { + this.print = BeforeLoadLocal.this.discriminator.printLVT(); } boolean success() { @@ -132,18 +130,29 @@ public class BeforeLoadLocal extends ContextualInjectionPoint { this.varNode = node; } - void check(Collection nodes, AbstractInsnNode insn, int local) { + void check(Target target, Collection nodes, AbstractInsnNode insn) { + Context context = new Context(BeforeLoadLocal.this.returnType, BeforeLoadLocal.this.discriminator.isArgsOnly(), target, insn); + int local = SearchState.INVALID_IMPLICIT; + + try { + local = BeforeLoadLocal.this.discriminator.findLocal(context); + } catch (InvalidImplicitDiscriminatorException ex) { + BeforeLoadLocal.this.addMessage("%s has invalid IMPLICIT discriminator for opcode %d in %s: %s", + BeforeLoadLocal.this.toString(context), target.indexOf(insn), target, ex.getMessage()); + } + this.pendingCheck = false; - if (local != this.varNode.var && (local > -2 || !this.print)) { + if (local != this.varNode.var && (local > SearchState.INVALID_IMPLICIT || !this.print)) { + this.varNode = null; return; } - if (this.targetOrdinal == -1 || this.targetOrdinal == this.ordinal) { + if (BeforeLoadLocal.this.ordinal == -1 || BeforeLoadLocal.this.ordinal == this.currentOrdinal) { nodes.add(insn); this.found = true; } - this.ordinal++; + this.currentOrdinal++; this.varNode = null; } @@ -153,22 +162,22 @@ public class BeforeLoadLocal extends ContextualInjectionPoint { * Return type of the handler, also the type of the local variable we're * interested in */ - private final Type returnType; + protected final Type returnType; /** * Discriminator, parsed from parent annotation */ - private final LocalVariableDiscriminator discriminator; + protected final LocalVariableDiscriminator discriminator; /** * Target opcode, inflected from return type */ - private final int opcode; + protected final int opcode; /** * Target ordinal */ - private final int ordinal; + protected final int ordinal; /** * True if this injection point should capture the opcode after a matching @@ -179,10 +188,9 @@ public class BeforeLoadLocal extends ContextualInjectionPoint { protected BeforeLoadLocal(InjectionPointData data) { this(data, Opcodes.ILOAD, false); } - - protected BeforeLoadLocal(InjectionPointData data, - int opcode, boolean opcodeAfter) { - super(data.getContext()); + + protected BeforeLoadLocal(InjectionPointData data, int opcode, boolean opcodeAfter) { + super(data); this.returnType = data.getMethodReturnType(); this.discriminator = data.getLocalVariableDiscriminator(); this.opcode = data.getOpcode(this.returnType.getOpcode(opcode)); @@ -191,27 +199,40 @@ public class BeforeLoadLocal extends ContextualInjectionPoint { } @Override - boolean find(Target target, Collection nodes) { - SearchState state = new SearchState(this.ordinal, this.discriminator.printLVT()); + boolean find(InjectionInfo info, Target target, Collection nodes) { + SearchState state = new SearchState(); ListIterator iter = target.method.instructions.iterator(); while (iter.hasNext()) { AbstractInsnNode insn = iter.next(); if (state.isPendingCheck()) { - int local = this.discriminator.findLocal(this.returnType, this.discriminator.isArgsOnly(), target, insn); - state.check(nodes, insn, local); + state.check(target, nodes, insn); } else if (insn instanceof VarInsnNode && insn.getOpcode() == this.opcode && (this.ordinal == -1 || !state.success())) { state.register((VarInsnNode)insn); if (this.opcodeAfter) { state.setPendingCheck(); } else { - int local = this.discriminator.findLocal(this.returnType, this.discriminator.isArgsOnly(), target, insn); - state.check(nodes, insn, local); + state.check(target, nodes, insn); } } } return state.success(); } + + // No synthetic + @Override + protected void addMessage(String format, Object... args) { + super.addMessage(format, args); + } + + @Override + public String toString() { + return String.format("@At(\"%s\" %s)", this.getAtCode(), this.discriminator.toString()); + } + + public String toString(Context context) { + return String.format("@At(\"%s\" %s)", this.getAtCode(), this.discriminator.toString(context)); + } } diff --git a/src/main/java/org/spongepowered/asm/mixin/injection/modify/LocalVariableDiscriminator.java b/src/main/java/org/spongepowered/asm/mixin/injection/modify/LocalVariableDiscriminator.java index 499589a1..b5c5ad87 100644 --- a/src/main/java/org/spongepowered/asm/mixin/injection/modify/LocalVariableDiscriminator.java +++ b/src/main/java/org/spongepowered/asm/mixin/injection/modify/LocalVariableDiscriminator.java @@ -167,6 +167,16 @@ public class LocalVariableDiscriminator { } } } + + public int getCandidateCount() { + int candidateCount = 0; + for (int l = this.baseArgIndex; l < this.locals.length; l++) { + if (this.locals[l] != null && this.returnType.equals(this.locals[l].type)) { + candidateCount++; + } + } + return candidateCount; + } @Override public void print(PrettyPrinter printer) { @@ -272,6 +282,19 @@ public class LocalVariableDiscriminator { public boolean printLVT() { return this.print; } + + @Override + public String toString() { + return String.format("ordinal=%d index=%d", this.ordinal, this.index); + } + + public String toString(Context context) { + String typeName = SignaturePrinter.getTypeName(context.returnType, false, false); + if (this.isImplicit(context)) { + return "implicit " + typeName; + } + return String.format("explicit %s at ordinal=%d index=%d", typeName, this.ordinal, this.index); + } /** * If the user specifies no values for ordinal, index or @@ -286,23 +309,6 @@ public class LocalVariableDiscriminator { return this.ordinal < 0 && this.index < context.baseArgIndex && this.names.isEmpty(); } - /** - * Find a matching local variable in the specified target - * - * @param returnType variable tyoe - * @param argsOnly only match in the method args - * @param target target method - * @param node current instruction - * @return index of local or -1 if not matched - */ - public int findLocal(Type returnType, boolean argsOnly, Target target, AbstractInsnNode node) { - try { - return this.findLocal(new Context(returnType, argsOnly, target, node)); - } catch (InvalidImplicitDiscriminatorException ex) { - return -2; - } - } - /** * Find a local variable for the specified context * 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 133a5a84..7e2916af 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 @@ -34,13 +34,14 @@ import org.objectweb.asm.tree.InsnList; import org.objectweb.asm.tree.MethodNode; import org.objectweb.asm.tree.VarInsnNode; import org.spongepowered.asm.mixin.injection.InjectionPoint; -import org.spongepowered.asm.mixin.injection.ModifyVariable; import org.spongepowered.asm.mixin.injection.InjectionPoint.RestrictTargetLevel; +import org.spongepowered.asm.mixin.injection.ModifyVariable; import org.spongepowered.asm.mixin.injection.code.Injector; import org.spongepowered.asm.mixin.injection.struct.InjectionInfo; +import org.spongepowered.asm.mixin.injection.struct.InjectionPointData; +import org.spongepowered.asm.mixin.injection.struct.InjectionNodes.InjectionNode; import org.spongepowered.asm.mixin.injection.struct.Target; import org.spongepowered.asm.mixin.injection.struct.Target.Extension; -import org.spongepowered.asm.mixin.injection.struct.InjectionNodes.InjectionNode; import org.spongepowered.asm.mixin.injection.throwables.InvalidInjectionException; import org.spongepowered.asm.mixin.refmap.IMixinContext; import org.spongepowered.asm.util.Bytecode; @@ -73,21 +74,22 @@ public class ModifyVariableInjector extends Injector { /** * Specialised injection point which uses a target-aware search pattern */ - abstract static class ContextualInjectionPoint extends InjectionPoint { + abstract static class LocalVariableInjectionPoint extends InjectionPoint { - protected final IMixinContext context; - - ContextualInjectionPoint(IMixinContext context) { - this.context = context; + protected final IMixinContext mixin; + + LocalVariableInjectionPoint(InjectionPointData data) { + super(data); + this.mixin = data.getContext(); } @Override public boolean find(String desc, InsnList insns, Collection nodes) { - throw new InvalidInjectionException(this.context, this.getAtCode() + " injection point must be used in conjunction with @ModifyVariable"); + throw new InvalidInjectionException(this.mixin, this.getAtCode() + " injection point must be used in conjunction with @ModifyVariable"); } - - abstract boolean find(Target target, Collection nodes); + abstract boolean find(InjectionInfo info, Target target, Collection nodes); + } /** @@ -106,9 +108,9 @@ public class ModifyVariableInjector extends Injector { @Override protected boolean findTargetNodes(MethodNode into, InjectionPoint injectionPoint, InsnList insns, Collection nodes) { - if (injectionPoint instanceof ContextualInjectionPoint) { + if (injectionPoint instanceof LocalVariableInjectionPoint) { Target target = this.info.getContext().getTargetMethod(into); - return ((ContextualInjectionPoint)injectionPoint).find(target, nodes); + return ((LocalVariableInjectionPoint)injectionPoint).find(this.info, target, nodes); } return injectionPoint.find(into.desc, insns, nodes); } @@ -178,6 +180,11 @@ public class ModifyVariableInjector extends Injector { SignaturePrinter handlerSig = new SignaturePrinter(this.info.getMethodName(), this.returnType, this.methodArgs, new String[] { "var" }); handlerSig.setModifiers(this.methodNode); + String matchMode = "EXPLICIT (match by criteria)"; + if (this.discriminator.isImplicit(context)) { + int candidateCount = context.getCandidateCount(); + matchMode = "IMPLICIT (match single) - " + (candidateCount == 1 ? "VALID (exactly 1 match)" : "INVALID (" + candidateCount + " matches)"); + } new PrettyPrinter() .kvWidth(20) .kv("Target Class", this.classNode.name.replace('/', '.')) @@ -186,7 +193,7 @@ public class ModifyVariableInjector extends Injector { .kv("Capture Type", SignaturePrinter.getTypeName(this.returnType, false)) .kv("Instruction", "[%d] %s %s", target.insns.indexOf(context.node), context.node.getClass().getSimpleName(), Bytecode.getOpcodeName(context.node.getOpcode())).hr() - .kv("Match mode", this.discriminator.isImplicit(context) ? "IMPLICIT (match single)" : "EXPLICIT (match by criteria)") + .kv("Match mode", matchMode) .kv("Match ordinal", this.discriminator.getOrdinal() < 0 ? "any" : this.discriminator.getOrdinal()) .kv("Match index", this.discriminator.getIndex() < context.baseArgIndex ? "any" : this.discriminator.getIndex()) .kv("Match name(s)", this.discriminator.hasNames() ? this.discriminator.getNames() : "any") 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 3f0da671..7fea5525 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 @@ -70,6 +70,7 @@ import org.spongepowered.asm.util.asm.ElementNode; import org.spongepowered.asm.util.asm.MethodNodeEx; import org.spongepowered.asm.util.logging.MessageRouter; +import com.google.common.base.Joiner; import com.google.common.base.Strings; import com.google.common.collect.ImmutableSet; @@ -176,7 +177,7 @@ public abstract class InjectionInfo extends SpecialMethodInfo implements ISliceC InjectionInfo.register(ModifyVariableInjectionInfo.class); // @ModifyVariable InjectionInfo.register(ModifyConstantInjectionInfo.class); // @ModifyConstant } - + /** * Annotated method is static */ @@ -248,6 +249,13 @@ public abstract class InjectionInfo extends SpecialMethodInfo implements ISliceC */ private int injectedCallbackCount = 0; + /** + * Injection messages which are not severe enough to log when they occur, + * but may be of interest if a require clause fails in order to + * diagnose why no injections were performed. + */ + private List messages; + /** * ctor * @@ -319,7 +327,7 @@ public abstract class InjectionInfo extends SpecialMethodInfo implements ISliceC } protected void parseInjectionPoints(List ats) { - this.injectionPoints.addAll(InjectionPoint.parse(this.mixin, this.method, this.annotation, ats)); + this.injectionPoints.addAll(InjectionPoint.parse(this, ats)); } protected void parseRequirements() { @@ -389,21 +397,21 @@ public abstract class InjectionInfo extends SpecialMethodInfo implements ISliceC String description = this.getDescription(); String refMapStatus = this.mixin.getReferenceMapper().getStatus(); - String dynamicInfo = this.getDynamicInfo(); - if ((this.mixin.getEnvironment().getOption(Option.DEBUG_INJECTORS) && this.injectedCallbackCount < this.expectedCallbackCount)) { + String extraInfo = this.getDynamicInfo() + this.getMessages(); + if ((this.mixin.getOption(Option.DEBUG_INJECTORS) && this.injectedCallbackCount < this.expectedCallbackCount)) { throw new InvalidInjectionException(this, String.format("Injection validation failed: %s %s%s in %s expected %d invocation(s) but %d succeeded. Scanned %d target(s). %s%s", description, this.methodName, this.method.desc, this.mixin, this.expectedCallbackCount, this.injectedCallbackCount, - this.targetCount, refMapStatus, dynamicInfo)); + this.targetCount, refMapStatus, extraInfo)); } else if (this.injectedCallbackCount < this.requiredCallbackCount) { throw new InjectionError( String.format("Critical injection failure: %s %s%s in %s failed injection check, (%d/%d) succeeded. Scanned %d target(s). %s%s", description, this.methodName, this.method.desc, this.mixin, this.injectedCallbackCount, this.requiredCallbackCount, - this.targetCount, refMapStatus, dynamicInfo)); + this.targetCount, refMapStatus, extraInfo)); } else if (this.injectedCallbackCount > this.maxCallbackCount) { throw new InjectionError( String.format("Critical injection failure: %s %s%s in %s failed injection check, %d succeeded of %d allowed.%s", - description, this.methodName, this.method.desc, this.mixin, this.injectedCallbackCount, this.maxCallbackCount, dynamicInfo)); + description, this.methodName, this.method.desc, this.mixin, this.injectedCallbackCount, this.maxCallbackCount, extraInfo)); } } @@ -417,7 +425,6 @@ public abstract class InjectionInfo extends SpecialMethodInfo implements ISliceC // this.targets.remove(target.method); } - protected String getDescription() { return "Callback method"; } @@ -489,6 +496,28 @@ public abstract class InjectionInfo extends SpecialMethodInfo implements ISliceC this.injectedCallbackCount++; } + /** + * Notify method, called by injector or injection point when a notable but + * non-fatal failures occur, for example allows injection points to add + * notes when they return no results. + * + * @param format Message format + * @param args Format args + */ + @Override + public void addMessage(String format, Object... args) { + super.addMessage(format, args); + if (this.messages == null) { + this.messages = new ArrayList(); + } + String message = String.format(format, args); + this.messages.add(message); + } + + protected String getMessages() { + return this.messages != null ? " Messages: { " + Joiner.on(" ").join(this.messages) + "}" : ""; + } + /** * Finds methods in the target class which match searchFor * @@ -501,7 +530,7 @@ public abstract class InjectionInfo extends SpecialMethodInfo implements ISliceC // environment. In certain circumstances including the descriptor for // the method may actually fail, so we will do a second pass without the // descriptor if this happens. - int passes = this.mixin.getEnvironment().getOption(Option.REFMAP_REMAP) ? 2 : 1; + int passes = this.mixin.getOption(Option.REFMAP_REMAP) ? 2 : 1; for (ITargetSelector selector : selectors) { int matchCount = selector.getMatchCount(); @@ -533,7 +562,7 @@ public abstract class InjectionInfo extends SpecialMethodInfo implements ISliceC return; } - if ((this.mixin.getEnvironment().getOption(Option.DEBUG_INJECTORS) && this.expectedCallbackCount > 0)) { + if ((this.mixin.getOption(Option.DEBUG_INJECTORS) && this.expectedCallbackCount > 0)) { throw new InvalidInjectionException(this, String.format("Injection validation failed: %s annotation on %s could not find any targets matching %s in %s. %s%s", this.annotationType, this.methodName, InjectionInfo.namesOf(selectors), this.mixin.getTarget(), @@ -571,7 +600,7 @@ public abstract class InjectionInfo extends SpecialMethodInfo implements ISliceC if (upstream != null) { description = String.format("{%s} %s", upstream.getClassName(), description).trim(); } - return description.length() > 0 ? String.format(" Method is @Dynamic(%s)", description) : ""; + return description.length() > 0 ? String.format(" Method is @Dynamic(%s).", description) : ""; } /** diff --git a/src/main/java/org/spongepowered/asm/mixin/injection/struct/InjectionPointData.java b/src/main/java/org/spongepowered/asm/mixin/injection/struct/InjectionPointData.java index 3cb6bba1..f970537e 100644 --- a/src/main/java/org/spongepowered/asm/mixin/injection/struct/InjectionPointData.java +++ b/src/main/java/org/spongepowered/asm/mixin/injection/struct/InjectionPointData.java @@ -34,6 +34,7 @@ import org.objectweb.asm.Type; import org.objectweb.asm.tree.AnnotationNode; import org.objectweb.asm.tree.MethodNode; import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.IInjectionPointContext; import org.spongepowered.asm.mixin.injection.Inject; import org.spongepowered.asm.mixin.injection.InjectionPoint.Selector; import org.spongepowered.asm.mixin.injection.modify.LocalVariableDiscriminator; @@ -41,6 +42,7 @@ import org.spongepowered.asm.mixin.injection.selectors.ITargetSelector; import org.spongepowered.asm.mixin.injection.selectors.TargetSelector; import org.spongepowered.asm.mixin.injection.throwables.InvalidInjectionPointException; import org.spongepowered.asm.mixin.refmap.IMixinContext; +import org.spongepowered.asm.util.IMessageSink; import com.google.common.base.Joiner; import com.google.common.base.Strings; @@ -62,19 +64,9 @@ public class InjectionPointData { private final Map args = new HashMap(); /** - * Mixin + * Injection point context (annotated method) */ - private final IMixinContext context; - - /** - * Injector callback - */ - private final MethodNode method; - - /** - * Parent annotation - */ - private final AnnotationNode parent; + private final IInjectionPointContext context; /** * At arg @@ -116,11 +108,9 @@ public class InjectionPointData { */ private final String id; - public InjectionPointData(IMixinContext context, MethodNode method, AnnotationNode parent, String at, List args, String target, + public InjectionPointData(IInjectionPointContext context, String at, List args, String target, String slice, int ordinal, int opcode, String id) { this.context = context; - this.method = method; - this.parent = parent; this.at = at; this.target = target; this.slice = Strings.nullToEmpty(slice); @@ -154,6 +144,13 @@ public class InjectionPointData { } } } + + /** + * Get the message sink for this injection point + */ + public IMessageSink getMessageSink() { + return this.context; + } /** * Get the at value on the injector @@ -180,28 +177,28 @@ public class InjectionPointData { * Get the context */ public IMixinContext getContext() { - return this.context; + return this.context.getContext(); } /** * Get the annotated method */ public MethodNode getMethod() { - return this.method; + return this.context.getMethod(); } /** * Get the return type of the annotated method */ public Type getMethodReturnType() { - return Type.getReturnType(this.method.desc); + return Type.getReturnType(this.getMethod().desc); } /** * Get the root annotation (eg. {@link Inject}) */ public AnnotationNode getParent() { - return this.parent; + return this.context.getAnnotation(); } /** @@ -212,7 +209,7 @@ public class InjectionPointData { } public LocalVariableDiscriminator getLocalVariableDiscriminator() { - return LocalVariableDiscriminator.parse(this.parent); + return LocalVariableDiscriminator.parse(this.getParent()); } /** @@ -261,9 +258,9 @@ public class InjectionPointData { */ public ITargetSelector get(String key) { try { - return TargetSelector.parseAndValidate(this.get(key, ""), this.context); + return TargetSelector.parseAndValidate(this.get(key, ""), this.getContext()); } catch (InvalidMemberDescriptorException ex) { - throw new InvalidInjectionPointException(this.context, "Failed parsing @At(\"%s\").%s descriptor \"%s\" on %s", + throw new InvalidInjectionPointException(this.getContext(), "Failed parsing @At(\"%s\").%s descriptor \"%s\" on %s", this.at, key, this.target, this.getDescription()); } } @@ -273,9 +270,9 @@ public class InjectionPointData { */ public ITargetSelector getTarget() { try { - return TargetSelector.parseAndValidate(this.target, this.context); + return TargetSelector.parseAndValidate(this.target, this.getContext()); } catch (InvalidMemberDescriptorException ex) { - throw new InvalidInjectionPointException(this.context, "Failed parsing @At(\"%s\").target descriptor \"%s\" on %s", + throw new InvalidInjectionPointException(this.getContext(), "Failed parsing @At(\"%s\").target descriptor \"%s\" on %s", this.at, this.target, this.getDescription()); } } @@ -284,7 +281,7 @@ public class InjectionPointData { * Get a description of this injector for use in error messages */ public String getDescription() { - return InjectionInfo.describeInjector(this.context, this.parent, this.method); + return InjectionInfo.describeInjector(this.getContext(), this.getParent(), this.getMethod()); } /** diff --git a/src/main/java/org/spongepowered/asm/mixin/struct/AnnotatedMethodInfo.java b/src/main/java/org/spongepowered/asm/mixin/struct/AnnotatedMethodInfo.java new file mode 100644 index 00000000..3fac92c6 --- /dev/null +++ b/src/main/java/org/spongepowered/asm/mixin/struct/AnnotatedMethodInfo.java @@ -0,0 +1,107 @@ +/* + * 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.struct; + +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import org.objectweb.asm.tree.AnnotationNode; +import org.objectweb.asm.tree.MethodNode; +import org.spongepowered.asm.mixin.MixinEnvironment.Option; +import org.spongepowered.asm.mixin.injection.IInjectionPointContext; +import org.spongepowered.asm.mixin.refmap.IMixinContext; + +/** + * Data bundle for an annotated method in a mixin + */ +public class AnnotatedMethodInfo implements IInjectionPointContext { + + /** + * Log more things + */ + protected static final Logger logger = LogManager.getLogger("mixin"); + + /** + * Mixin context + */ + private final IMixinContext context; + + /** + * Annotated method + */ + protected final MethodNode method; + + /** + * Annotation on the method + */ + protected final AnnotationNode annotation; + + public AnnotatedMethodInfo(IMixinContext mixin, MethodNode method, AnnotationNode annotation) { + this.context = mixin; + this.method = method; + this.annotation = annotation; + } + + /** + * Get the mixin target context for this annotated method + * + * @return the target context + */ + @Override + public final IMixinContext getContext() { + return this.context; + } + + /** + * Get method being called + * + * @return injector method + */ + @Override + public final MethodNode getMethod() { + return this.method; + } + + /** + * Get the annotation which this InjectionInfo was created from + * + * @return The annotation which this InjectionInfo was created from + */ + @Override + public final AnnotationNode getAnnotation() { + return this.annotation; + } + + /* (non-Javadoc) + * @see org.spongepowered.asm.mixin.injection.IInjectionPointContext + * #addMessage(java.lang.String, java.lang.Object[]) + */ + @Override + public void addMessage(String format, Object... args) { + if (this.context.getOption(Option.DEBUG_VERBOSE)) { + AnnotatedMethodInfo.logger.warn(String.format(format, args)); + } + } + +} diff --git a/src/main/java/org/spongepowered/asm/mixin/struct/SpecialMethodInfo.java b/src/main/java/org/spongepowered/asm/mixin/struct/SpecialMethodInfo.java index 0ceea9bb..09344f69 100644 --- a/src/main/java/org/spongepowered/asm/mixin/struct/SpecialMethodInfo.java +++ b/src/main/java/org/spongepowered/asm/mixin/struct/SpecialMethodInfo.java @@ -27,8 +27,6 @@ package org.spongepowered.asm.mixin.struct; import org.objectweb.asm.tree.AnnotationNode; import org.objectweb.asm.tree.ClassNode; import org.objectweb.asm.tree.MethodNode; -import org.spongepowered.asm.mixin.injection.IInjectionPointContext; -import org.spongepowered.asm.mixin.refmap.IMixinContext; import org.spongepowered.asm.mixin.transformer.ClassInfo; import org.spongepowered.asm.mixin.transformer.MixinTargetContext; import org.spongepowered.asm.util.Bytecode; @@ -37,12 +35,7 @@ import org.spongepowered.asm.util.asm.MethodNodeEx; /** * Information about a special mixin method such as an injector or accessor */ -public abstract class SpecialMethodInfo implements IInjectionPointContext { - - /** - * Annotation on the method - */ - protected final AnnotationNode annotation; +public class SpecialMethodInfo extends AnnotatedMethodInfo { /** * Human-readable annotation type @@ -54,11 +47,6 @@ public abstract class SpecialMethodInfo implements IInjectionPointContext { */ protected final ClassNode classNode; - /** - * Annotated method - */ - protected final MethodNode method; - /** * Original name of the method, if available */ @@ -70,33 +58,12 @@ public abstract class SpecialMethodInfo implements IInjectionPointContext { protected final MixinTargetContext mixin; public SpecialMethodInfo(MixinTargetContext mixin, MethodNode method, AnnotationNode annotation) { + super(mixin, method, annotation); this.mixin = mixin; - this.method = method; - this.annotation = annotation; this.annotationType = this.annotation != null ? "@" + Bytecode.getSimpleName(this.annotation) : "Undecorated injector"; this.classNode = mixin.getTargetClassNode(); this.methodName = MethodNodeEx.getName(method); } - - /** - * Get the mixin target context for this injection - * - * @return the target context - */ - @Override - public final IMixinContext getContext() { - return this.mixin; - } - - /** - * Get the annotation which this InjectionInfo was created from - * - * @return The annotation which this InjectionInfo was created from - */ - @Override - public final AnnotationNode getAnnotation() { - return this.annotation; - } /** * Get the class node for this injection @@ -113,16 +80,6 @@ public abstract class SpecialMethodInfo implements IInjectionPointContext { public final ClassInfo getClassInfo() { return this.mixin.getClassInfo(); } - - /** - * Get method being called - * - * @return injector method - */ - @Override - public final MethodNode getMethod() { - return this.method; - } /** * Get the original name of the method, if available diff --git a/src/main/java/org/spongepowered/asm/util/IMessageSink.java b/src/main/java/org/spongepowered/asm/util/IMessageSink.java new file mode 100644 index 00000000..0a5d5604 --- /dev/null +++ b/src/main/java/org/spongepowered/asm/util/IMessageSink.java @@ -0,0 +1,40 @@ +/* + * 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.util; + +/** + * Interface for generic message consumers + */ +public interface IMessageSink { + + /** + * Add a new message + * + * @param format Message format + * @param args Message args + */ + public abstract void addMessage(String format, Object... args); + +}