File size: 3,779 Bytes
d517dc9
 
 
 
1
2
3
4
5
{"type": "session", "harness": "opencode", "id": "mc-0029", "name": "InjectionPoint resolution: how @Inject finds its target"}
{"type": "message", "message": {"role": "system", "content": "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."}}
{"type": "message", "message": {"role": "user", "content": "How does @Inject find its target method and injection point? Explain the InjectionPoint resolution mechanism."}}
{"type": "message", "message": {"role": "assistant", "content": "The `@Inject` annotation targets a method and an injection point within that method. The resolution process works as follows:\n\n**Step 1: Method Resolution**\nThe target method is resolved from the `@Mixin` target class using the method name and descriptor:\n\n```java\n// @Inject(method = \"tick\", at = @At(\"HEAD\"))\n// Resolution: find MethodNode with name \"tick\" and matching descriptor in target class\n\nClassNode targetNode = ...;\nMethodNode targetMethod = null;\nfor (MethodNode mn : targetNode.methods) {\n    if (mn.name.equals(\"tick\") && mn.desc.equals(\"()V\")) {\n        targetMethod = mn;\n        break;\n    }\n}\n```\n\n**Step 2: InjectionPoint Resolution**\nThe `@At` annotation specifies where to inject. The `InjectionPoint` class hierarchy resolves this:\n\n```java\npublic abstract class InjectionPoint {\n    public abstract boolean find(String desc, InsnList insns, List<AbstractInsnNode> nodes);\n    \n    // HEAD - first instruction\n    public static class AtHead extends InjectionPoint {\n        public boolean find(String desc, InsnList insns, List<AbstractInsnNode> nodes) {\n            nodes.add(insns.getFirst());\n            return true;\n        }\n    }\n    \n    // RETURN - all return instructions\n    public static class AtReturn extends InjectionPoint {\n        public boolean find(String desc, InsnList insns, List<AbstractInsnNode> nodes) {\n            for (AbstractInsnNode insn : insns) {\n                if (insn instanceof InsnNode && isReturnOpcode(insn.getOpcode())) {\n                    nodes.add(insn);\n                }\n            }\n            return !nodes.isEmpty();\n        }\n    }\n    \n    // INVOKE - specific method invocation\n    public static class AtInvoke extends InjectionPoint {\n        private final String targetMethod;\n        \n        public boolean find(String desc, InsnList insns, List<AbstractInsnNode> nodes) {\n            for (AbstractInsnNode insn : insns) {\n                if (insn instanceof MethodInsnNode) {\n                    MethodInsnNode min = (MethodInsnNode) insn;\n                    if (min.name.equals(targetMethod)) {\n                        nodes.add(min);\n                    }\n                }\n            }\n            return !nodes.isEmpty();\n        }\n    }\n}\n```\n\n**Step 3: Injectivity and Slice**\nThe `@At` can be combined with `@Slice` to narrow injection points:\n\n```java\n// Inject before the FIRST call to \"playSound\" in the method\n@Inject(\n    method = \"tick\",\n    at = @At(\n        value = \"INVOKE\",\n        target = \"Lnet/minecraft/world/World;playSound(...)V\",\n        ordinal = 0\n    )\n)\n\n// With slice - only inject in the first half of the method\n@Inject(\n    method = \"tick\",\n    at = @At(\n        value = \"INVOKE\",\n        target = \"Lnet/minecraft/world/World;playSound(...)V\"\n    ),\n    slice = @Slice(\n        from = @At(value = \"HEAD\"),\n        to = @At(value = \"INVOKE\", target = \"...\", ordinal = 2)\n    )\n)\n```\n\nThe `ordinal` parameter selects which occurrence (0-indexed) of the matched instruction to inject at. Without it, all occurrences are injection targets."}}