Revert "New: Allow injection point specifiers anywhere. (#129)"

This reverts commit e70693a79a.
This commit is contained in:
LlamaLad7 committed 2024-05-19 19:04:03 +01:00
1 parent 2f35debf10
commit 3a090bd0ca
8 files changed
+57 -115

No files matched your search

@@ -162,19 +162,15 @@ public abstract class InjectionPoint {
}
/**
* Additional specifier for injection points. <tt>Specifiers</tt> can be
* supplied in {@link At} annotations by including a colon (<tt>:</tt>)
* character followed by the specifier type (case-sensitive), eg:
* Selector type for slice delmiters, ignored for normal injection points.
* <tt>Selectors</tt> can be supplied in {@link At} annotations by including
* a colon (<tt>:</tt>) character followed by the selector type
* (case-sensitive), eg:
*
* <blockquote><pre>&#064;At(value = "INVOKE:LAST", ... )</pre></blockquote>
*/
public enum Specifier {
/**
* Use all instructions from the query result.
*/
ALL,
public enum Selector {
/**
* Use the <em>first</em> instruction from the query result.
*/
@@ -190,13 +186,13 @@ public abstract class InjectionPoint {
* more than one instruction this should be considered a fail-fast error
* state and a runtime exception will be thrown.
*/
ONE,
ONE;
/**
* Use the default setting as defined by the consumer. For slices this
* is {@link #FIRST}, for all other consumers this is {@link #ALL}
* Default selector type used if no selector is explicitly specified.
* <em>For internal use only. Currently {@link #FIRST}</em>
*/
DEFAULT;
public static final Selector DEFAULT = Selector.FIRST;
}
@@ -280,26 +276,26 @@ public abstract class InjectionPoint {
}
private final String slice;
private final Specifier specifier;
private final Selector selector;
private final String id;
private final IMessageSink messageSink;
protected InjectionPoint() {
this("", Specifier.DEFAULT, null);
this("", Selector.DEFAULT, null);
}
protected InjectionPoint(InjectionPointData data) {
this(data.getSlice(), data.getSpecifier(), data.getId(), data.getMessageSink());
this(data.getSlice(), data.getSelector(), data.getId(), data.getMessageSink());
}
public InjectionPoint(String slice, Specifier specifier, String id) {
this(slice, specifier, id, null);
public InjectionPoint(String slice, Selector selector, String id) {
this(slice, selector, id, null);
}
public InjectionPoint(String slice, Specifier specifier, String id, IMessageSink messageSink) {
public InjectionPoint(String slice, Selector selector, String id, IMessageSink messageSink) {
this.slice = slice;
this.specifier = specifier;
this.selector = selector;
this.id = id;
this.messageSink = messageSink;
}
@@ -308,8 +304,8 @@ public abstract class InjectionPoint {
return this.slice;
}
public Specifier getSpecifier(Specifier defaultSpecifier) {
return this.specifier == Specifier.DEFAULT ? defaultSpecifier : this.specifier;
public Selector getSelector() {
return this.selector;
}
public String getId() {
@@ -28,7 +28,7 @@ import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.spongepowered.asm.mixin.injection.InjectionPoint.Specifier;
import org.spongepowered.asm.mixin.injection.InjectionPoint.Selector;
/**
* A <tt>Slice</tt> identifies a section of a method to search for injection
@@ -162,10 +162,10 @@ public @interface Slice {
/**
* Injection point which specifies the <em>start</em> of the slice region.
* {@link At}s supplied here should generally use a {@link Specifier}
* {@link At}s supplied here should generally specify a {@link Selector}
* in order to identify which instruction should be used for queries which
* return multiple results. The specifier is supplied by appending the
* specifier type to the injection point type as follows:
* return multiple results. The selector is specified by appending the
* selector type to the injection point type as follows:
*
* <blockquote><pre>&#064;At(value = "INVOKE:LAST", ... )</pre></blockquote>
*
@@ -182,9 +182,9 @@ public @interface Slice {
/**
* Injection point which specifies the <em>end</em> of the slice region.
* Like {@link #from}, {@link At}s supplied here should generally specify a
* {@link Specifier} in order to identify which instruction should be used
* for queries which return multiple results. The specifier is supplied by
* appending the specifier type to the injection point type as follows:
* {@link Selector} in order to identify which instruction should be used
* for queries which return multiple results. The selector is specified by
* appending the selector type to the injection point type as follows:
*
* <blockquote><pre>&#064;At(value = "INVOKE:LAST", ... )</pre></blockquote>
*
@@ -42,7 +42,6 @@ import org.spongepowered.asm.mixin.MixinEnvironment.Option;
import org.spongepowered.asm.mixin.injection.Coerce;
import org.spongepowered.asm.mixin.injection.InjectionPoint;
import org.spongepowered.asm.mixin.injection.InjectionPoint.RestrictTargetLevel;
import org.spongepowered.asm.mixin.injection.InjectionPoint.Specifier;
import org.spongepowered.asm.mixin.injection.invoke.RedirectInjector;
import org.spongepowered.asm.mixin.injection.struct.InjectionInfo;
import org.spongepowered.asm.mixin.injection.struct.InjectionNodes.InjectionNode;
@@ -295,7 +294,7 @@ public abstract class Injector {
IMixinContext mixin = this.info.getMixin();
MethodNode method = injectorTarget.getMethod();
Map<Integer, TargetNode> targetNodes = new TreeMap<Integer, TargetNode>();
List<AbstractInsnNode> nodes = new ArrayList<AbstractInsnNode>(32);
Collection<AbstractInsnNode> nodes = new ArrayList<AbstractInsnNode>(32);
for (InjectionPoint injectionPoint : injectionPoints) {
nodes.clear();
@@ -308,20 +307,15 @@ public abstract class Injector {
injectorTarget, injectorTarget.getMergedBy(), injectorTarget.getMergedPriority()));
}
if (!this.findTargetNodes(method, injectionPoint, injectorTarget, nodes)) {
continue;
}
Specifier specifier = injectionPoint.getSpecifier(Specifier.ALL);
if (specifier == Specifier.ONE && nodes.size() != 1) {
throw new InvalidInjectionException(this.info, String.format("%s on %s has specifier :ONE but matched %d instructions",
injectionPoint, this, nodes.size()));
} else if (specifier != Specifier.ALL && nodes.size() > 1) {
AbstractInsnNode specified = nodes.get(specifier == Specifier.FIRST ? 0 : nodes.size() - 1);
this.addTargetNode(method, targetNodes, injectionPoint, specified);
} else {
if (this.findTargetNodes(method, injectionPoint, injectorTarget, nodes)) {
for (AbstractInsnNode insn : nodes) {
this.addTargetNode(method, targetNodes, injectionPoint, insn);
Integer key = method.instructions.indexOf(insn);
TargetNode targetNode = targetNodes.get(key);
if (targetNode == null) {
targetNode = new TargetNode(insn);
targetNodes.put(key, targetNode);
}
targetNode.nominators.add(injectionPoint);
}
}
}
@@ -329,16 +323,6 @@ public abstract class Injector {
return targetNodes.values();
}
protected void addTargetNode(MethodNode method, Map<Integer, TargetNode> targetNodes, InjectionPoint injectionPoint, AbstractInsnNode insn) {
Integer key = method.instructions.indexOf(insn);
TargetNode targetNode = targetNodes.get(key);
if (targetNode == null) {
targetNode = new TargetNode(insn);
targetNodes.put(key, targetNode);
}
targetNode.nominators.add(injectionPoint);
}
protected boolean findTargetNodes(MethodNode into, InjectionPoint injectionPoint, InjectorTarget injectorTarget,
Collection<AbstractInsnNode> nodes) {
return injectionPoint.find(into.desc, injectorTarget.getSlice(injectionPoint), nodes);
@@ -37,7 +37,7 @@ import org.objectweb.asm.tree.MethodNode;
import org.spongepowered.asm.mixin.MixinEnvironment.Option;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.InjectionPoint;
import org.spongepowered.asm.mixin.injection.InjectionPoint.Specifier;
import org.spongepowered.asm.mixin.injection.InjectionPoint.Selector;
import org.spongepowered.asm.mixin.injection.Slice;
import org.spongepowered.asm.mixin.injection.struct.InjectionPointAnnotationContext;
import org.spongepowered.asm.mixin.injection.throwables.InjectionError;
@@ -325,11 +325,6 @@ public final class MethodSlice {
* Descriptive name of the slice, used in exceptions
*/
private final String name;
/**
* Success counts for from and to injection points
*/
private int successCountFrom, successCountTo;
/**
* ctor
@@ -366,8 +361,8 @@ public final class MethodSlice {
*/
public InsnListReadOnly getSlice(MethodNode method) {
int max = method.instructions.size() - 1;
int start = this.find(method, this.from, 0, 0, "from");
int end = this.find(method, this.to, max, start, "to");
int start = this.find(method, this.from, 0, 0, this.name + "(from)");
int end = this.find(method, this.to, max, start, this.name + "(to)");
if (start > end) {
throw new InvalidSliceException(this.owner, String.format("%s is negative size. Range(%d -> %d)", this.describe(), start, end));
@@ -394,53 +389,32 @@ public final class MethodSlice {
* @param defaultValue Value to return if injection point is null (open
* ended)
* @param failValue Value to use if query fails
* @param argument The name of the argument ("from" or "to") for debug msgs
* @param description Description for error message
* @return matching insn index
*/
private int find(MethodNode method, InjectionPoint injectionPoint, int defaultValue, int failValue, String argument) {
private int find(MethodNode method, InjectionPoint injectionPoint, int defaultValue, int failValue, String description) {
if (injectionPoint == null) {
return defaultValue;
}
String description = String.format("%s(%s)", this.name, argument);
Deque<AbstractInsnNode> nodes = new LinkedList<AbstractInsnNode>();
InsnListReadOnly insns = new InsnListReadOnly(method.instructions);
boolean result = injectionPoint.find(method.desc, insns, nodes);
Specifier specifier = injectionPoint.getSpecifier(Specifier.FIRST);
if (specifier == Specifier.ALL) {
throw new InvalidSliceException(this.owner, String.format("ALL is not a valid specifier for slice %s", this.describe(description)));
}
if (nodes.size() != 1 && specifier == Specifier.ONE) {
Selector select = injectionPoint.getSelector();
if (nodes.size() != 1 && select == Selector.ONE) {
throw new InvalidSliceException(this.owner, String.format("%s requires 1 result but found %d", this.describe(description), nodes.size()));
}
if (!result) {
if (this.owner.getMixin().getOption(Option.DEBUG_VERBOSE)) {
MethodSlice.logger.warn("{} did not match any instructions", this.describe(description));
}
return failValue;
}
if ("from".equals(argument)) {
this.successCountFrom++;
} else {
this.successCountTo++;
}
return method.instructions.indexOf(specifier == Specifier.FIRST ? nodes.getFirst() : nodes.getLast());
return method.instructions.indexOf(select == Selector.FIRST ? nodes.getFirst() : nodes.getLast());
}
/**
* Perform post-injection debugging and validation tasks
*/
public void postInject() {
if (this.owner.getMixin().getOption(Option.DEBUG_VERBOSE)) {
if (this.from != null && this.successCountFrom == 0) {
MethodSlice.logger.warn("{} did not match any instructions", this.describe(this.name + "(from)"));
}
if (this.to != null && this.successCountTo == 0) {
MethodSlice.logger.warn("{} did not match any instructions", this.describe(this.name + "(to)"));
}
}
}
/* (non-Javadoc)
* @see java.lang.Object#toString()
*/
@@ -82,16 +82,6 @@ public final class MethodSlices {
return this.slices.get(id);
}
/**
* Called to do post-injection validation/debug logging for slices
*/
public void postInject() {
for (MethodSlice slice : this.slices.values()) {
slice.postInject();
}
}
/* (non-Javadoc)
* @see java.lang.Object#toString()
*/
@@ -147,7 +147,7 @@ public class BeforeConstant extends InjectionPoint {
private final boolean log;
public BeforeConstant(IMixinContext context, AnnotationNode node, String returnType) {
super(Annotations.<String>getValue(node, "slice", ""), Specifier.DEFAULT, null);
super(Annotations.<String>getValue(node, "slice", ""), Selector.DEFAULT, null);
Boolean empty = Annotations.<Boolean>getValue(node, "nullValue", (Boolean)null);
this.ordinal = Annotations.<Integer>getValue(node, "ordinal", Integer.valueOf(-1));
@@ -473,8 +473,6 @@ public abstract class InjectionInfo extends SpecialMethodInfo implements ISliceC
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, extraInfo));
}
this.slices.postInject();
}
/**
@@ -36,7 +36,7 @@ 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.Specifier;
import org.spongepowered.asm.mixin.injection.InjectionPoint.Selector;
import org.spongepowered.asm.mixin.injection.modify.LocalVariableDiscriminator;
import org.spongepowered.asm.mixin.injection.selectors.ITargetSelector;
import org.spongepowered.asm.mixin.injection.selectors.InvalidSelectorException;
@@ -86,7 +86,7 @@ public class InjectionPointData {
/**
* Selector parsed from the at argument, only used by slices
*/
private final Specifier specifier;
private final Selector selector;
/**
* Target
@@ -131,7 +131,7 @@ public class InjectionPointData {
Matcher matcher = InjectionPointData.AT_PATTERN.matcher(at);
this.type = InjectionPointData.parseType(matcher, at);
this.specifier = InjectionPointData.parseSpecifier(matcher);
this.selector = InjectionPointData.parseSelector(matcher);
}
private void parseArgs(List<String> args) {
@@ -172,10 +172,10 @@ public class InjectionPointData {
}
/**
* Get the specifier value parsed from the injector
* Get the selector value parsed from the injector
*/
public Specifier getSpecifier() {
return this.specifier;
public Selector getSelector() {
return this.selector;
}
/**
@@ -362,7 +362,7 @@ public class InjectionPointData {
}
private static Pattern createPattern() {
return Pattern.compile(String.format("^(.+?)(:(%s))?$", Joiner.on('|').join(Specifier.values())));
return Pattern.compile(String.format("^(.+?)(:(%s))?$", Joiner.on('|').join(Selector.values())));
}
/**
@@ -380,8 +380,8 @@ public class InjectionPointData {
return matcher.matches() ? matcher.group(1) : at;
}
private static Specifier parseSpecifier(Matcher matcher) {
return matcher.matches() && matcher.group(3) != null ? Specifier.valueOf(matcher.group(3)) : Specifier.DEFAULT;
private static Selector parseSelector(Matcher matcher) {
return matcher.matches() && matcher.group(3) != null ? Selector.valueOf(matcher.group(3)) : Selector.DEFAULT;
}
private static int parseInt(String string, int defaultValue) {