Class Injector
- java.lang.Object
-
- org.spongepowered.asm.mixin.injection.code.Injector
-
- Direct Known Subclasses:
CallbackInjector,InvokeInjector,ModifyVariableInjector
public abstract class Injector extends java.lang.ObjectBase class for bytecode injectors
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classInjector.InjectorDataRedirection data bundle base.static classInjector.TargetNodeA nominated target node
-
Field Summary
Fields Modifier and Type Field Description protected java.lang.StringannotationTypeAnnotation type, for use in informational errorsprotected org.objectweb.asm.tree.ClassNodeclassNodeClass nodeprotected InjectionInfoinfoInjection infoprotected booleanisInterfaceTrue if the callback method is in an interfaceprotected booleanisStaticTrue if the callback method is staticprotected static ILoggerloggerLog more thingsprotected org.objectweb.asm.Type[]methodArgsArguments of the handler methodprotected org.objectweb.asm.tree.MethodNodemethodNodeCallback methodprotected org.objectweb.asm.TypereturnTypeReturn type of the handler method
-
Constructor Summary
Constructors Constructor Description Injector(InjectionInfo info, java.lang.String annotationType)Make a new CallbackInjector for the supplied InjectionInfo
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description protected voidaddTargetNode(InjectorTarget injectorTarget, java.util.List<InjectionNodes.InjectionNode> myNodes, org.objectweb.asm.tree.AbstractInsnNode node, java.util.Set<InjectionPoint> nominators)protected voidaddTargetNode(InjectorTarget target, java.util.Map<java.lang.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 booleancanCoerce(java.lang.String from, java.lang.String to)Returns 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 booleancheckCoerce(int index, org.objectweb.asm.Type toType, java.lang.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 voidcheckTargetModifiers(Target target, boolean exactMatch)Check that the static modifier of the target method matches the handlerjava.util.List<InjectionNodes.InjectionNode>find(InjectorTarget injectorTarget, java.util.List<InjectionPoint> injectionPoints)...protected booleanfindTargetNodes(InjectorTarget target, InjectionPoint injectionPoint, java.util.Collection<org.objectweb.asm.tree.AbstractInsnNode> nodes)voidinject(Target target, java.util.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)voidpreInject(Target target, java.util.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, java.util.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, java.lang.String exceptionType, java.lang.String message)Throw an exception.java.lang.StringtoString()protected 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 Detail
-
logger
protected static final ILogger logger
Log more things
-
info
protected InjectionInfo info
Injection info
-
annotationType
protected final java.lang.String annotationType
Annotation type, for use in informational errors
-
classNode
protected final org.objectweb.asm.tree.ClassNode classNode
Class node
-
methodNode
protected final org.objectweb.asm.tree.MethodNode methodNode
Callback method
-
methodArgs
protected final org.objectweb.asm.Type[] methodArgs
Arguments of the handler method
-
returnType
protected final org.objectweb.asm.Type returnType
Return type of the handler method
-
isStatic
protected final boolean isStatic
True if the callback method is static
-
isInterface
protected final boolean isInterface
True if the callback method is in an interface
-
-
Constructor Detail
-
Injector
public Injector(InjectionInfo info, java.lang.String annotationType)
Make a new CallbackInjector for the supplied InjectionInfo- Parameters:
info- Information about this injection
-
-
Method Detail
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
find
public final java.util.List<InjectionNodes.InjectionNode> find(InjectorTarget injectorTarget, java.util.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, java.util.List<InjectionNodes.InjectionNode> myNodes, org.objectweb.asm.tree.AbstractInsnNode node, java.util.Set<InjectionPoint> nominators)
-
preInject
public final void preInject(Target target, java.util.List<InjectionNodes.InjectionNode> nodes)
Performs pre-injection checks and tasks on the specified target- Parameters:
target- target potentially being injected intonodes- selected target nodes
-
inject
public final void inject(Target target, java.util.List<InjectionNodes.InjectionNode> nodes)
Performs the injection on the specified target- Parameters:
target- target to inject intonodes- selected nodes
-
addTargetNode
protected void addTargetNode(InjectorTarget target, java.util.Map<java.lang.Integer,Injector.TargetNode> targetNodes, InjectionPoint injectionPoint, org.objectweb.asm.tree.AbstractInsnNode insn)
-
findTargetNodes
protected boolean findTargetNodes(InjectorTarget target, InjectionPoint injectionPoint, java.util.Collection<org.objectweb.asm.tree.AbstractInsnNode> nodes)
-
sanityCheck
protected void sanityCheck(Target target, java.util.List<InjectionPoint> injectionPoints)
-
checkTargetModifiers
protected final void checkTargetModifiers(Target target, boolean exactMatch)
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
protected void preInject(Target target, InjectionNodes.InjectionNode node)
-
inject
protected abstract void inject(Target target, InjectionNodes.InjectionNode node)
-
postInject
protected void postInject(Target target, InjectionNodes.InjectionNode node)
-
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, java.lang.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, java.lang.String exceptionType, java.lang.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
public static boolean canCoerce(java.lang.String from, java.lang.String 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
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
-
-