SystemYou are an expert SpongePowered Mixin internals developer. You understand how mixins work at the bytecode level using ASM, and can debug, write, and optimize mixins.
You are an expert SpongePowered Mixin internals developer. You understand how mixins work at the bytecode level using ASM, and can debug, write, and optimize mixins.
User
How does @Inject find its target method and injection point? Explain the InjectionPoint resolution mechanism.
Assistant
The @Inject annotation targets a method and an injection point within that method. The resolution process works as follows:
Step 1: Method Resolution The target method is resolved from the @Mixin target class using the method name and descriptor:
java
// @Inject(method = "tick", at = @At("HEAD"))// Resolution: find MethodNode with name "tick" and matching descriptor in target classClassNodetargetNode= ...;
MethodNodetargetMethod=null;
for (MethodNode mn : targetNode.methods) {
if (mn.name.equals("tick") && mn.desc.equals("()V")) {
targetMethod = mn;
break;
}
}
Step 2: InjectionPoint Resolution The @At annotation specifies where to inject. The InjectionPoint class hierarchy resolves this:
Step 3: Injectivity and Slice The @At can be combined with @Slice to narrow injection points:
java
// Inject before the FIRST call to "playSound" in the method@Inject( method = "tick", at = @At( value = "INVOKE", target = "Lnet/minecraft/world/World;playSound(...)V", ordinal = 0 ))// With slice - only inject in the first half of the method@Inject( method = "tick", at = @At( value = "INVOKE", target = "Lnet/minecraft/world/World;playSound(...)V" ), slice = @Slice( from = @At(value = "HEAD"), to = @At(value = "INVOKE", target = "...", ordinal = 2) ))
The ordinal parameter selects which occurrence (0-indexed) of the matched instruction to inject at. Without it, all occurrences are injection targets.