Class Injector
java.lang.Object
org.spongepowered.asm.mixin.injection.code.Injector
- Direct Known Subclasses:
CallbackInjector,InvokeInjector,ModifyVariableInjector
Base class for bytecode injectors
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classRedirection data bundle base.static final classA nominated target node -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected final StringAnnotation type, for use in informational errorsprotected final org.objectweb.asm.tree.ClassNodeClass nodeprotected InjectionInfoInjection infoprotected final booleanTrue if the callback method is in an interfaceprotected final booleanTrue if the callback method is staticprotected static final ILoggerLog more thingsprotected final org.objectweb.asm.Type[]Arguments of the handler methodprotected final org.objectweb.asm.tree.MethodNodeCallback methodprotected final org.objectweb.asm.TypeReturn type of the handler method -
Constructor Summary
ConstructorsConstructorDescriptionInjector(InjectionInfo info, String annotationType) Make a new CallbackInjector for the supplied InjectionInfo -
Method Summary
Modifier and TypeMethodDescriptionprotected voidaddTargetNode(InjectorTarget injectorTarget, List<InjectionNodes.InjectionNode> myNodes, org.objectweb.asm.tree.AbstractInsnNode node, Set<InjectionPoint> nominators) protected voidaddTargetNode(InjectorTarget target, Map<Integer, Injector.TargetNode> targetNodes, InjectionPoint injectionPoint, org.objectweb.asm.tree.AbstractInsnNode insn) static booleancanCoerce(char from, char to) Returns whether the from type can be coerced to the to type.static booleanReturns whether the from type can be coerced to the to type.static booleancanCoerce(org.objectweb.asm.Type from, org.objectweb.asm.Type to) Returns whether the from type can be coerced to the to type.protected final booleancheckCoerce(int index, org.objectweb.asm.Type toType, String description, boolean allowCoercion) Called insidevalidateParams(org.spongepowered.asm.mixin.injection.code.Injector.InjectorData, org.objectweb.asm.Type, org.objectweb.asm.Type...)but can also be used directly.protected voidcheckTargetForNode(Target target, InjectionNodes.InjectionNode node, InjectionPoint.RestrictTargetLevel targetLevel) The normal staticness check is not location-aware, in that it merely enforces static modifiers of handlers to match their targets.protected final voidcheckTargetModifiers(Target target, boolean exactMatch) Check that the static modifier of the target method matches the handlerfinal List<InjectionNodes.InjectionNode> find(InjectorTarget injectorTarget, List<InjectionPoint> injectionPoints) ...protected booleanfindTargetNodes(InjectorTarget target, InjectionPoint injectionPoint, Collection<org.objectweb.asm.tree.AbstractInsnNode> nodes) final voidinject(Target target, List<InjectionNodes.InjectionNode> nodes) Performs the injection on the specified targetprotected abstract voidinject(Target target, InjectionNodes.InjectionNode node) protected org.objectweb.asm.tree.AbstractInsnNodeinvokeHandler(org.objectweb.asm.tree.InsnList insns) Invoke the handler methodprotected org.objectweb.asm.tree.AbstractInsnNodeinvokeHandler(org.objectweb.asm.tree.InsnList insns, org.objectweb.asm.tree.MethodNode handler) Invoke a handler methodprotected org.objectweb.asm.tree.AbstractInsnNodeinvokeHandlerWithArgs(org.objectweb.asm.Type[] args, org.objectweb.asm.tree.InsnList insns, int[] argMap) protected org.objectweb.asm.tree.AbstractInsnNodeinvokeHandlerWithArgs(org.objectweb.asm.Type[] args, org.objectweb.asm.tree.InsnList insns, int[] argMap, int startArg, int endArg) protected voidpostInject(Target target, InjectionNodes.InjectionNode node) final voidpreInject(Target target, List<InjectionNodes.InjectionNode> nodes) Performs pre-injection checks and tasks on the specified targetprotected voidpreInject(Target target, InjectionNodes.InjectionNode node) protected voidpushArgs(org.objectweb.asm.Type[] args, org.objectweb.asm.tree.InsnList insns, int[] argMap, int start, int end) Load args onto the stack from their positions allocated in argMapprotected voidpushArgs(org.objectweb.asm.Type[] args, org.objectweb.asm.tree.InsnList insns, int[] argMap, int start, int end, Target.Extension extension) Load args onto the stack from their positions allocated in argMapprotected voidsanityCheck(Target target, List<InjectionPoint> injectionPoints) protected int[]storeArgs(Target target, org.objectweb.asm.Type[] args, org.objectweb.asm.tree.InsnList insns, int start) Store args on the stack starting at the end and working back to position specified by start, return the generated argMapprotected voidstoreArgs(Target target, org.objectweb.asm.Type[] args, org.objectweb.asm.tree.InsnList insns, int[] argMap, int start, int end) Store args on the stack to their positions allocated based on argMapprotected voidstoreArgs(Target target, org.objectweb.asm.Type[] args, org.objectweb.asm.tree.InsnList insns, int[] argMap, int start, int end, org.objectweb.asm.tree.LabelNode from, org.objectweb.asm.tree.LabelNode to) Store args on the stack to their positions allocated based on argMapprotected int[]storeArgs(Target target, org.objectweb.asm.Type[] args, org.objectweb.asm.tree.InsnList insns, int start, org.objectweb.asm.tree.LabelNode from, org.objectweb.asm.tree.LabelNode to) Store args on the stack starting at the end and working back to position specified by start, return the generated argMapprotected voidthrowException(org.objectweb.asm.tree.InsnList insns, Target.Extension extraStack, String exceptionType, String message) Throw an exception.toString()protected final voidvalidateParams(Injector.InjectorData injector, org.objectweb.asm.Type returnType, org.objectweb.asm.Type... args) 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.
-
Field Details
-
logger
Log more things -
info
Injection info -
annotationType
Annotation type, for use in informational errors -
classNode
protected final org.objectweb.asm.tree.ClassNode classNodeClass node -
methodNode
protected final org.objectweb.asm.tree.MethodNode methodNodeCallback method -
methodArgs
protected final org.objectweb.asm.Type[] methodArgsArguments of the handler method -
returnType
protected final org.objectweb.asm.Type returnTypeReturn type of the handler method -
isStatic
protected final boolean isStaticTrue if the callback method is static -
isInterface
protected final boolean isInterfaceTrue if the callback method is in an interface
-
-
Constructor Details
-
Injector
Make a new CallbackInjector for the supplied InjectionInfo- Parameters:
info- Information about this injection
-
-
Method Details
-
toString
-
find
public final List<InjectionNodes.InjectionNode> find(InjectorTarget injectorTarget, List<InjectionPoint> injectionPoints) ...- Parameters:
injectorTarget- Target method to inject intoinjectionPoints- InjectionPoint instances which will identify target insns in the target method- Returns:
- discovered injection points
-
addTargetNode
protected void addTargetNode(InjectorTarget injectorTarget, List<InjectionNodes.InjectionNode> myNodes, org.objectweb.asm.tree.AbstractInsnNode node, Set<InjectionPoint> nominators) -
preInject
Performs pre-injection checks and tasks on the specified target- Parameters:
target- target potentially being injected intonodes- selected target nodes
-
inject
Performs the injection on the specified target- Parameters:
target- target to inject intonodes- selected nodes
-
addTargetNode
protected void addTargetNode(InjectorTarget target, Map<Integer, Injector.TargetNode> targetNodes, InjectionPoint injectionPoint, org.objectweb.asm.tree.AbstractInsnNode insn) -
findTargetNodes
protected boolean findTargetNodes(InjectorTarget target, InjectionPoint injectionPoint, Collection<org.objectweb.asm.tree.AbstractInsnNode> nodes) -
sanityCheck
-
checkTargetModifiers
Check that the static modifier of the target method matches the handler- Parameters:
target- Target to checkexactMatch- True if static must match, false to only check if an instance handler is targetting a static method
-
checkTargetForNode
protected void checkTargetForNode(Target target, InjectionNodes.InjectionNode node, InjectionPoint.RestrictTargetLevel targetLevel) The normal staticness check is not location-aware, in that it merely enforces static modifiers of handlers to match their targets. For injecting into constructors however (which are ostensibly instance methods) calls which are injected before the call to super() cannot access this and must therefore be declared as static.- Parameters:
target- Target methodnode- Injection location
-
preInject
-
inject
-
postInject
-
invokeHandler
protected org.objectweb.asm.tree.AbstractInsnNode invokeHandler(org.objectweb.asm.tree.InsnList insns) Invoke the handler method- Parameters:
insns- Instruction list to inject into- Returns:
- injected insn node
-
invokeHandler
protected org.objectweb.asm.tree.AbstractInsnNode invokeHandler(org.objectweb.asm.tree.InsnList insns, org.objectweb.asm.tree.MethodNode handler) Invoke a handler method- Parameters:
insns- Instruction list to inject intohandler- Actual method to invoke (may be different if using a surrogate)- Returns:
- injected insn node
-
invokeHandlerWithArgs
protected org.objectweb.asm.tree.AbstractInsnNode invokeHandlerWithArgs(org.objectweb.asm.Type[] args, org.objectweb.asm.tree.InsnList insns, int[] argMap) - Parameters:
args- handler argumentsinsns- InsnList to inject insns intoargMap- Mapping of args to local variables- Returns:
- injected insn node
-
invokeHandlerWithArgs
protected org.objectweb.asm.tree.AbstractInsnNode invokeHandlerWithArgs(org.objectweb.asm.Type[] args, org.objectweb.asm.tree.InsnList insns, int[] argMap, int startArg, int endArg) - Parameters:
args- handler argumentsinsns- InsnList to inject insns intoargMap- Mapping of args to local variablesstartArg- Starting arg to consumeendArg- Ending arg to consume- Returns:
- injected insn node
-
storeArgs
protected int[] storeArgs(Target target, org.objectweb.asm.Type[] args, org.objectweb.asm.tree.InsnList insns, int start) Store args on the stack starting at the end and working back to position specified by start, return the generated argMap- Parameters:
target- target methodargs- argument typesinsns- instruction list to generate insns intostart- Starting index- Returns:
- the generated argmap
-
storeArgs
protected int[] storeArgs(Target target, org.objectweb.asm.Type[] args, org.objectweb.asm.tree.InsnList insns, int start, org.objectweb.asm.tree.LabelNode from, org.objectweb.asm.tree.LabelNode to) Store args on the stack starting at the end and working back to position specified by start, return the generated argMap- Parameters:
target- target methodargs- argument typesinsns- instruction list to generate insns intostart- Starting indexfrom- The label marking the start of the region for the localsto- The label marking the end of the region for the stored locals- Returns:
- the generated argmap
-
storeArgs
protected void storeArgs(Target target, org.objectweb.asm.Type[] args, org.objectweb.asm.tree.InsnList insns, int[] argMap, int start, int end) Store args on the stack to their positions allocated based on argMap- Parameters:
args- argument typesinsns- instruction list to generate insns intoargMap- generated argmap containing local indices for all argsstart- Starting indexend- Ending index
-
storeArgs
protected void storeArgs(Target target, org.objectweb.asm.Type[] args, org.objectweb.asm.tree.InsnList insns, int[] argMap, int start, int end, org.objectweb.asm.tree.LabelNode from, org.objectweb.asm.tree.LabelNode to) Store args on the stack to their positions allocated based on argMap- Parameters:
args- argument typesinsns- instruction list to generate insns intoargMap- generated argmap containing local indices for all argsstart- Starting indexend- Ending indexfrom- The label marking the start of the region for the localsto- The label marking the end of the region for the stored locals
-
pushArgs
protected void pushArgs(org.objectweb.asm.Type[] args, org.objectweb.asm.tree.InsnList insns, int[] argMap, int start, int end) Load args onto the stack from their positions allocated in argMap- Parameters:
args- argument typesinsns- instruction list to generate insns intoargMap- generated argmap containing local indices for all argsstart- Starting indexend- Ending index
-
pushArgs
protected void pushArgs(org.objectweb.asm.Type[] args, org.objectweb.asm.tree.InsnList insns, int[] argMap, int start, int end, Target.Extension extension) Load args onto the stack from their positions allocated in argMap- Parameters:
args- argument typesinsns- instruction list to generate insns intoargMap- generated argmap containing local indices for all argsstart- Starting indexend- Ending index
-
validateParams
protected final void validateParams(Injector.InjectorData injector, org.objectweb.asm.Type returnType, org.objectweb.asm.Type... args) 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.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 allowCoerceArgs on the redirect instance determines whether coercion is supported for the base args and return type, coercion is always allowed for captured target args.
Following validation, the captureTargetArgs and coerceReturnType values will be set on the bundle and the calling injector function should adjust itself accordingly.
- Parameters:
injector- Data bundle for the injectorreturnType- Return type for the handler, must not be nullargs- Array of handler args, must not be null
-
checkCoerce
protected final boolean checkCoerce(int index, org.objectweb.asm.Type toType, String description, boolean allowCoercion) Called insidevalidateParams(org.spongepowered.asm.mixin.injection.code.Injector.InjectorData, org.objectweb.asm.Type, org.objectweb.asm.Type...)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.- Parameters:
index- Handler argument index, pass in a negative value (by convention -1) to specify handler return typetoType- Desired type based on the injector contractdescription- human-readable description of the handler method, used in raised exceptionallowCoercion- True if coercion logic can be applied to this argument, false to only allow a precise match- Returns:
- false if the argument matched perfectly, true if coercion is required for the argument
-
throwException
protected void throwException(org.objectweb.asm.tree.InsnList insns, Target.Extension extraStack, String exceptionType, String message) Throw an exception. The exception class must have a string which takes a string argument- Parameters:
insns- Insn list to inject intoexceptionType- Type of exception to throw (binary name)message- Message to pass to the exception constructor
-
canCoerce
public static boolean canCoerce(org.objectweb.asm.Type from, org.objectweb.asm.Type to) Returns whether the from type can be coerced to the to type.- Parameters:
from- type to coerce fromto- type to coerce to- Returns:
- true if from can be coerced to to
-
canCoerce
Returns whether the from type can be coerced to the to type.- Parameters:
from- type to coerce fromto- type to coerce to- Returns:
- true if from can be coerced to to
-
canCoerce
public static boolean canCoerce(char from, char to) Returns whether the from type can be coerced to the to type.- Parameters:
from- type to coerce fromto- type to coerce to- Returns:
- true if from can be coerced to to
-