Skip to content

Commit 084efa1

Browse files
committed
Add inspection for shadow parameter names not matching the target class
1 parent 1d86aa7 commit 084efa1

File tree

2 files changed

+76
-0
lines changed

2 files changed

+76
-0
lines changed
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
* Minecraft Development for IntelliJ
3+
*
4+
* https://mcdev.io/
5+
*
6+
* Copyright (C) 2025 minecraft-dev
7+
*
8+
* This program is free software: you can redistribute it and/or modify
9+
* it under the terms of the GNU Lesser General Public License as published
10+
* by the Free Software Foundation, version 3.0 only.
11+
*
12+
* This program is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU Lesser General Public License
18+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
19+
*/
20+
21+
package com.demonwav.mcdev.platform.mixin.inspection.shadow
22+
23+
import com.demonwav.mcdev.platform.mixin.handlers.MixinAnnotationHandler
24+
import com.demonwav.mcdev.platform.mixin.inspection.MixinInspection
25+
import com.demonwav.mcdev.platform.mixin.util.MethodTargetMember
26+
import com.demonwav.mcdev.platform.mixin.util.MixinConstants
27+
import com.demonwav.mcdev.platform.mixin.util.hasAccess
28+
import com.demonwav.mcdev.platform.mixin.util.hasNamedLocalVariables
29+
import com.demonwav.mcdev.util.toJavaIdentifier
30+
import com.intellij.codeInspection.ProblemsHolder
31+
import com.intellij.psi.JavaElementVisitor
32+
import com.intellij.psi.PsiAnnotation
33+
import com.intellij.psi.PsiMethod
34+
import com.siyeh.ig.fixes.RenameFix
35+
import org.objectweb.asm.Opcodes
36+
37+
class ShadowParameterNameInspection : MixinInspection() {
38+
override fun getStaticDescription() = "Reports when a parameter name in a shadow method does not match the target method"
39+
40+
override fun buildVisitor(holder: ProblemsHolder) = object : JavaElementVisitor() {
41+
override fun visitAnnotation(annotation: PsiAnnotation) {
42+
if (!annotation.hasQualifiedName(MixinConstants.Annotations.SHADOW)) {
43+
return
44+
}
45+
46+
val target = MixinAnnotationHandler.resolveTarget(annotation).firstOrNull() as? MethodTargetMember ?: return
47+
48+
if (!annotation.hasNamedLocalVariables(target.classAndMethod.clazz.name.replace('/', '.'))) {
49+
return
50+
}
51+
val thisLocals = if (target.classAndMethod.method.hasAccess(Opcodes.ACC_STATIC)) 0 else 1
52+
val targetLocals = target.classAndMethod.method.localVariables?.drop(thisLocals) ?: return
53+
54+
val method = annotation.parent?.parent as? PsiMethod ?: return
55+
for ((param, targetLocal) in method.parameterList.parameters.zip(targetLocals)) {
56+
val correctName = targetLocal?.name?.toJavaIdentifier() ?: continue
57+
if (param.name != correctName) {
58+
val problemElement = param.nameIdentifier ?: param
59+
holder.registerProblem(
60+
problemElement,
61+
"Parameter name does not match name '$correctName' in target class",
62+
RenameFix(correctName)
63+
)
64+
}
65+
}
66+
}
67+
}
68+
}

src/main/resources/META-INF/plugin.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1435,6 +1435,14 @@
14351435
level="WARNING"
14361436
hasStaticDescription="true"
14371437
implementationClass="com.demonwav.mcdev.platform.mixin.inspection.shadow.ShadowModifiersInspection"/>
1438+
<localInspection displayName="Shadow parameter names don't match the target class"
1439+
shortName="ShadowNameDoesntMatchTargetClass"
1440+
groupName="Mixin"
1441+
language="JAVA"
1442+
enabledByDefault="true"
1443+
level="WEAK WARNING"
1444+
hasStaticDescription="true"
1445+
implementationClass="com.demonwav.mcdev.platform.mixin.inspection.shadow.ShadowParameterNameInspection"/>
14381446
<localInspection displayName="@Shadow field with prefix"
14391447
shortName="ShadowFieldPrefix"
14401448
groupName="Mixin"

0 commit comments

Comments
 (0)