|
| 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 | +} |
0 commit comments