diff --git a/src/main/java/org/spongepowered/asm/mixin/injection/At.java b/src/main/java/org/spongepowered/asm/mixin/injection/At.java index 611f4249..b637c813 100644 --- a/src/main/java/org/spongepowered/asm/mixin/injection/At.java +++ b/src/main/java/org/spongepowered/asm/mixin/injection/At.java @@ -27,6 +27,7 @@ package org.spongepowered.asm.mixin.injection; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; import org.spongepowered.asm.mixin.injection.points.*; /** @@ -73,6 +74,19 @@ public @interface At { } + /** + * The identifier for this injection point, can be retrieved via the + * {@link CallbackInfo#getId} accessor. If specified, the ID is appended to + * the value specified in the outer annotion. Eg. specifying "foo" for this + * attribute and "bar" for the Inject.{@link Inject#id} attribute + * will result in a combined id of "bar:foo". Note that if no id + * is specified for the outer injector, the name of the calling method is + * used. + * + * @return the injection point id to use + */ + public String id() default ""; + /** *

Type of {@link InjectionPoint} to use. Can be a built-in class or the * fully-qualified name of a custom class which extends diff --git a/src/main/java/org/spongepowered/asm/mixin/injection/InjectionNodes.java b/src/main/java/org/spongepowered/asm/mixin/injection/InjectionNodes.java index c674b068..755352fa 100644 --- a/src/main/java/org/spongepowered/asm/mixin/injection/InjectionNodes.java +++ b/src/main/java/org/spongepowered/asm/mixin/injection/InjectionNodes.java @@ -53,6 +53,16 @@ public class InjectionNodes extends ArrayList { */ public static class InjectionNode implements Comparable { + /** + * Next unique id + */ + private static int nextId = 0; + + /** + * Injection node unique id + */ + private final int id; + /** * The original node targetted */ @@ -77,6 +87,14 @@ public class InjectionNodes extends ArrayList { */ public InjectionNode(AbstractInsnNode node) { this.currentTarget = this.originalTarget = node; + this.id = InjectionNode.nextId++; + } + + /** + * Get the unique id for this injector + */ + public int getId() { + return this.id; } /** 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 034edbe6..a2fb677f 100644 --- a/src/main/java/org/spongepowered/asm/mixin/injection/InjectionPoint.java +++ b/src/main/java/org/spongepowered/asm/mixin/injection/InjectionPoint.java @@ -150,18 +150,20 @@ public abstract class InjectionPoint { private final String slice; private final Selector selector; + private final String id; protected InjectionPoint() { - this("", Selector.DEFAULT); + this("", Selector.DEFAULT, null); } protected InjectionPoint(InjectionPointData data) { - this(data.getSlice(), data.getSelector()); + this(data.getSlice(), data.getSelector(), data.getId()); } - public InjectionPoint(String slice, Selector selector) { + public InjectionPoint(String slice, Selector selector, String id) { this.slice = slice; this.selector = selector; + this.id = id; } public String getSlice() { @@ -171,6 +173,10 @@ public abstract class InjectionPoint { public Selector getSelector() { return this.selector; } + + public String getId() { + return this.id; + } /** * Find injection points in the supplied insn list @@ -452,7 +458,7 @@ public abstract class InjectionPoint { */ public static InjectionPoint parse(IInjectionPointContext owner, At at) { return InjectionPoint.parse(owner.getContext(), owner.getMethod(), owner.getAnnotation(), at.value(), at.shift(), at.by(), - Arrays.asList(at.args()), at.target(), at.slice(), at.ordinal(), at.opcode()); + Arrays.asList(at.args()), at.target(), at.slice(), at.ordinal(), at.opcode(), at.id()); } /** @@ -468,7 +474,7 @@ public abstract class InjectionPoint { */ 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.ordinal(), at.opcode(), at.id()); } /** @@ -506,12 +512,13 @@ public abstract class InjectionPoint { 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"); if (args == null) { args = ImmutableList.of(); } - return InjectionPoint.parse(context, method, parent, at, shift, by, args, target, slice, ordinal, opcode); + return InjectionPoint.parse(context, method, parent, at, shift, by, args, target, slice, ordinal, opcode, id); } /** @@ -530,12 +537,13 @@ public abstract class InjectionPoint { * @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(IMixinContext context, MethodNode method, AnnotationNode parent, String at, At.Shift shift, int by, - List args, String target, String slice, int ordinal, int opcode) { - InjectionPointData data = new InjectionPointData(context, method, parent, at, args, target, slice, ordinal, opcode); + 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(point, shift, by); 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 8e20dd1e..2eb73bd2 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 @@ -25,7 +25,10 @@ package org.spongepowered.asm.mixin.injection.callback; import java.util.ArrayList; +import java.util.HashMap; import java.util.List; +import java.util.Map; +import java.util.Set; import org.spongepowered.asm.lib.Opcodes; import org.spongepowered.asm.lib.Type; @@ -72,7 +75,7 @@ public class CallbackInjector extends Injector { /** * Target node, callback injected before this node */ - final AbstractInsnNode node; + final InjectionNode node; /** * Calculated local variables @@ -158,7 +161,7 @@ public class CallbackInjector extends Injector { Callback(MethodNode handler, Target target, final InjectionNode node, final LocalVariableNode[] locals, boolean captureLocals) { this.handler = handler; this.target = target; - this.node = node.getCurrentTarget(); + this.node = node; this.locals = locals; this.localTypes = locals != null ? new Type[locals.length] : null; this.frameSize = Bytecode.getFirstNonArgLocalIndex(target.arguments, !CallbackInjector.this.isStatic()); @@ -184,7 +187,7 @@ public class CallbackInjector extends Injector { this.extraArgs = Math.max(0, Bytecode.getFirstNonArgLocalIndex(this.handler) - (this.frameSize + 1)); this.argNames = argNames != null ? argNames.toArray(new String[argNames.size()]) : null; this.canCaptureLocals = captureLocals && locals != null && locals.length > this.frameSize; - this.isAtReturn = this.node instanceof InsnNode && this.isValueReturnOpcode(this.node.getOpcode()); + this.isAtReturn = this.node.getCurrentTarget() instanceof InsnNode && this.isValueReturnOpcode(this.node.getCurrentTarget().getOpcode()); this.desc = target.getCallbackDescriptor(this.localTypes, target.arguments); this.descl = target.getCallbackDescriptor(true, this.localTypes, target.arguments, this.frameSize, this.extraArgs); // this.typeCasts = new Type[this.frameSize + this.extraArgs]; @@ -231,7 +234,7 @@ public class CallbackInjector extends Injector { * for the method based on our calculated values */ void inject() { - this.target.insns.insertBefore(this.node, this); + this.target.insertBefore(this.node, this); this.target.addToStack(Math.max(this.invoke, this.ctor)); } @@ -298,6 +301,11 @@ public class CallbackInjector extends Injector { */ private final String identifier; + /** + * Injection point ids + */ + private final Map ids = new HashMap(); + /** * Make a new CallbackInjector with the supplied args * @@ -335,6 +343,34 @@ public class CallbackInjector extends Injector { } } } + + /* (non-Javadoc) + * @see org.spongepowered.asm.mixin.injection.code.Injector#addTargetNode( + * org.spongepowered.asm.mixin.injection.struct.Target, java.util.List, + * org.spongepowered.asm.lib.tree.AbstractInsnNode, java.util.Set) + */ + @Override + protected void addTargetNode(Target target, List myNodes, AbstractInsnNode node, Set nominators) { + InjectionNode injectionNode = target.injectionNodes.add(node); + + for (InjectionPoint ip : nominators) { + String id = ip.getId(); + if (Strings.isNullOrEmpty(id)) { + continue; + } + + String existingId = this.ids.get(Integer.valueOf(injectionNode.getId())); + if (existingId != null && !existingId.equals(id)) { + Injector.logger.warn("Conflicting id for {} insn in {}, found id {} on {}, previously defined as {}", Bytecode.getOpcodeName(node), + target.toString(), id, this.info, existingId); + break; + } + + this.ids.put(Integer.valueOf(injectionNode.getId()), id); + } + + myNodes.add(injectionNode); + } /* (non-Javadoc) * @see org.spongepowered.asm.mixin.injection.callback.BytecodeInjector @@ -442,7 +478,7 @@ public class CallbackInjector extends Injector { * @return generated message */ private String generateBadLVTMessage(final Callback callback) { - int position = callback.target.method.instructions.indexOf(callback.node); + int position = callback.target.indexOf(callback.node); List expected = CallbackInjector.summariseLocals(this.methodNode.desc, callback.target.arguments.length + 1); List found = CallbackInjector.summariseLocals(callback.getDescriptorWithAllLocals(), callback.frameSize); String message = String.format("LVT in %s has incompatible changes at opcode %d in callback %s.\nExpected: %s\n Found: %s", @@ -488,7 +524,8 @@ public class CallbackInjector extends Injector { printer.kv("Target Max LOCALS", callback.target.getMaxLocals()); printer.kv("Initial Frame Size", callback.frameSize); printer.kv("Callback Name", this.methodNode.name); - printer.kv("Instruction", "%s %s", callback.node.getClass().getSimpleName(), Bytecode.getOpcodeName(callback.node.getOpcode())); + printer.kv("Instruction", "%s %s", callback.node.getClass().getSimpleName(), + Bytecode.getOpcodeName(callback.node.getCurrentTarget().getOpcode())); printer.hr(); if (callback.locals.length > callback.frameSize) { printer.add(" %s %20s %s", "LOCAL", "TYPE", "NAME"); @@ -605,7 +642,9 @@ public class CallbackInjector extends Injector { * @return Identifier to use */ private String getIdentifier(Callback callback) { - return Strings.isNullOrEmpty(this.identifier) ? callback.target.method.name : this.identifier; + String baseId = Strings.isNullOrEmpty(this.identifier) ? callback.target.method.name : this.identifier; + String locationId = this.ids.get(Integer.valueOf(callback.node.getId())); + return baseId + (Strings.isNullOrEmpty(locationId) ? "" : ":" + locationId); } /** 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 b307e1df..6dd1b8be 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 @@ -26,6 +26,7 @@ package org.spongepowered.asm.mixin.injection.code; import java.util.ArrayList; import java.util.Collection; +import java.util.Collections; import java.util.HashMap; import java.util.HashSet; import java.util.List; @@ -61,7 +62,7 @@ public abstract class Injector { /** * A nominated target node */ - class TargetNode { + public static final class TargetNode { final AbstractInsnNode node; @@ -70,6 +71,29 @@ public abstract class Injector { TargetNode(AbstractInsnNode node) { this.node = node; } + + public AbstractInsnNode getNode() { + return this.node; + } + + public Set getNominators() { + return Collections.unmodifiableSet(this.nominators); + } + + @Override + public boolean equals(Object obj) { + if (obj == null || obj.getClass() != TargetNode.class) { + return false; + } + + return ((TargetNode)obj).node == this.node; + } + + @Override + public int hashCode() { + return this.node.hashCode(); + } + } /** 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 97fef302..2a3f2989 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 @@ -109,8 +109,13 @@ public class InjectionPointData { */ private final int opcode; + /** + * Injection point id from annotation + */ + private final String id; + public InjectionPointData(IMixinContext context, MethodNode method, AnnotationNode parent, String at, List args, String target, - String slice, int ordinal, int opcode) { + String slice, int ordinal, int opcode, String id) { this.context = context; this.method = method; this.parent = parent; @@ -119,6 +124,7 @@ public class InjectionPointData { this.slice = Strings.nullToEmpty(slice); this.ordinal = Math.max(-1, ordinal); this.opcode = opcode; + this.id = id; this.parseArgs(args); @@ -315,6 +321,13 @@ public class InjectionPointData { return defaultOpcode; } + /** + * Get the id specified on the injection point (or null if not specified) + */ + public String getId() { + return this.id; + } + @Override public String toString() { return this.type; 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 c60b57b4..4c90be05 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 @@ -35,6 +35,7 @@ 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.InjectionNodes.InjectionNode; import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; import org.spongepowered.asm.mixin.transformer.ClassInfo; import org.spongepowered.asm.util.Bytecode; @@ -319,6 +320,16 @@ public class Target implements Comparable, Iterable { } return this.toString().compareTo(o.toString()); } + + /** + * Return the index of the specified instruction in this instruction list + * + * @param node instruction to locate, must exist in the target + * @return opcode index + */ + public int indexOf(InjectionNode node) { + return this.insns.indexOf(node.getCurrentTarget()); + } /** * Return the index of the specified instruction in this instruction list @@ -384,6 +395,26 @@ public class Target implements Comparable, Iterable { return Bytecode.findSuperInit(this.method, ClassInfo.forName(this.classNode.name).getSuperName()); } + /** + * Insert the supplied instructions before the specified instruction + * + * @param location Instruction to insert before + * @param insns Instructions to insert + */ + public void insertBefore(InjectionNode location, final InsnList insns) { + this.insns.insertBefore(location.getCurrentTarget(), insns); + } + + /** + * Insert the supplied instructions before the specified instruction + * + * @param location Instruction to insert before + * @param insns Instructions to insert + */ + public void insertBefore(AbstractInsnNode location, final InsnList insns) { + this.insns.insertBefore(location, insns); + } + /** * Replace an instruction in this target with the specified instruction and * mark the node as replaced for other injectors