Support IDs on injection points, for better CallbackInfo context

This commit is contained in:
Mumfrey committed 2017-07-18 18:02:03 +01:00
1 parent 217bf321ba
commit 54bad25ce6
7 files changed
+164 -17

No files matched your search

@@ -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 <tt>Inject.{@link Inject#id}</tt> attribute
* will result in a combined id of <tt>"bar:foo"</tt>. 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 "";
/**
* <p>Type of {@link InjectionPoint} to use. Can be a built-in class or the
* fully-qualified name of a custom class which extends
@@ -53,6 +53,16 @@ public class InjectionNodes extends ArrayList<InjectionNodes.InjectionNode> {
*/
public static class InjectionNode implements Comparable<InjectionNode> {
/**
* 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<InjectionNodes.InjectionNode> {
*/
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;
}
/**
@@ -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.<Integer>getValue(node, "by", Integer.valueOf(0));
int ordinal = Annotations.<Integer>getValue(node, "ordinal", Integer.valueOf(-1));
int opcode = Annotations.<Integer>getValue(node, "opcode", Integer.valueOf(0));
String id = Annotations.<String>getValue(node, "id");
if (args == null) {
args = ImmutableList.<String>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<String> args, String target, String slice, int ordinal, int opcode) {
InjectionPointData data = new InjectionPointData(context, method, parent, at, args, target, slice, ordinal, opcode);
List<String> 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<? extends InjectionPoint> ipClass = findClass(context, data);
InjectionPoint point = InjectionPoint.create(context, data, ipClass);
return InjectionPoint.shift(point, shift, by);
@@ -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 <b>before</b> 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<Integer, String> ids = new HashMap<Integer, String>();
/**
* 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<InjectionNode> myNodes, AbstractInsnNode node, Set<InjectionPoint> 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<String> expected = CallbackInjector.summariseLocals(this.methodNode.desc, callback.target.arguments.length + 1);
List<String> 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);
}
/**
@@ -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<InjectionPoint> getNominators() {
return Collections.<InjectionPoint>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();
}
}
/**
@@ -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<String> 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;
@@ -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<Target>, Iterable<AbstractInsnNode> {
}
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<Target>, Iterable<AbstractInsnNode> {
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