Skip to content

Commit 6f5a795

Browse files
committed
Actually use InjectorAnnotationHandler.isInsnAllowed. Errors are reported, auto completion is filtered, and invalid injector inspection is disabled if there is an invalid insn. Closes #2290
1 parent 285b72b commit 6f5a795

15 files changed

+130
-10
lines changed

src/main/kotlin/platform/mixin/handlers/InjectorAnnotationHandler.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,10 +164,12 @@ abstract class InjectorAnnotationHandler : MixinAnnotationHandler {
164164
targetMethod: MethodNode,
165165
): List<MethodSignature>?
166166

167-
open fun isInsnAllowed(insn: AbstractInsnNode): Boolean {
167+
open fun isInsnAllowed(insn: AbstractInsnNode, decorations: Map<String, Any?>): Boolean {
168168
return true
169169
}
170170

171+
open val allowedInsnDescription = "all instructions"
172+
171173
override fun createUnresolvedMessage(annotation: PsiAnnotation): String? {
172174
return "Cannot resolve any target instructions in target class"
173175
}

src/main/kotlin/platform/mixin/handlers/ModifyArgHandler.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,12 @@ import org.objectweb.asm.tree.MethodInsnNode
3838
import org.objectweb.asm.tree.MethodNode
3939

4040
class ModifyArgHandler : InjectorAnnotationHandler() {
41-
override fun isInsnAllowed(insn: AbstractInsnNode): Boolean {
41+
override fun isInsnAllowed(insn: AbstractInsnNode, decorations: Map<String, Any?>): Boolean {
4242
return insn is MethodInsnNode
4343
}
4444

45+
override val allowedInsnDescription = "method invocations"
46+
4547
override fun expectedMethodSignature(
4648
annotation: PsiAnnotation,
4749
targetClass: ClassNode,

src/main/kotlin/platform/mixin/handlers/ModifyArgsHandler.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,12 @@ import org.objectweb.asm.tree.MethodInsnNode
3434
import org.objectweb.asm.tree.MethodNode
3535

3636
class ModifyArgsHandler : InjectorAnnotationHandler() {
37-
override fun isInsnAllowed(insn: AbstractInsnNode): Boolean {
37+
override fun isInsnAllowed(insn: AbstractInsnNode, decorations: Map<String, Any?>): Boolean {
3838
return insn is MethodInsnNode
3939
}
4040

41+
override val allowedInsnDescription = "method invocations"
42+
4143
override fun expectedMethodSignature(
4244
annotation: PsiAnnotation,
4345
targetClass: ClassNode,

src/main/kotlin/platform/mixin/handlers/ModifyConstantHandler.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,9 +129,11 @@ class ModifyConstantHandler : InjectorAnnotationHandler() {
129129
}.toList()
130130
}
131131

132-
override fun isInsnAllowed(insn: AbstractInsnNode): Boolean {
132+
override fun isInsnAllowed(insn: AbstractInsnNode, decorations: Map<String, Any?>): Boolean {
133133
return insn.opcode in allowedOpcodes
134134
}
135135

136+
override val allowedInsnDescription = "constants"
137+
136138
override val mixinExtrasExpressionContextType = ExpressionContext.Type.MODIFY_CONSTANT
137139
}

src/main/kotlin/platform/mixin/handlers/RedirectInjectorHandler.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,12 @@ class RedirectInjectorHandler : InjectorAnnotationHandler() {
7878
}
7979
}
8080

81-
override fun isInsnAllowed(insn: AbstractInsnNode): Boolean {
81+
override fun isInsnAllowed(insn: AbstractInsnNode, decorations: Map<String, Any?>): Boolean {
8282
return getRedirectType(insn)?.isInsnAllowed(insn) ?: false
8383
}
8484

85+
override val allowedInsnDescription = "redirect targets (see docs)"
86+
8587
override fun expectedMethodSignature(
8688
annotation: PsiAnnotation,
8789
targetClass: ClassNode,

src/main/kotlin/platform/mixin/handlers/injectionPoint/AtResolver.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020

2121
package com.demonwav.mcdev.platform.mixin.handlers.injectionPoint
2222

23+
import com.demonwav.mcdev.platform.mixin.handlers.InjectorAnnotationHandler
24+
import com.demonwav.mcdev.platform.mixin.handlers.MixinAnnotationHandler
2325
import com.demonwav.mcdev.platform.mixin.handlers.desugar.DesugarContext
2426
import com.demonwav.mcdev.platform.mixin.handlers.desugar.DesugarUtil
2527
import com.demonwav.mcdev.platform.mixin.reference.MixinSelector
@@ -276,6 +278,8 @@ class AtResolver(
276278
val injectionPoint = getInjectionPoint(at) ?: return emptyList()
277279
val targetAttr = at.findAttributeValue("target")
278280
val target = targetAttr?.let { parseMixinSelector(it) }
281+
val injector = findInjectorAnnotation(at)?.let(MixinAnnotationHandler::forMixinAnnotation)
282+
as? InjectorAnnotationHandler
279283

280284
// Collect all possible targets
281285
fun <T : PsiElement> doCollectVariants(injectionPoint: InjectionPoint<T>): List<Any> {
@@ -287,6 +291,7 @@ class AtResolver(
287291

288292
return visitor.visit(targetMethod)
289293
.results
294+
.filter { result -> injector?.isInsnAllowed(result.insn, result.decorations) != false }
290295
.mapNotNull { result ->
291296
injectionPoint.createLookup(getTargetClass(target), result)?.let { completionHandler(it) }
292297
}

src/main/kotlin/platform/mixin/handlers/mixinextras/MixinExtrasInjectorAnnotationHandler.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,9 @@ abstract class MixinExtrasInjectorAnnotationHandler : InjectorAnnotationHandler(
9595

9696
abstract val supportedInstructionTypes: Collection<InstructionType>
9797

98-
open fun extraTargetRestrictions(insn: AbstractInsnNode): Boolean = true
98+
override fun isInsnAllowed(insn: AbstractInsnNode, decorations: Map<String, Any?>): Boolean {
99+
return supportedInstructionTypes.any { it.matches(TargetInsn(insn, decorations)) }
100+
}
99101

100102
abstract fun expectedMethodSignature(
101103
annotation: PsiAnnotation,
@@ -118,7 +120,6 @@ abstract class MixinExtrasInjectorAnnotationHandler : InjectorAnnotationHandler(
118120
val insns = resolveInstructions(annotation, targetClass, targetMethod)
119121
.ifEmpty { return emptyList() }
120122
.map { TargetInsn(it.insn, it.decorations) }
121-
if (insns.any { insn -> supportedInstructionTypes.none { it.matches(insn) } }) return emptyList()
122123
val signatures = insns.map { insn ->
123124
expectedMethodSignature(annotation, targetClass, targetMethod, insn)
124125
}

src/main/kotlin/platform/mixin/handlers/mixinextras/ModifyExpressionValueHandler.kt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,17 @@ class ModifyExpressionValueHandler : MixinExtrasInjectorAnnotationHandler() {
4141
InstructionType.SIMPLE_EXPRESSION, InstructionType.STRING_CONCAT_EXPRESSION
4242
)
4343

44-
override fun extraTargetRestrictions(insn: AbstractInsnNode): Boolean {
44+
override fun isInsnAllowed(insn: AbstractInsnNode, decorations: Map<String, Any?>): Boolean {
45+
if (!super.isInsnAllowed(insn, decorations)) {
46+
return false
47+
}
48+
4549
val returnType = getInsnReturnType(insn) ?: return false
4650
return returnType != Type.VOID_TYPE
4751
}
4852

53+
override val allowedInsnDescription = "instructions that return a value"
54+
4955
override fun expectedMethodSignature(
5056
annotation: PsiAnnotation,
5157
targetClass: ClassNode,

src/main/kotlin/platform/mixin/handlers/mixinextras/ModifyReceiverHandler.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,15 @@ class ModifyReceiverHandler : MixinExtrasInjectorAnnotationHandler() {
3434
InstructionType.METHOD_CALL, InstructionType.FIELD_GET, InstructionType.FIELD_SET
3535
)
3636

37-
override fun extraTargetRestrictions(insn: AbstractInsnNode) = when (insn.opcode) {
37+
override fun isInsnAllowed(insn: AbstractInsnNode, decorations: Map<String, Any?>) = when (insn.opcode) {
3838
Opcodes.INVOKEVIRTUAL, Opcodes.INVOKESPECIAL, Opcodes.INVOKEINTERFACE,
3939
Opcodes.GETFIELD, Opcodes.PUTFIELD -> true
4040

4141
else -> false
4242
}
4343

44+
override val allowedInsnDescription = "non-static method invocations and field references"
45+
4446
override fun expectedMethodSignature(
4547
annotation: PsiAnnotation,
4648
targetClass: ClassNode,

src/main/kotlin/platform/mixin/handlers/mixinextras/ModifyReturnValueHandler.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ import org.objectweb.asm.tree.MethodNode
3232
class ModifyReturnValueHandler : MixinExtrasInjectorAnnotationHandler() {
3333
override val supportedInstructionTypes = listOf(InstructionType.RETURN)
3434

35+
override val allowedInsnDescription = "return instructions"
36+
3537
override fun expectedMethodSignature(
3638
annotation: PsiAnnotation,
3739
targetClass: ClassNode,

0 commit comments

Comments
 (0)