User: joe Date: 23 Jan 24 00:39 Revision: ab985497ef6095c7c2dcb11a253606fa6da72e96 Summary: Add inspection for CTOR_HEAD missing unsafe = true TeamCity URL: https://ci.mcdev.io/viewModification.html?tab=vcsModificationFiles&modId=9031&personal=false Index: src/main/kotlin/platform/mixin/inspection/injector/CtorHeadNoUnsafeInspection.kt =================================================================== --- src/main/kotlin/platform/mixin/inspection/injector/CtorHeadNoUnsafeInspection.kt (revision ab985497ef6095c7c2dcb11a253606fa6da72e96) +++ src/main/kotlin/platform/mixin/inspection/injector/CtorHeadNoUnsafeInspection.kt (revision ab985497ef6095c7c2dcb11a253606fa6da72e96) @@ -0,0 +1,84 @@ +/* + * Minecraft Development for IntelliJ + * + * https://mcdev.io/ + * + * Copyright (C) 2024 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.injector + +import com.demonwav.mcdev.facet.MinecraftFacet +import com.demonwav.mcdev.platform.fabric.FabricModuleType +import com.demonwav.mcdev.platform.mixin.inspection.MixinInspection +import com.demonwav.mcdev.platform.mixin.util.MixinConstants +import com.demonwav.mcdev.util.constantStringValue +import com.demonwav.mcdev.util.constantValue +import com.demonwav.mcdev.util.findModule +import com.intellij.codeInspection.ProblemsHolder +import com.intellij.psi.JavaElementVisitor +import com.intellij.psi.PsiAnnotation +import com.intellij.psi.PsiElementVisitor +import java.awt.FlowLayout +import javax.swing.JCheckBox +import javax.swing.JComponent +import javax.swing.JPanel + +class CtorHeadNoUnsafeInspection : MixinInspection() { + @JvmField + var ignoreForFabric = true + + override fun createOptionsPanel(): JComponent { + val panel = JPanel(FlowLayout(FlowLayout.LEFT)) + val checkbox = JCheckBox("Ignore in Fabric mods", ignoreForFabric) + checkbox.addActionListener { + ignoreForFabric = checkbox.isSelected + } + panel.add(checkbox) + return panel + } + + override fun getStaticDescription() = "Reports when @At(\"CTOR_HEAD\") is missing unsafe" + + override fun buildVisitor(holder: ProblemsHolder): PsiElementVisitor { + if (ignoreForFabric) { + val isFabric = holder.file.findModule()?.let { MinecraftFacet.getInstance(it) }?.isOfType(FabricModuleType) + ?: false + if (isFabric) { + return PsiElementVisitor.EMPTY_VISITOR + } + } + + return object : JavaElementVisitor() { + override fun visitAnnotation(annotation: PsiAnnotation) { + if (!annotation.hasQualifiedName(MixinConstants.Annotations.AT)) { + return + } + val valueElement = annotation.findDeclaredAttributeValue("value") + if (valueElement?.constantStringValue != "CTOR_HEAD") { + return + } + if (annotation.findDeclaredAttributeValue("unsafe")?.constantValue == true) { + return + } + holder.registerProblem( + valueElement, + "CTOR_HEAD is missing unsafe = true", + InjectIntoConstructorInspection.AddUnsafeFix(annotation), + ) + } + } + } +} Index: src/main/kotlin/platform/mixin/inspection/injector/InjectIntoConstructorInspection.kt =================================================================== --- src/main/kotlin/platform/mixin/inspection/injector/InjectIntoConstructorInspection.kt (revision 53c3cca60486df0c2e0afe88f31ed99f0c825f58) +++ src/main/kotlin/platform/mixin/inspection/injector/InjectIntoConstructorInspection.kt (revision ab985497ef6095c7c2dcb11a253606fa6da72e96) @@ -131,7 +131,7 @@ override fun getStaticDescription() = "@Inject into Constructor" - private class AddUnsafeFix(at: PsiAnnotation) : LocalQuickFixOnPsiElement(at) { + class AddUnsafeFix(at: PsiAnnotation) : LocalQuickFixOnPsiElement(at) { override fun getFamilyName() = "Add unsafe = true" override fun getText() = "Add unsafe = true" Index: src/main/resources/META-INF/plugin.xml =================================================================== --- src/main/resources/META-INF/plugin.xml (revision 53c3cca60486df0c2e0afe88f31ed99f0c825f58) +++ src/main/resources/META-INF/plugin.xml (revision ab985497ef6095c7c2dcb11a253606fa6da72e96) @@ -888,6 +888,14 @@ level="ERROR" hasStaticDescription="true" implementationClass="com.demonwav.mcdev.platform.mixin.inspection.injector.CancellableBeforeSuperCallInspection"/> +