Support for modifying local variables in methods

This commit is contained in:
Mumfrey committed 2016-02-01 15:46:05 +00:00
1 parent 6ad5bc8f76
commit cd215795d2
16 files changed
+643 -27

No files matched your search

+1 -1
View File
@@ -4,5 +4,5 @@ packaging=jar
description=Mixin
url=https://www.spongepowered.org
organization=SpongePowered
buildVersion=0.4.17
buildVersion=0.4.18
buildType=SNAPSHOT
@@ -39,6 +39,7 @@ import javax.tools.Diagnostic.Kind;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.ModifyArg;
import org.spongepowered.asm.mixin.injection.ModifyVariable;
import org.spongepowered.asm.mixin.injection.Redirect;
import org.spongepowered.tools.MirrorUtils;
@@ -69,6 +70,7 @@ public class InjectionObfuscationProcessor extends MixinProcessor {
this.processInjectors(roundEnv, Inject.class);
this.processInjectors(roundEnv, ModifyArg.class);
this.processInjectors(roundEnv, Redirect.class);
this.processInjectors(roundEnv, ModifyVariable.class);
this.postProcess(roundEnv);
return true;
@@ -60,7 +60,7 @@ public abstract class MixinBootstrap {
/**
* Subsystem version
*/
public static final String VERSION = "0.4.17";
public static final String VERSION = "0.4.18";
/**
* Blackboard key where the subsystem version will be stored to indicate
@@ -147,9 +147,10 @@ public @interface Inject {
* specified is not achieved then an {@link InjectionError} is thrown at
* application time. Use this option with care.</p>
*
* @return Minimum required number of injected callbacks, default 0
* @return Minimum required number of injected callbacks, default specified
* by the containing config
*/
public int require() default 0;
public int require() default -1;
/**
* Like {@link #require()} but only enabled if the
@@ -106,9 +106,10 @@ public @interface ModifyArg {
* specified is not achieved then an {@link InjectionError} is thrown at
* application time. Use this option with care.</p>
*
* @return Minimum required number of injected callbacks, default 0
* @return Minimum required number of injected callbacks, default specified
* by the containing config
*/
public int require() default 0;
public int require() default -1;
/**
* Like {@link #require()} but only enabled if the
@@ -0,0 +1,187 @@
/*
* This file is part of Mixin, licensed under the MIT License (MIT).
*
* Copyright (c) SpongePowered <https://www.spongepowered.org>
* 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.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.spongepowered.asm.mixin.MixinEnvironment.Option;
/**
* Specifies that this mixin method should inject a variable modifier callback
* to itself in the target method(s) identified by {@link #method}.
*
* <p><tt>ModifyVariable</tt> callbacks should always take one argument of the
* type to capture and return the same type. For example a <tt>ModifyVariable
* </tt> for a local of type {@link String} should have the signature:</p>
*
* <code>private String methodName(String variable) { ...</code>
*
* <p>The callback receives the current value of the local variable, and should
* return the new value.</p>
*
* <p>The injector has two operating modes, <em>explicit</em> and <em>implicit
* </em>, and can operate either on the entire LVT or on the method arguments
* only</p>.
*
* <p>In <em>explicit</em> mode, the variable to capture can be specified by
* specifying values for the discriminator arguments {@link ordinal},
* {@link index} and {@link name}. The injector uses the discriminators in order
* to attempt to locate the variable to capture. If no local variable matches
* any discriminators, the capture fails.</p>
*
* <p>If no values for the capture discrimiators are specified, the injector
* operates in <em>implicit</em> mode. If exactly one variable of the capture
* type exists in the target LVT, then capture will succeed. However, if more
* than one variable of the required type is encountered in the LVT then an
* {@link InvalidInjectionException} is thrown.</p>
*/
@Target({ ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
public @interface ModifyVariable {
/**
* String representation of a
* {@link org.spongepowered.asm.mixin.injection.struct.MemberInfo
* MemberInfo} which identifies the target method.
*
* @return target method
*/
public String method();
/**
* An {@link At} annotation which describes the {@link InjectionPoint} in
* the target method. The specified {@link InjectionPoint} <i>must only</i>
* return {@link org.spongepowered.asm.lib.tree.MethodInsnNode} instances
* and an exception will be thrown if this is not the case.
*
* @return {@link At} which identifies the target method invocation
*/
public At at();
/**
* When creating a {@link ModifyVariable} callback, you may wish to first
* inspect the local variable table in the target method at the injection
* point. Set this value to <tt>true</tt> to print the local variable table
* and perform no injection.
*
* @return true to print the LVT to the console
*/
public boolean print() default false;
/**
* Gets the local variable ordinal by type. For example, if there are 3
* {@link String} arguments in the local variable table, ordinal 0 specifies
* the first, 1 specifies the second, etc. Use <tt>ordinal</tt> when the
* index within the LVT is known. Takes precedence over {@link index}.
*
* @return variable ordinal
*/
public int ordinal() default -1;
/**
* Gets the absolute index of the local variable within the local variable
* table to capture. The local variable at the specified index must be of
* the same type as the capture. Takes precedence over {@link name}.
*
* @return argument index to modify or -1 for automatic
*/
public int index() default -1;
/**
* Gets the name of the variable to capture. Only used if the variable
* cannot be located via {@link ordinal} or {@link index}.
*
* @return possible names to capture, only useful when the LVT in the target
* method is known to be complete.
*/
public String[] name() default {};
/**
* Under normal circumstances the injector will consider all local variables
* including method arguments and all other local variables. This involves
* reading (and possibly generating) the local variable table for the method
* which can have mixed results. Set this value to <tt>true</tt> to <i>only
* </i> consider method arguments.
*
* @return true if this injector should only consider method arguments and
* not all locals.
*/
public boolean argsOnly() default false;
/**
* By default, the annotation processor will attempt to locate an
* obfuscation mapping for all {@link ModifyVariable} methods since it is
* anticipated that in general the target of a {@link ModifyVariable}
* annotation will be an obfuscated method in the target class. However
* since it is possible to also apply mixins to non-obfuscated targets (or
* non- obfuscated methods in obfuscated targets, such as methods added by
* Forge) it may be necessary to suppress the compiler error which would
* otherwise be generated. Setting this value to <em>false</em> will cause
* the annotation processor to skip this annotation when attempting to build
* the obfuscation table for the mixin.
*
* @return True to instruct the annotation processor to search for
* obfuscation mappings for this annotation
*/
public boolean remap() default true;
/**
* In general, injectors are intended to "fail soft" in that a failure to
* locate the injection point in the target method is not considered an
* error condition. Another transformer may have changed the method
* structure or any number of reasons may cause an injection to fail. This
* also makes it possible to define several injections to achieve the same
* task given <em>expected</em> mutation of the target class and the
* injectors which fail are simply ignored.
*
* <p>However, this behaviour is not always desirable. For example, if your
* application depends on a particular injection succeeding you may wish to
* detect the injection failure as an error condition. This argument is thus
* provided to allow you to stipulate a <b>minimum</b> number of successful
* injections for this callback handler. If the number of injections
* specified is not achieved then an {@link InjectionError} is thrown at
* application time. Use this option with care.</p>
*
* @return Minimum required number of injected callbacks, default specified
* by the containing config
*/
public int require() default -1;
/**
* Like {@link #require()} but only enabled if the
* {@link Option#DEBUG_INJECTORS mixin.debug.countInjections} option is set
* to <tt>true</tt> and defaults to 1. Use this option during debugging to
* perform simple checking of your injectors. Causes the injector to throw
* a {@link InvalidInjectionException} if the expected number of injections
* is not realised.
*
* @return Minimum number of <em>expected</em> callbacks, default 1
*/
public int expect() default 1;
}
@@ -128,9 +128,10 @@ public @interface Redirect {
* specified is not achieved then an {@link InjectionError} is thrown at
* application time. Use this option with care.</p>
*
* @return Minimum required number of injected callbacks, default 0
* @return Minimum required number of injected callbacks, default specified
* by the containing config
*/
public int require() default 0;
public int require() default -1;
/**
* Like {@link #require()} but only enabled if the
@@ -310,6 +310,8 @@ public class CallbackInjector extends Injector {
*/
@Override
protected void sanityCheck(Target target, List<InjectionPoint> injectionPoints) {
super.sanityCheck(target, injectionPoints);
if (ASMHelper.methodIsStatic(target.method) != this.isStatic) {
throw new InvalidInjectionException(this.info, "'static' modifier of callback method does not match target in " + this.methodNode.name);
}
@@ -65,6 +65,16 @@ public abstract class Injector {
* Callback method
*/
protected final MethodNode methodNode;
/**
* Arguments of the handler method
*/
protected final Type[] methodArgs;
/**
* Return type of the handler method
*/
protected final Type returnType;
/**
* True if the callback method is static
@@ -90,6 +100,8 @@ public abstract class Injector {
private Injector(ClassNode classNode, MethodNode methodNode) {
this.classNode = classNode;
this.methodNode = methodNode;
this.methodArgs = Type.getArgumentTypes(this.methodNode.desc);
this.returnType = Type.getReturnType(this.methodNode.desc);
this.isStatic = ASMHelper.methodIsStatic(this.methodNode);
}
@@ -126,7 +138,7 @@ public abstract class Injector {
for (InjectionPoint injectionPoint : injectionPoints) {
nodes.clear();
if (injectionPoint.find(into.desc, insns, nodes)) {
if (this.findTargetNodes(into, injectionPoint, insns, nodes)) {
targetNodes.addAll(nodes);
}
}
@@ -135,6 +147,10 @@ public abstract class Injector {
return targetNodes;
}
protected boolean findTargetNodes(MethodNode into, InjectionPoint injectionPoint, ReadOnlyInsnList insns, Collection<AbstractInsnNode> nodes) {
return injectionPoint.find(into.desc, insns, nodes);
}
protected void sanityCheck(Target target, List<InjectionPoint> injectionPoints) {
// stub for subclasses
}
@@ -43,16 +43,6 @@ import org.spongepowered.asm.mixin.injection.struct.Target;
*/
public abstract class InvokeInjector extends Injector {
/**
* Arguments of the handler method
*/
protected final Type[] methodArgs;
/**
* Return type of the handler method
*/
protected final Type returnType;
protected final String annotationType;
/**
@@ -61,8 +51,6 @@ public abstract class InvokeInjector extends Injector {
*/
public InvokeInjector(InjectionInfo info, String annotationType) {
super(info);
this.methodArgs = Type.getArgumentTypes(this.methodNode.desc);
this.returnType = Type.getReturnType(this.methodNode.desc);
this.annotationType = annotationType;
}
@@ -73,6 +61,8 @@ public abstract class InvokeInjector extends Injector {
*/
@Override
protected void sanityCheck(Target target, List<InjectionPoint> injectionPoints) {
super.sanityCheck(target, injectionPoints);
if (target.isStatic != this.isStatic) {
throw new InvalidInjectionException(this.info, "'static' modifier of callback method does not match target in " + this.methodNode.name);
}
@@ -0,0 +1,331 @@
/*
* This file is part of Mixin, licensed under the MIT License (MIT).
*
* Copyright (c) SpongePowered <https://www.spongepowered.org>
* 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.modify;
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;
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.LocalVariableNode;
import org.spongepowered.asm.lib.tree.VarInsnNode;
import org.spongepowered.asm.mixin.injection.InjectionPoint;
import org.spongepowered.asm.mixin.injection.InvalidInjectionException;
import org.spongepowered.asm.mixin.injection.code.Injector;
import org.spongepowered.asm.mixin.injection.modify.ModifyVariableInjector.Context.Local;
import org.spongepowered.asm.mixin.injection.struct.InjectionInfo;
import org.spongepowered.asm.mixin.injection.struct.Target;
import org.spongepowered.asm.util.ASMHelper;
import org.spongepowered.asm.util.Locals;
import org.spongepowered.asm.util.PrettyPrinter;
import org.spongepowered.asm.util.SignaturePrinter;
/**
* A bytecode injector which allows a single argument of a chosen method call to
* be altered.
*/
public class ModifyVariableInjector extends Injector {
class Context extends InsnList {
class Local {
int ord = 0;
String name;
Type type;
public Local(String name, Type type) {
this.name = name;
this.type = type;
}
@Override
public String toString() {
return String.format("Local[ordinal=%d, name=%s, type=%s]", this.ord, this.name, this.type);
}
}
final Target target;
final AbstractInsnNode node;
final int baseArgIndex;
final Local[] locals;
private final ClassNode targetClass;
private final boolean isStaticHandler;
public Context(ClassNode targetClass, boolean isStaticHandler, Target target, AbstractInsnNode node, boolean argsOnly) {
this.targetClass = targetClass;
this.isStaticHandler = isStaticHandler;
this.target = target;
this.node = node;
this.baseArgIndex = this.isStaticHandler ? 0 : 1;
this.locals = this.initLocals(target, node, argsOnly);
this.initOrdinals();
}
private Local[] initLocals(Target target, AbstractInsnNode node, boolean argsOnly) {
if (!argsOnly) {
LocalVariableNode[] locals = Locals.getLocalsAt(this.targetClass, target.method, node);
if (locals != null) {
Local[] lvt = new Local[locals.length];
for (int l = 0; l < locals.length; l++) {
if (locals[l] != null) {
lvt[l] = new Local(locals[l].name, Type.getType(locals[l].desc));
}
}
return lvt;
}
}
Local[] lvt = new Local[this.baseArgIndex + target.arguments.length];
if (!this.isStaticHandler) {
lvt[0] = new Local("this", Type.getType(this.targetClass.name));
}
for (int local = this.baseArgIndex; local < lvt.length; local++) {
Type arg = target.arguments[local - this.baseArgIndex];
lvt[local] = new Local("arg" + local, arg);
}
return lvt;
}
private void initOrdinals() {
Map<Type, Integer> ordinalMap = new HashMap<Type, Integer>();
for (int l = 0; l < this.locals.length; l++) {
Integer ordinal = Integer.valueOf(0);
if (this.locals[l] != null) {
ordinal = ordinalMap.get(this.locals[l].type);
ordinalMap.put(this.locals[l].type, ordinal = Integer.valueOf(ordinal == null ? 0 : ordinal.intValue() + 1));
this.locals[l].ord = ordinal.intValue();
}
}
}
}
/**
* Print LVT
*/
private final boolean print;
/**
* True to consider only method args
*/
private final boolean argsOnly;
/**
* Ordinal of the target variable or -1 to fail over to {@link index}
*/
private final int ordinal;
/**
* Ordinal of the target variable or -1 to fail over to {@link names}
*/
private final int index;
/**
* Candidate names for the local variable, if empty fails over to matching
* single local by type
*/
private final Set<String> names;
/**
* @param info Injection info
* @param print
* @param argsOnly
* @param ordinal target variable ordinal
* @param index target variable index
* @param names target variable names
*/
public ModifyVariableInjector(InjectionInfo info, boolean print, boolean argsOnly, int ordinal, int index, Set<String> names) {
super(info);
this.print = print;
this.argsOnly = argsOnly;
this.ordinal = ordinal;
this.index = index;
this.names = names;
}
/* (non-Javadoc)
* @see org.spongepowered.asm.mixin.injection.callback.BytecodeInjector
* #sanityCheck(org.spongepowered.asm.mixin.injection.callback.Target,
* java.util.List)
*/
@Override
protected void sanityCheck(Target target, List<InjectionPoint> injectionPoints) {
super.sanityCheck(target, injectionPoints);
if (target.isStatic != this.isStatic) {
throw new InvalidInjectionException(this.info, "'static' of variable modifier method does not match target in " + this.methodNode.name);
}
if (this.ordinal < -1) {
throw new InvalidInjectionException(this.info, "Invalid ordinal " + this.ordinal + " specified in " + this.methodNode.name);
}
if (this.index == 0 && !this.isStatic) {
throw new InvalidInjectionException(this.info, "Invalid index 0 specified in non-static variable modifier " + this.methodNode.name);
}
}
/**
* If the user specifies no values for {@link ordinal}, {@link index} or
* {@link names} then we are considered to be operating in "implicit mode"
* where only a single local variable of the specified type is expected to
* exist.
*
* @param context Target context
* @return true if operating in implicit mode
*/
protected boolean isImplicitMode(final Context context) {
return this.ordinal < 0 && this.index < context.baseArgIndex && this.names.isEmpty();
}
/**
* Do the injection
*/
@Override
protected void inject(Target target, AbstractInsnNode node) {
Context context = new Context(this.classNode, this.isStatic, target, node, this.argsOnly);
if (this.print) {
this.printLocals(context);
}
int local = this.findLocal(context);
if (local > -1) {
this.inject(context, local);
}
target.insns.insertBefore(node, context);
target.addToStack(this.isStatic ? 1 : 2);
}
/**
* Pretty-print local variable information to stderr
*/
private void printLocals(final Context context) {
SignaturePrinter handlerSig = new SignaturePrinter(this.methodNode.name, this.returnType, this.methodArgs, new String[] { "var" });
handlerSig.setModifiers(this.methodNode);
PrettyPrinter printer = new PrettyPrinter();
printer.add("%20s : %s", "Target Class", this.classNode.name.replace('/', '.'));
printer.add("%20s : %s", "Target Method", context.target.method.name);
printer.add("%20s : %s", "Callback Name", this.methodNode.name);
printer.add("%20s : %s", "Capture Type", SignaturePrinter.getTypeName(this.returnType, false));
printer.add("%20s : %s %s", "Instruction", context.node.getClass().getSimpleName(), ASMHelper.getOpcodeName(context.node.getOpcode())).hr();
printer.add("%20s : %s", "Match mode", this.isImplicitMode(context) ? "IMPLICIT (match single)" : "EXPLICIT (match by criteria)");
printer.add("%20s : %s", "Match ordinal", this.ordinal < 0 ? "any" : this.ordinal);
printer.add("%20s : %s", "Match index", this.index < context.baseArgIndex ? "any" : this.index);
printer.add("%20s : %s", "Match name(s)", this.names.isEmpty() ? "any" : this.names);
printer.add("%20s : %s", "Args only", this.argsOnly).hr();
printer.add("%5s %7s %20s %-50s %s", "INDEX", "ORDINAL", "TYPE", "NAME", "CANDIDATE");
for (int l = context.baseArgIndex; l < context.locals.length; l++) {
Local local = context.locals[l];
if (local != null) {
Type localType = local.type;
String localName = local.name;
int ordinal = local.ord;
String candidate = this.returnType.equals(localType) ? "YES" : "-";
printer.add("[%3d] [%3d] %20s %-50s %s", l, ordinal, SignaturePrinter.getTypeName(localType, false), localName, candidate);
} else if (l > 0) {
Local prevLocal = context.locals[l - 1];
boolean isTop = prevLocal != null && prevLocal.type != null && prevLocal.type.getSize() > 1;
printer.add("[%3d] %20s", l, isTop ? "<top>" : "-");
}
}
printer.print(System.err);
}
private void inject(final Context context, final int local) {
if (!this.isStatic) {
context.add(new VarInsnNode(Opcodes.ALOAD, 0));
}
context.add(new VarInsnNode(this.returnType.getOpcode(Opcodes.ILOAD), local));
this.invokeHandler(context);
context.add(new VarInsnNode(this.returnType.getOpcode(Opcodes.ISTORE), local));
}
private int findLocal(Context context) {
if (this.isImplicitMode(context)) {
return this.findImplicitLocal(context);
}
return this.findExplicitLocal(context);
}
private int findImplicitLocal(final Context context) {
int found = 0;
int count = 0;
for (int index = context.baseArgIndex; index < context.locals.length; index++) {
Local local = context.locals[index];
if (local == null || !local.type.equals(this.returnType)) {
continue;
}
count++;
found = index;
}
if (count == 1) {
return found;
}
throw new InvalidInjectionException(this.info, "Implicit variable modifier injection failed in " + this.methodNode.name + ", found "
+ count + " candidate variables but exactly 1 is required.");
}
private int findExplicitLocal(final Context context) {
for (int index = context.baseArgIndex; index < context.locals.length; index++) {
Local local = context.locals[index];
if (local == null || !local.type.equals(this.returnType)) {
continue;
}
if (this.ordinal > -1) {
if (this.ordinal == local.ord) {
return index;
}
continue;
}
if (this.index >= context.baseArgIndex) {
if (this.index == index) {
return index;
}
continue;
}
if (this.names.contains(local.name)) {
return index;
}
}
return -1;
}
}
@@ -45,4 +45,5 @@ public class CallbackInjectionInfo extends InjectionInfo {
return new CallbackInjector(this, cancellable, locals);
}
}
@@ -42,6 +42,7 @@ import org.spongepowered.asm.mixin.injection.InjectionPoint;
import org.spongepowered.asm.mixin.injection.InjectorGroupInfo;
import org.spongepowered.asm.mixin.injection.InvalidInjectionException;
import org.spongepowered.asm.mixin.injection.ModifyArg;
import org.spongepowered.asm.mixin.injection.ModifyVariable;
import org.spongepowered.asm.mixin.injection.Redirect;
import org.spongepowered.asm.mixin.injection.code.Injector;
import org.spongepowered.asm.mixin.transformer.InvalidMixinException;
@@ -192,7 +193,7 @@ public abstract class InjectionInfo {
}
Integer require = ASMHelper.<Integer>getAnnotationValue(this.annotation, "require");
if (require != null) {
if (require != null && require.intValue() > -1) {
this.requiredCallbackCount = require.intValue();
} else if (this.group.isDefault()) {
this.requiredCallbackCount = this.mixin.getDefaultRequiredInjections();
@@ -234,15 +235,19 @@ public abstract class InjectionInfo {
if ((MixinEnvironment.getCurrentEnvironment().getOption(Option.DEBUG_INJECTORS) && this.injectedCallbackCount < this.expectedCallbackCount)) {
throw new InvalidInjectionException(this,
String.format("Injection validation failed: Callback method %s%s in %s expected %d invocation(s) but %d succeeded",
this.method.name, this.method.desc, this.mixin, this.expectedCallbackCount, this.injectedCallbackCount));
String.format("Injection validation failed: %s %s%s in %s expected %d invocation(s) but %d succeeded",
this.getDescription(), this.method.name, this.method.desc, this.mixin, this.expectedCallbackCount, this.injectedCallbackCount));
} else if (this.injectedCallbackCount < this.requiredCallbackCount) {
throw new InjectionError(
String.format("Critical injection failure: Callback method %s%s in %s failed injection check, (%d/%d) succeeded",
this.method.name, this.method.desc, this.mixin, this.injectedCallbackCount, this.requiredCallbackCount));
String.format("Critical injection failure: %s %s%s in %s failed injection check, (%d/%d) succeeded",
this.getDescription(), this.method.name, this.method.desc, this.mixin, this.injectedCallbackCount, this.requiredCallbackCount));
}
}
protected String getDescription() {
return "Callback method";
}
@Override
public String toString() {
return String.format("%s->@%s::%s%s", this.mixin.toString(), this.annotationType, this.method.name, this.method.desc);
@@ -356,7 +361,7 @@ public abstract class InjectionInfo {
public static InjectionInfo parse(MixinTargetContext mixin, MethodNode method) {
AnnotationNode annotation = null;
try {
annotation = ASMHelper.getSingleVisibleAnnotation(method, Inject.class, ModifyArg.class, Redirect.class);
annotation = ASMHelper.getSingleVisibleAnnotation(method, Inject.class, ModifyArg.class, Redirect.class, ModifyVariable.class);
} catch (IllegalArgumentException ex) {
throw new InvalidMixinException(mixin, "Error parsing annotations on " + method.name + " in " + mixin.getClassName() + ": "
+ ex.getMessage());
@@ -372,6 +377,8 @@ public abstract class InjectionInfo {
return new ModifyArgInjectionInfo(mixin, method, annotation);
} else if (annotation.desc.endsWith(Redirect.class.getSimpleName() + ";")) {
return new RedirectInjectionInfo(mixin, method, annotation);
} else if (annotation.desc.endsWith(ModifyVariable.class.getSimpleName() + ";")) {
return new ModifyVariableInjectionInfo(mixin, method, annotation);
}
return null;
@@ -43,4 +43,10 @@ public class ModifyArgInjectionInfo extends InjectionInfo {
return new ModifyArgInjector(this, index);
}
@Override
protected String getDescription() {
return "Argument modifier method";
}
}
@@ -0,0 +1,65 @@
/*
* This file is part of Mixin, licensed under the MIT License (MIT).
*
* Copyright (c) SpongePowered <https://www.spongepowered.org>
* 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.struct;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.spongepowered.asm.lib.tree.AnnotationNode;
import org.spongepowered.asm.lib.tree.MethodNode;
import org.spongepowered.asm.mixin.injection.code.Injector;
import org.spongepowered.asm.mixin.injection.modify.ModifyVariableInjector;
import org.spongepowered.asm.mixin.transformer.MixinTargetContext;
import org.spongepowered.asm.util.ASMHelper;
public class ModifyVariableInjectionInfo extends InjectionInfo {
public ModifyVariableInjectionInfo(MixinTargetContext mixin, MethodNode method, AnnotationNode annotation) {
super(mixin, method, annotation);
}
@Override
protected Injector initInjector(AnnotationNode injectAnnotation) {
boolean print = ASMHelper.<Boolean>getAnnotationValue(injectAnnotation, "print", Boolean.FALSE).booleanValue();
boolean argsOnly = ASMHelper.<Boolean>getAnnotationValue(injectAnnotation, "argsOnly", Boolean.FALSE).booleanValue();
int ordinal = ASMHelper.<Integer>getAnnotationValue(injectAnnotation, "ordinal", -1);
int index = ASMHelper.<Integer>getAnnotationValue(injectAnnotation, "index", -1);
Set<String> names = new HashSet<String>();
List<String> namesList = ASMHelper.<List<String>>getAnnotationValue(injectAnnotation, "name", (List<String>)null);
if (namesList != null) {
names.addAll(namesList);
}
return new ModifyVariableInjector(this, print, argsOnly, ordinal, index, names);
}
@Override
protected String getDescription() {
return "Variable modifier method";
}
}
@@ -40,4 +40,10 @@ public class RedirectInjectionInfo extends InjectionInfo {
protected Injector initInjector(AnnotationNode injectAnnotation) {
return new RedirectInjector(this);
}
@Override
protected String getDescription() {
return "Redirector";
}
}