|
| 1 | +/* |
| 2 | + * Minecraft Development for IntelliJ |
| 3 | + * |
| 4 | + * https://mcdev.io/ |
| 5 | + * |
| 6 | + * Copyright (C) 2024 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.injector |
| 22 | + |
| 23 | +import com.demonwav.mcdev.facet.MinecraftFacet |
| 24 | +import com.demonwav.mcdev.platform.fabric.FabricModuleType |
| 25 | +import com.demonwav.mcdev.platform.mixin.inspection.MixinInspection |
| 26 | +import com.demonwav.mcdev.platform.mixin.util.MixinConstants |
| 27 | +import com.demonwav.mcdev.util.constantStringValue |
| 28 | +import com.demonwav.mcdev.util.constantValue |
| 29 | +import com.demonwav.mcdev.util.findModule |
| 30 | +import com.intellij.codeInspection.ProblemsHolder |
| 31 | +import com.intellij.psi.JavaElementVisitor |
| 32 | +import com.intellij.psi.PsiAnnotation |
| 33 | +import com.intellij.psi.PsiElementVisitor |
| 34 | +import java.awt.FlowLayout |
| 35 | +import javax.swing.JCheckBox |
| 36 | +import javax.swing.JComponent |
| 37 | +import javax.swing.JPanel |
| 38 | + |
| 39 | +class CtorHeadNoUnsafeInspection : MixinInspection() { |
| 40 | + @JvmField |
| 41 | + var ignoreForFabric = true |
| 42 | + |
| 43 | + override fun createOptionsPanel(): JComponent { |
| 44 | + val panel = JPanel(FlowLayout(FlowLayout.LEFT)) |
| 45 | + val checkbox = JCheckBox("Ignore in Fabric mods", ignoreForFabric) |
| 46 | + checkbox.addActionListener { |
| 47 | + ignoreForFabric = checkbox.isSelected |
| 48 | + } |
| 49 | + panel.add(checkbox) |
| 50 | + return panel |
| 51 | + } |
| 52 | + |
| 53 | + override fun getStaticDescription() = "Reports when @At(\"CTOR_HEAD\") is missing unsafe" |
| 54 | + |
| 55 | + override fun buildVisitor(holder: ProblemsHolder): PsiElementVisitor { |
| 56 | + if (ignoreForFabric) { |
| 57 | + val isFabric = holder.file.findModule()?.let { MinecraftFacet.getInstance(it) }?.isOfType(FabricModuleType) |
| 58 | + ?: false |
| 59 | + if (isFabric) { |
| 60 | + return PsiElementVisitor.EMPTY_VISITOR |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + return object : JavaElementVisitor() { |
| 65 | + override fun visitAnnotation(annotation: PsiAnnotation) { |
| 66 | + if (!annotation.hasQualifiedName(MixinConstants.Annotations.AT)) { |
| 67 | + return |
| 68 | + } |
| 69 | + val valueElement = annotation.findDeclaredAttributeValue("value") |
| 70 | + if (valueElement?.constantStringValue != "CTOR_HEAD") { |
| 71 | + return |
| 72 | + } |
| 73 | + if (annotation.findDeclaredAttributeValue("unsafe")?.constantValue == true) { |
| 74 | + return |
| 75 | + } |
| 76 | + holder.registerProblem( |
| 77 | + valueElement, |
| 78 | + "CTOR_HEAD is missing unsafe = true", |
| 79 | + InjectIntoConstructorInspection.AddUnsafeFix(annotation), |
| 80 | + ) |
| 81 | + } |
| 82 | + } |
| 83 | + } |
| 84 | +} |
0 commit comments