User: joe
Date: 02 Jan 26 16:50
Revision: 084efa14e328fce0e2b1afd04af79a3743597202
Summary:
Add inspection for shadow parameter names not matching the target class
TeamCity URL: http://ci.mcdev.io:80/viewModification.html?tab=vcsModificationFiles&modId=10374&personal=false
Index: src/main/kotlin/platform/mixin/inspection/shadow/ShadowParameterNameInspection.kt
===================================================================
--- src/main/kotlin/platform/mixin/inspection/shadow/ShadowParameterNameInspection.kt (revision 084efa14e328fce0e2b1afd04af79a3743597202)
+++ src/main/kotlin/platform/mixin/inspection/shadow/ShadowParameterNameInspection.kt (revision 084efa14e328fce0e2b1afd04af79a3743597202)
@@ -0,0 +1,68 @@
+/*
+ * Minecraft Development for IntelliJ
+ *
+ * https://mcdev.io/
+ *
+ * Copyright (C) 2025 minecraft-dev
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published
+ * by the Free Software Foundation, version 3.0 only.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program. If not, see .
+ */
+
+package com.demonwav.mcdev.platform.mixin.inspection.shadow
+
+import com.demonwav.mcdev.platform.mixin.handlers.MixinAnnotationHandler
+import com.demonwav.mcdev.platform.mixin.inspection.MixinInspection
+import com.demonwav.mcdev.platform.mixin.util.MethodTargetMember
+import com.demonwav.mcdev.platform.mixin.util.MixinConstants
+import com.demonwav.mcdev.platform.mixin.util.hasAccess
+import com.demonwav.mcdev.platform.mixin.util.hasNamedLocalVariables
+import com.demonwav.mcdev.util.toJavaIdentifier
+import com.intellij.codeInspection.ProblemsHolder
+import com.intellij.psi.JavaElementVisitor
+import com.intellij.psi.PsiAnnotation
+import com.intellij.psi.PsiMethod
+import com.siyeh.ig.fixes.RenameFix
+import org.objectweb.asm.Opcodes
+
+class ShadowParameterNameInspection : MixinInspection() {
+ override fun getStaticDescription() = "Reports when a parameter name in a shadow method does not match the target method"
+
+ override fun buildVisitor(holder: ProblemsHolder) = object : JavaElementVisitor() {
+ override fun visitAnnotation(annotation: PsiAnnotation) {
+ if (!annotation.hasQualifiedName(MixinConstants.Annotations.SHADOW)) {
+ return
+ }
+
+ val target = MixinAnnotationHandler.resolveTarget(annotation).firstOrNull() as? MethodTargetMember ?: return
+
+ if (!annotation.hasNamedLocalVariables(target.classAndMethod.clazz.name.replace('/', '.'))) {
+ return
+ }
+ val thisLocals = if (target.classAndMethod.method.hasAccess(Opcodes.ACC_STATIC)) 0 else 1
+ val targetLocals = target.classAndMethod.method.localVariables?.drop(thisLocals) ?: return
+
+ val method = annotation.parent?.parent as? PsiMethod ?: return
+ for ((param, targetLocal) in method.parameterList.parameters.zip(targetLocals)) {
+ val correctName = targetLocal?.name?.toJavaIdentifier() ?: continue
+ if (param.name != correctName) {
+ val problemElement = param.nameIdentifier ?: param
+ holder.registerProblem(
+ problemElement,
+ "Parameter name does not match name '$correctName' in target class",
+ RenameFix(correctName)
+ )
+ }
+ }
+ }
+ }
+}
Index: src/main/resources/META-INF/plugin.xml
===================================================================
--- src/main/resources/META-INF/plugin.xml (revision 1d86aa7f7337ec725bb3d6178b7dde3f1024a393)
+++ src/main/resources/META-INF/plugin.xml (revision 084efa14e328fce0e2b1afd04af79a3743597202)
@@ -1435,6 +1435,14 @@
level="WARNING"
hasStaticDescription="true"
implementationClass="com.demonwav.mcdev.platform.mixin.inspection.shadow.ShadowModifiersInspection"/>
+