mirror of
https://github.com/LegacyModdingMC/UniMix.git
synced 2026-11-28 21:38:42 +00:00
More injector refactoring, allow capture target args in ModifyVariable
This commit is contained in:
1 parent
dd89e23215
commit
f9e19c32f4
6 files changed
+288
-297
No files matched your search
@@ -63,6 +63,10 @@ import org.spongepowered.asm.util.ConstraintParser.Constraint;
|
||||
* 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>
|
||||
*
|
||||
* <p>Like other injectors, the callback signature can optionally include the
|
||||
* target method arguments by simply appending them to the handler method
|
||||
* signature.</p>
|
||||
*/
|
||||
@Target({ ElementType.METHOD })
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@@ -87,9 +91,7 @@ public @interface ModifyVariable {
|
||||
|
||||
/**
|
||||
* An {@link At} annotation which describes the {@link InjectionPoint} in
|
||||
* the target method. The specified {@link InjectionPoint} <i>must only</i>
|
||||
* return {@link org.objectweb.asm.tree.MethodInsnNode} instances
|
||||
* and an exception will be thrown if this is not the case.
|
||||
* the target method.
|
||||
*
|
||||
* @return {@link At} which identifies the target method invocation
|
||||
*/
|
||||
|
||||
@@ -25,6 +25,7 @@
|
||||
package org.spongepowered.asm.mixin.injection.code;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.HashSet;
|
||||
@@ -37,19 +38,15 @@ import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
import org.objectweb.asm.Opcodes;
|
||||
import org.objectweb.asm.Type;
|
||||
import org.objectweb.asm.tree.AbstractInsnNode;
|
||||
import org.objectweb.asm.tree.ClassNode;
|
||||
import org.objectweb.asm.tree.InsnList;
|
||||
import org.objectweb.asm.tree.InsnNode;
|
||||
import org.objectweb.asm.tree.LdcInsnNode;
|
||||
import org.objectweb.asm.tree.MethodInsnNode;
|
||||
import org.objectweb.asm.tree.MethodNode;
|
||||
import org.objectweb.asm.tree.TypeInsnNode;
|
||||
import org.objectweb.asm.tree.*;
|
||||
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.invoke.RedirectInjector;
|
||||
import org.spongepowered.asm.mixin.injection.struct.InjectionInfo;
|
||||
import org.spongepowered.asm.mixin.injection.struct.InjectionNodes.InjectionNode;
|
||||
import org.spongepowered.asm.mixin.injection.struct.Target.Extension;
|
||||
import org.spongepowered.asm.mixin.injection.struct.Target;
|
||||
import org.spongepowered.asm.mixin.injection.throwables.InjectionError;
|
||||
import org.spongepowered.asm.mixin.injection.throwables.InvalidInjectionException;
|
||||
@@ -57,9 +54,13 @@ import org.spongepowered.asm.mixin.refmap.IMixinContext;
|
||||
import org.spongepowered.asm.mixin.transformer.ClassInfo;
|
||||
import org.spongepowered.asm.mixin.transformer.ClassInfo.Traversal;
|
||||
import org.spongepowered.asm.mixin.transformer.ClassInfo.TypeLookup;
|
||||
import org.spongepowered.asm.util.Annotations;
|
||||
import org.spongepowered.asm.util.Bytecode;
|
||||
import org.spongepowered.asm.util.SignaturePrinter;
|
||||
import org.spongepowered.asm.util.Bytecode.DelegateInitialiser;
|
||||
|
||||
import com.google.common.collect.ObjectArrays;
|
||||
|
||||
/**
|
||||
* Base class for bytecode injectors
|
||||
*/
|
||||
@@ -102,6 +103,66 @@ public abstract class Injector {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Redirection data bundle base. No this isn't meant to be pretty, it's a
|
||||
* way of passing a bunch of state around the injector without having dumb
|
||||
* method signatures.
|
||||
*/
|
||||
public static class InjectorData {
|
||||
|
||||
/**
|
||||
* Redirect target
|
||||
*/
|
||||
public final Target target;
|
||||
|
||||
/**
|
||||
* Mutable description. The bundle is be passed to different types of
|
||||
* handler and the handler decorates the bundle with a description of
|
||||
* the <em>type</em> of injection, purely for use in error messages.
|
||||
*/
|
||||
public String description;
|
||||
|
||||
/**
|
||||
* When passing through {@link RedirectInjector#validateParams} this
|
||||
* switch determines whether coercion is supported for both the primary
|
||||
* handler args and captured target args, or for target args only.
|
||||
*/
|
||||
public boolean allowCoerceArgs;
|
||||
|
||||
/**
|
||||
* Number of arguments to capture from the target, determined by the
|
||||
* number of extra args on the handler method
|
||||
*/
|
||||
public int captureTargetArgs = 0;
|
||||
|
||||
/**
|
||||
* True if the method itself is decorated with {@link Coerce} and the
|
||||
* return type is coerced. Instructs the injector to add a CHECKCAST
|
||||
* following the handler.
|
||||
*/
|
||||
public boolean coerceReturnType = false;
|
||||
|
||||
public InjectorData(Target target) {
|
||||
this(target, "handler");
|
||||
}
|
||||
|
||||
public InjectorData(Target target, String description) {
|
||||
this(target, description, true);
|
||||
}
|
||||
|
||||
public InjectorData(Target target, String description, boolean allowCoerceArgs) {
|
||||
this.target = target;
|
||||
this.description = description;
|
||||
this.allowCoerceArgs = allowCoerceArgs;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return this.description;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Log more things
|
||||
*/
|
||||
@@ -351,7 +412,204 @@ public abstract class Injector {
|
||||
this.info.addCallbackInvocation(handler);
|
||||
return insn;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param args handler arguments
|
||||
* @param insns InsnList to inject insns into
|
||||
* @param argMap Mapping of args to local variables
|
||||
* @return injected insn node
|
||||
*/
|
||||
protected AbstractInsnNode invokeHandlerWithArgs(Type[] args, InsnList insns, int[] argMap) {
|
||||
return this.invokeHandlerWithArgs(args, insns, argMap, 0, args.length);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param args handler arguments
|
||||
* @param insns InsnList to inject insns into
|
||||
* @param argMap Mapping of args to local variables
|
||||
* @param startArg Starting arg to consume
|
||||
* @param endArg Ending arg to consume
|
||||
* @return injected insn node
|
||||
*/
|
||||
protected AbstractInsnNode invokeHandlerWithArgs(Type[] args, InsnList insns, int[] argMap, int startArg, int endArg) {
|
||||
if (!this.isStatic) {
|
||||
insns.add(new VarInsnNode(Opcodes.ALOAD, 0));
|
||||
}
|
||||
this.pushArgs(args, insns, argMap, startArg, endArg);
|
||||
return this.invokeHandler(insns);
|
||||
}
|
||||
|
||||
/**
|
||||
* Store args on the stack starting at the end and working back to position
|
||||
* specified by start, return the generated argMap
|
||||
*
|
||||
* @param target target method
|
||||
* @param args argument types
|
||||
* @param insns instruction list to generate insns into
|
||||
* @param start Starting index
|
||||
* @return the generated argmap
|
||||
*/
|
||||
protected int[] storeArgs(Target target, Type[] args, InsnList insns, int start) {
|
||||
int[] argMap = target.generateArgMap(args, start);
|
||||
this.storeArgs(args, insns, argMap, start, args.length);
|
||||
return argMap;
|
||||
}
|
||||
|
||||
/**
|
||||
* Store args on the stack to their positions allocated based on argMap
|
||||
*
|
||||
* @param args argument types
|
||||
* @param insns instruction list to generate insns into
|
||||
* @param argMap generated argmap containing local indices for all args
|
||||
* @param start Starting index
|
||||
* @param end Ending index
|
||||
*/
|
||||
protected void storeArgs(Type[] args, InsnList insns, int[] argMap, int start, int end) {
|
||||
for (int arg = end - 1; arg >= start; arg--) {
|
||||
insns.add(new VarInsnNode(args[arg].getOpcode(Opcodes.ISTORE), argMap[arg]));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Load args onto the stack from their positions allocated in argMap
|
||||
*
|
||||
* @param args argument types
|
||||
* @param insns instruction list to generate insns into
|
||||
* @param argMap generated argmap containing local indices for all args
|
||||
* @param start Starting index
|
||||
* @param end Ending index
|
||||
*/
|
||||
protected void pushArgs(Type[] args, InsnList insns, int[] argMap, int start, int end) {
|
||||
this.pushArgs(args, insns, argMap, start, end, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Load args onto the stack from their positions allocated in argMap
|
||||
*
|
||||
* @param args argument types
|
||||
* @param insns instruction list to generate insns into
|
||||
* @param argMap generated argmap containing local indices for all args
|
||||
* @param start Starting index
|
||||
* @param end Ending index
|
||||
*/
|
||||
protected void pushArgs(Type[] args, InsnList insns, int[] argMap, int start, int end, Extension extension) {
|
||||
for (int arg = start; arg < end && arg < args.length; arg++) {
|
||||
insns.add(new VarInsnNode(args[arg].getOpcode(Opcodes.ILOAD), argMap[arg]));
|
||||
if (extension != null) {
|
||||
extension.add(args[arg].getSize());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Collects all the logic from old validateParams/checkDescriptor so that we
|
||||
* can consistently apply coercion logic to method params, and also provide
|
||||
* more detailed errors when something doesn't line up.
|
||||
*
|
||||
* <p>The supplied return type and argument list will be verified first. Any
|
||||
* arguments on the handler beyond the base arguments consume arguments from
|
||||
* the target. The flag <tt>allowCoerceArgs</tt> on the <tt>redirect</tt>
|
||||
* instance determines whether coercion is supported for the base args and
|
||||
* return type, coercion is always allowed for captured target args.</p>
|
||||
*
|
||||
* <p>Following validation, the <tt>captureTargetArgs</tt> and
|
||||
* <tt>coerceReturnType</tt> values will be set on the bundle and the
|
||||
* calling injector function should adjust itself accordingly.</p>
|
||||
*
|
||||
* @param injector Data bundle for the injector
|
||||
* @param returnType Return type for the handler, must not be null
|
||||
* @param args Array of handler args, must not be null
|
||||
*/
|
||||
protected final void validateParams(InjectorData injector, Type returnType, Type... args) {
|
||||
String description = String.format("%s %s method %s from %s", this.annotationType, injector, this, this.info.getContext());
|
||||
int argIndex = 0;
|
||||
try {
|
||||
injector.coerceReturnType = this.checkCoerce(-1, returnType, description, injector.allowCoerceArgs);
|
||||
|
||||
for (Type arg : args) {
|
||||
if (arg != null) {
|
||||
this.checkCoerce(argIndex, arg, description, injector.allowCoerceArgs);
|
||||
argIndex++;
|
||||
}
|
||||
}
|
||||
|
||||
if (argIndex == this.methodArgs.length) {
|
||||
return;
|
||||
}
|
||||
|
||||
for (int targetArg = 0; targetArg < injector.target.arguments.length && argIndex < this.methodArgs.length; targetArg++, argIndex++) {
|
||||
this.checkCoerce(argIndex, injector.target.arguments[targetArg], description, true);
|
||||
injector.captureTargetArgs++;
|
||||
}
|
||||
} catch (InvalidInjectionException ex) {
|
||||
String expected = this.methodArgs.length > args.length
|
||||
? Bytecode.generateDescriptor(returnType, ObjectArrays.concat(args, injector.target.arguments, Type.class))
|
||||
: Bytecode.generateDescriptor(returnType, args);
|
||||
throw new InvalidInjectionException(this.info, String.format("%s. Handler signature: %s Expected signature: %s", ex.getMessage(),
|
||||
this.methodNode.desc, expected));
|
||||
}
|
||||
|
||||
if (argIndex < this.methodArgs.length) {
|
||||
Type[] extraArgs = Arrays.copyOfRange(this.methodArgs, argIndex, this.methodArgs.length);
|
||||
throw new InvalidInjectionException(this.info, String.format(
|
||||
"%s has an invalid signature. Found %d unexpected additional method arguments: %s",
|
||||
description, this.methodArgs.length - argIndex, new SignaturePrinter(extraArgs).getFormattedArgs()));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Called inside {@link #validateParams} but can also be used directly. This
|
||||
* method checks whether the supplied type is compatible with the specified
|
||||
* handler argument, apply coercion logic where necessary.
|
||||
*
|
||||
* @param index Handler argument index, pass in a negative value (by
|
||||
* convention -1) to specify handler return type
|
||||
* @param toType Desired type based on the injector contract
|
||||
* @param description human-readable description of the handler method, used
|
||||
* in raised exception
|
||||
* @param allowCoercion True if coercion logic can be applied to this
|
||||
* argument, false to only allow a precise match
|
||||
* @return <tt>false</tt> if the argument matched perfectly, <tt>true</tt>
|
||||
* if coercion is required for the argument
|
||||
*/
|
||||
protected final boolean checkCoerce(int index, Type toType, String description, boolean allowCoercion) {
|
||||
Type fromType = index < 0 ? this.returnType : this.methodArgs[index];
|
||||
if (index >= this.methodArgs.length) {
|
||||
throw new InvalidInjectionException(this.info, String.format(
|
||||
"%s has an invalid signature. Not enough arguments: expected argument type %s at index %d",
|
||||
description, SignaturePrinter.getTypeName(toType), index));
|
||||
}
|
||||
|
||||
AnnotationNode coerce = Annotations.getInvisibleParameter(this.methodNode, Coerce.class, index);
|
||||
boolean isReturn = index < 0;
|
||||
String argType = isReturn ? "return" : "argument";
|
||||
Object argIndex = isReturn ? "" : " at index " + index;
|
||||
|
||||
if (fromType.equals(toType)) {
|
||||
if (coerce != null && this.info.getContext().getOption(Option.DEBUG_VERBOSE)) {
|
||||
Injector.logger.info("Possibly-redundant @Coerce on {} {} type{}, {} is identical to {}", description, argType, argIndex,
|
||||
SignaturePrinter.getTypeName(toType), SignaturePrinter.getTypeName(fromType));
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
if (coerce == null || !allowCoercion) {
|
||||
String coerceWarning = coerce != null ? ". @Coerce not allowed here" : "";
|
||||
throw new InvalidInjectionException(this.info, String.format(
|
||||
"%s has an invalid signature. Found unexpected %s type %s%s, expected %s%s", description, argType,
|
||||
SignaturePrinter.getTypeName(fromType), argIndex, SignaturePrinter.getTypeName(toType), coerceWarning));
|
||||
}
|
||||
|
||||
boolean canCoerce = Injector.canCoerce(fromType, toType);
|
||||
if (!canCoerce) {
|
||||
throw new InvalidInjectionException(this.info, String.format(
|
||||
"%s has an invalid signature. Cannot @Coerce %s type %s%s to %s", description, argType,
|
||||
SignaturePrinter.getTypeName(toType), argIndex, SignaturePrinter.getTypeName(fromType)));
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Throw an exception. The exception class must have a string which takes a
|
||||
* string argument
|
||||
|
||||
@@ -24,95 +24,20 @@
|
||||
*/
|
||||
package org.spongepowered.asm.mixin.injection.invoke;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import org.objectweb.asm.Opcodes;
|
||||
import org.objectweb.asm.Type;
|
||||
import org.objectweb.asm.tree.AbstractInsnNode;
|
||||
import org.objectweb.asm.tree.AnnotationNode;
|
||||
import org.objectweb.asm.tree.InsnList;
|
||||
import org.objectweb.asm.tree.MethodInsnNode;
|
||||
import org.objectweb.asm.tree.VarInsnNode;
|
||||
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.code.Injector;
|
||||
import org.spongepowered.asm.mixin.injection.struct.InjectionInfo;
|
||||
import org.spongepowered.asm.mixin.injection.struct.Target;
|
||||
import org.spongepowered.asm.mixin.injection.struct.InjectionNodes.InjectionNode;
|
||||
import org.spongepowered.asm.mixin.injection.struct.Target.Extension;
|
||||
import org.spongepowered.asm.mixin.injection.struct.Target;
|
||||
import org.spongepowered.asm.mixin.injection.throwables.InvalidInjectionException;
|
||||
import org.spongepowered.asm.util.Annotations;
|
||||
import org.spongepowered.asm.util.Bytecode;
|
||||
import org.spongepowered.asm.util.SignaturePrinter;
|
||||
|
||||
import com.google.common.collect.ObjectArrays;
|
||||
|
||||
/**
|
||||
* Base class for injectors which inject at method invokes
|
||||
*/
|
||||
public abstract class InvokeInjector extends Injector {
|
||||
|
||||
/**
|
||||
* Redirection data bundle base. No this isn't meant to be pretty, it's a
|
||||
* way of passing a bunch of state around the injector without having dumb
|
||||
* method signatures.
|
||||
*/
|
||||
static class InjectorData {
|
||||
|
||||
/**
|
||||
* Redirect target
|
||||
*/
|
||||
final Target target;
|
||||
|
||||
/**
|
||||
* Mutable description. The bundle is be passed to different types of
|
||||
* handler and the handler decorates the bundle with a description of
|
||||
* the <em>type</em> of injection, purely for use in error messages.
|
||||
*/
|
||||
String description;
|
||||
|
||||
/**
|
||||
* When passing through {@link RedirectInjector#validateParams} this
|
||||
* switch determines whether coercion is supported for both the primary
|
||||
* handler args and captured target args, or for target args only.
|
||||
*/
|
||||
boolean allowCoerceArgs;
|
||||
|
||||
/**
|
||||
* Number of arguments to capture from the target, determined by the
|
||||
* number of extra args on the handler method
|
||||
*/
|
||||
int captureTargetArgs = 0;
|
||||
|
||||
/**
|
||||
* True if the method itself is decorated with {@link Coerce} and the
|
||||
* return type is coerced. Instructs the injector to add a CHECKCAST
|
||||
* following the handler.
|
||||
*/
|
||||
boolean coerceReturnType = false;
|
||||
|
||||
InjectorData(Target target) {
|
||||
this(target, "handler");
|
||||
}
|
||||
|
||||
InjectorData(Target target, String description) {
|
||||
this(target, description, true);
|
||||
}
|
||||
|
||||
InjectorData(Target target, String description, boolean allowCoerceArgs) {
|
||||
this.target = target;
|
||||
this.description = description;
|
||||
this.allowCoerceArgs = allowCoerceArgs;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return this.description;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @param info Information about this injection
|
||||
@@ -165,201 +90,4 @@ public abstract class InvokeInjector extends Injector {
|
||||
*/
|
||||
protected abstract void injectAtInvoke(Target target, InjectionNode node);
|
||||
|
||||
/**
|
||||
* @param args handler arguments
|
||||
* @param insns InsnList to inject insns into
|
||||
* @param argMap Mapping of args to local variables
|
||||
* @return injected insn node
|
||||
*/
|
||||
protected AbstractInsnNode invokeHandlerWithArgs(Type[] args, InsnList insns, int[] argMap) {
|
||||
return this.invokeHandlerWithArgs(args, insns, argMap, 0, args.length);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param args handler arguments
|
||||
* @param insns InsnList to inject insns into
|
||||
* @param argMap Mapping of args to local variables
|
||||
* @param startArg Starting arg to consume
|
||||
* @param endArg Ending arg to consume
|
||||
* @return injected insn node
|
||||
*/
|
||||
protected AbstractInsnNode invokeHandlerWithArgs(Type[] args, InsnList insns, int[] argMap, int startArg, int endArg) {
|
||||
if (!this.isStatic) {
|
||||
insns.add(new VarInsnNode(Opcodes.ALOAD, 0));
|
||||
}
|
||||
this.pushArgs(args, insns, argMap, startArg, endArg);
|
||||
return this.invokeHandler(insns);
|
||||
}
|
||||
|
||||
/**
|
||||
* Store args on the stack starting at the end and working back to position
|
||||
* specified by start, return the generated argMap
|
||||
*
|
||||
* @param target target method
|
||||
* @param args argument types
|
||||
* @param insns instruction list to generate insns into
|
||||
* @param start Starting index
|
||||
* @return the generated argmap
|
||||
*/
|
||||
protected int[] storeArgs(Target target, Type[] args, InsnList insns, int start) {
|
||||
int[] argMap = target.generateArgMap(args, start);
|
||||
this.storeArgs(args, insns, argMap, start, args.length);
|
||||
return argMap;
|
||||
}
|
||||
|
||||
/**
|
||||
* Store args on the stack to their positions allocated based on argMap
|
||||
*
|
||||
* @param args argument types
|
||||
* @param insns instruction list to generate insns into
|
||||
* @param argMap generated argmap containing local indices for all args
|
||||
* @param start Starting index
|
||||
* @param end Ending index
|
||||
*/
|
||||
protected void storeArgs(Type[] args, InsnList insns, int[] argMap, int start, int end) {
|
||||
for (int arg = end - 1; arg >= start; arg--) {
|
||||
insns.add(new VarInsnNode(args[arg].getOpcode(Opcodes.ISTORE), argMap[arg]));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Load args onto the stack from their positions allocated in argMap
|
||||
*
|
||||
* @param args argument types
|
||||
* @param insns instruction list to generate insns into
|
||||
* @param argMap generated argmap containing local indices for all args
|
||||
* @param start Starting index
|
||||
* @param end Ending index
|
||||
*/
|
||||
protected void pushArgs(Type[] args, InsnList insns, int[] argMap, int start, int end) {
|
||||
this.pushArgs(args, insns, argMap, start, end, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Load args onto the stack from their positions allocated in argMap
|
||||
*
|
||||
* @param args argument types
|
||||
* @param insns instruction list to generate insns into
|
||||
* @param argMap generated argmap containing local indices for all args
|
||||
* @param start Starting index
|
||||
* @param end Ending index
|
||||
*/
|
||||
protected void pushArgs(Type[] args, InsnList insns, int[] argMap, int start, int end, Extension extension) {
|
||||
for (int arg = start; arg < end && arg < args.length; arg++) {
|
||||
insns.add(new VarInsnNode(args[arg].getOpcode(Opcodes.ILOAD), argMap[arg]));
|
||||
if (extension != null) {
|
||||
extension.add(args[arg].getSize());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Collects all the logic from old validateParams/checkDescriptor so that we
|
||||
* can consistently apply coercion logic to method params, and also provide
|
||||
* more detailed errors when something doesn't line up.
|
||||
*
|
||||
* <p>The supplied return type and argument list will be verified first. Any
|
||||
* arguments on the handler beyond the base arguments consume arguments from
|
||||
* the target. The flag <tt>allowCoerceArgs</tt> on the <tt>redirect</tt>
|
||||
* instance determines whether coercion is supported for the base args and
|
||||
* return type, coercion is always allowed for captured target args.</p>
|
||||
*
|
||||
* <p>Following validation, the <tt>captureTargetArgs</tt> and
|
||||
* <tt>coerceReturnType</tt> values will be set on the bundle and the
|
||||
* calling injector function should adjust itself accordingly.</p>
|
||||
*
|
||||
* @param injector Data bundle for the injector
|
||||
* @param returnType Return type for the handler, must not be null
|
||||
* @param args Array of handler args, must not be null
|
||||
*/
|
||||
protected final void validateParams(InjectorData injector, Type returnType, Type... args) {
|
||||
String description = String.format("%s %s method %s from %s", this.annotationType, injector, this, this.info.getContext());
|
||||
int argIndex = 0;
|
||||
try {
|
||||
injector.coerceReturnType = this.checkCoerce(-1, returnType, description, injector.allowCoerceArgs);
|
||||
|
||||
for (Type arg : args) {
|
||||
if (arg != null) {
|
||||
this.checkCoerce(argIndex, arg, description, injector.allowCoerceArgs);
|
||||
argIndex++;
|
||||
}
|
||||
}
|
||||
|
||||
if (argIndex == this.methodArgs.length) {
|
||||
return;
|
||||
}
|
||||
|
||||
for (int targetArg = 0; targetArg < injector.target.arguments.length && argIndex < this.methodArgs.length; targetArg++, argIndex++) {
|
||||
this.checkCoerce(argIndex, injector.target.arguments[targetArg], description, true);
|
||||
injector.captureTargetArgs++;
|
||||
}
|
||||
} catch (InvalidInjectionException ex) {
|
||||
String expected = this.methodArgs.length > args.length
|
||||
? Bytecode.generateDescriptor(returnType, ObjectArrays.concat(args, injector.target.arguments, Type.class))
|
||||
: Bytecode.generateDescriptor(returnType, args);
|
||||
throw new InvalidInjectionException(this.info, String.format("%s. Handler signature: %s Expected signature: %s", ex.getMessage(),
|
||||
this.methodNode.desc, expected));
|
||||
}
|
||||
|
||||
if (argIndex < this.methodArgs.length) {
|
||||
Type[] extraArgs = Arrays.copyOfRange(this.methodArgs, argIndex, this.methodArgs.length);
|
||||
throw new InvalidInjectionException(this.info, String.format(
|
||||
"%s has an invalid signature. Found %d unexpected additional method arguments: %s",
|
||||
description, this.methodArgs.length - argIndex, new SignaturePrinter(extraArgs).getFormattedArgs()));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Called inside {@link #validateParams} but can also be used directly. This
|
||||
* method checks whether the supplied type is compatible with the specified
|
||||
* handler argument, apply coercion logic where necessary.
|
||||
*
|
||||
* @param index Handler argument index, pass in a negative value (by
|
||||
* convention -1) to specify handler return type
|
||||
* @param toType Desired type based on the injector contract
|
||||
* @param description human-readable description of the handler method, used
|
||||
* in raised exception
|
||||
* @param allowCoercion True if coercion logic can be applied to this
|
||||
* argument, false to only allow a precise match
|
||||
* @return <tt>false</tt> if the argument matched perfectly, <tt>true</tt>
|
||||
* if coercion is required for the argument
|
||||
*/
|
||||
protected final boolean checkCoerce(int index, Type toType, String description, boolean allowCoercion) {
|
||||
Type fromType = index < 0 ? this.returnType : this.methodArgs[index];
|
||||
if (index >= this.methodArgs.length) {
|
||||
throw new InvalidInjectionException(this.info, String.format(
|
||||
"%s has an invalid signature. Not enough arguments: expected argument type %s at index %d",
|
||||
description, SignaturePrinter.getTypeName(toType), index));
|
||||
}
|
||||
|
||||
AnnotationNode coerce = Annotations.getInvisibleParameter(this.methodNode, Coerce.class, index);
|
||||
boolean isReturn = index < 0;
|
||||
String argType = isReturn ? "return" : "argument";
|
||||
Object argIndex = isReturn ? "" : " at index " + index;
|
||||
|
||||
if (fromType.equals(toType)) {
|
||||
if (coerce != null && this.info.getContext().getOption(Option.DEBUG_VERBOSE)) {
|
||||
Injector.logger.info("Possibly-redundant @Coerce on {} {} type{}, {} is identical to {}", description, argType, argIndex,
|
||||
SignaturePrinter.getTypeName(toType), SignaturePrinter.getTypeName(fromType));
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
if (coerce == null || !allowCoercion) {
|
||||
String coerceWarning = coerce != null ? ". @Coerce not allowed here" : "";
|
||||
throw new InvalidInjectionException(this.info, String.format(
|
||||
"%s has an invalid signature. Found unexpected %s type %s%s, expected %s%s", description, argType,
|
||||
SignaturePrinter.getTypeName(fromType), argIndex, SignaturePrinter.getTypeName(toType), coerceWarning));
|
||||
}
|
||||
|
||||
boolean canCoerce = Injector.canCoerce(fromType, toType);
|
||||
if (!canCoerce) {
|
||||
throw new InvalidInjectionException(this.info, String.format(
|
||||
"%s has an invalid signature. Cannot @Coerce %s type %s%s to %s", description, argType,
|
||||
SignaturePrinter.getTypeName(toType), argIndex, SignaturePrinter.getTypeName(fromType)));
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
+2
-1
@@ -41,6 +41,7 @@ import org.spongepowered.asm.util.Bytecode;
|
||||
import org.spongepowered.asm.util.Annotations;
|
||||
import org.spongepowered.asm.util.Locals;
|
||||
import org.spongepowered.asm.util.PrettyPrinter;
|
||||
import org.spongepowered.asm.util.PrettyPrinter.IPrettyPrintable;
|
||||
import org.spongepowered.asm.util.SignaturePrinter;
|
||||
|
||||
/**
|
||||
@@ -54,7 +55,7 @@ public class LocalVariableDiscriminator {
|
||||
* Discriminator context information, wraps all relevant information about
|
||||
* a target location for use when performing discrimination
|
||||
*/
|
||||
public static class Context implements org.spongepowered.asm.util.PrettyPrinter.IPrettyPrintable {
|
||||
public static class Context implements IPrettyPrintable {
|
||||
|
||||
/**
|
||||
* Information about a local variable in the LVT, used during
|
||||
|
||||
+10
-8
@@ -150,18 +150,15 @@ public class ModifyVariableInjector extends Injector {
|
||||
this.printLocals(target, context);
|
||||
}
|
||||
|
||||
String handlerDesc = Bytecode.getDescriptor(new Type[] { this.returnType }, this.returnType);
|
||||
if (!handlerDesc.equals(this.methodNode.desc)) {
|
||||
throw new InvalidInjectionException(this.info, "Variable modifier " + this + " has an invalid signature, expected " + handlerDesc
|
||||
+ " but found " + this.methodNode.desc);
|
||||
}
|
||||
|
||||
InjectorData handler = new InjectorData(target, "handler", false);
|
||||
this.validateParams(handler, this.returnType, this.returnType);
|
||||
|
||||
Extension extraStack = target.extendStack();
|
||||
|
||||
try {
|
||||
int local = this.discriminator.findLocal(context);
|
||||
if (local > -1) {
|
||||
this.inject(context, extraStack, local);
|
||||
this.inject(context, handler, extraStack, local);
|
||||
}
|
||||
} catch (InvalidImplicitDiscriminatorException ex) {
|
||||
if (this.discriminator.printLVT()) {
|
||||
@@ -206,7 +203,7 @@ public class ModifyVariableInjector extends Injector {
|
||||
* @param extraStack stack extension
|
||||
* @param local local variable to capture
|
||||
*/
|
||||
private void inject(final Context context, Extension extraStack, final int local) {
|
||||
private void inject(final Context context, InjectorData handler, Extension extraStack, final int local) {
|
||||
if (!this.isStatic) {
|
||||
context.insns.add(new VarInsnNode(Opcodes.ALOAD, 0));
|
||||
extraStack.add();
|
||||
@@ -214,6 +211,11 @@ public class ModifyVariableInjector extends Injector {
|
||||
|
||||
context.insns.add(new VarInsnNode(this.returnType.getOpcode(Opcodes.ILOAD), local));
|
||||
extraStack.add();
|
||||
|
||||
if (handler.captureTargetArgs > 0) {
|
||||
this.pushArgs(handler.target.arguments, context.insns, handler.target.getArgIndices(), 0, handler.captureTargetArgs, extraStack);
|
||||
}
|
||||
|
||||
this.invokeHandler(context.insns);
|
||||
context.insns.add(new VarInsnNode(this.returnType.getOpcode(Opcodes.ISTORE), local));
|
||||
}
|
||||
|
||||
@@ -717,18 +717,18 @@ public final class Bytecode {
|
||||
* @param args argument types
|
||||
* @return method descriptor without return type
|
||||
*/
|
||||
public static String getDescriptor(Type[] args) {
|
||||
public static String getDescriptor(Type... args) {
|
||||
return "(" + Joiner.on("").join(args) + ")";
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate a method descriptor with the specified types
|
||||
*
|
||||
* @param args argument types
|
||||
* @param returnType return type
|
||||
* @param args argument types
|
||||
*
|
||||
* @return generated method descriptor
|
||||
*/
|
||||
public static String getDescriptor(Type[] args, Type returnType) {
|
||||
public static String getDescriptor(Type returnType, Type... args) {
|
||||
return Bytecode.getDescriptor(args) + returnType.toString();
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user