⁠
rednesto: 2024.1 EAP
- /*
- * 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 <https://www.gnu.org/licenses/>.
- */
- package com.demonwav.mcdev.platform.sponge.inspection
- import com.demonwav.mcdev.platform.sponge.SpongeModuleType
- import com.demonwav.mcdev.platform.sponge.util.SpongeConstants
- import com.demonwav.mcdev.platform.sponge.util.isSpongePluginClass
- import com.demonwav.mcdev.util.constantStringValue
- import com.intellij.codeInsight.daemon.impl.quickfix.AddDefaultConstructorFix
- import com.intellij.codeInsight.daemon.impl.quickfix.ModifierFix
- import com.intellij.codeInsight.intention.QuickFixFactory
- import com.intellij.codeInspection.AbstractBaseJavaLocalInspectionTool
- import com.intellij.codeInspection.InspectionManager
- import com.intellij.codeInspection.LocalQuickFix
- import com.intellij.codeInspection.ProblemDescriptor
- import com.intellij.codeInspection.ProblemHighlightType
- import com.intellij.codeInspection.ProblemsHolder
- import com.intellij.lang.jvm.JvmModifier
- import com.intellij.psi.JavaElementVisitor
- import com.intellij.psi.PsiClass
- import com.intellij.psi.PsiElementVisitor
- import com.intellij.psi.PsiFile
- import com.intellij.psi.PsiModifier
- class SpongePluginClassInspection : AbstractBaseJavaLocalInspectionTool() {
- override fun getStaticDescription() = "Checks the plugin class is valid."
- override fun buildVisitor(holder: ProblemsHolder, isOnTheFly: Boolean): PsiElementVisitor {
- return if (SpongeModuleType.isInModule(holder.file)) {
- Visitor(holder)
- } else {
- PsiElementVisitor.EMPTY_VISITOR
- }
- }
- override fun processFile(file: PsiFile, manager: InspectionManager): List<ProblemDescriptor> {
- return if (SpongeModuleType.isInModule(file)) {
- super.processFile(file, manager)
- } else {
- emptyList()
- }
- }
- class Visitor(private val holder: ProblemsHolder) : JavaElementVisitor() {
- override fun visitClass(aClass: PsiClass) {
- if (!aClass.isSpongePluginClass()) {
- return
- }
- val ctorInjectAnnos = aClass.constructors.mapNotNull {
- val annotation = it.getAnnotation(SpongeConstants.INJECT_ANNOTATION) ?: return@mapNotNull null
- it to annotation
- }
- if (ctorInjectAnnos.size > 1) {
- val quickFixFactory = QuickFixFactory.getInstance()
- ctorInjectAnnos.forEach { (injectedMethod, injectAnno) ->
- holder.registerProblem(
- injectAnno,
- "There can only be one injected constructor.",
- quickFixFactory.createDeleteFix(injectAnno, "Remove this @Inject"),
- quickFixFactory.createDeleteFix(injectedMethod, "Remove this injected constructor"),
- )
- }
- }
- val hasInjectedCtor = ctorInjectAnnos.isNotEmpty()
- val emptyCtor = aClass.constructors.find { !it.hasParameters() }
- if (emptyCtor == null && !hasInjectedCtor && aClass.constructors.isNotEmpty()) {
- val classIdentifier = aClass.nameIdentifier
- if (classIdentifier != null) {
- holder.registerProblem(
- classIdentifier,
- "Plugin class must have an empty constructor or an @Inject constructor.",
- ProblemHighlightType.GENERIC_ERROR,
- *LocalQuickFix.notNullElements(LocalQuickFix.from(AddDefaultConstructorFix(aClass))),
- )
- }
- }
- if (!hasInjectedCtor && emptyCtor != null && emptyCtor.hasModifier(JvmModifier.PRIVATE)) {
- val ctorIdentifier = emptyCtor.nameIdentifier
- if (ctorIdentifier != null) {
- holder.registerProblem(
- ctorIdentifier,
- "Plugin class empty constructor must not be private.",
- ProblemHighlightType.GENERIC_ERROR,
- ModifierFix(emptyCtor, PsiModifier.PACKAGE_LOCAL, true, false),
- ModifierFix(emptyCtor, PsiModifier.PROTECTED, true, false),
- ModifierFix(emptyCtor, PsiModifier.PUBLIC, true, false),
- )
- }
- }
- val pluginAnnotation = aClass.getAnnotation(SpongeConstants.PLUGIN_ANNOTATION)
- ?: aClass.getAnnotation(SpongeConstants.JVM_PLUGIN_ANNOTATION)
- ?: return
- val pluginIdValue = pluginAnnotation.findAttributeValue("id") ?: return
- val pluginId = pluginIdValue.constantStringValue ?: return
- if (!SpongeConstants.ID_PATTERN.matcher(pluginId).matches()) {
- holder.registerProblem(
- pluginIdValue,
- "Plugin IDs should be lowercase, and only contain characters from a-z, dashes or underscores," +
- " start with a lowercase letter, and not exceed 64 characters.",
- )
- }
- }
- }
- }
- /*
- * 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 <https://www.gnu.org/licenses/>.
- */
- package com.demonwav.mcdev.platform.sponge.inspection
- import com.demonwav.mcdev.platform.sponge.SpongeModuleType
- import com.demonwav.mcdev.platform.sponge.util.SpongeConstants
- import com.demonwav.mcdev.platform.sponge.util.isSpongePluginClass
- import com.demonwav.mcdev.util.constantStringValue
- import com.intellij.codeInsight.daemon.impl.quickfix.AddDefaultConstructorFix
- import com.intellij.codeInsight.daemon.impl.quickfix.ModifierFix
- import com.intellij.codeInsight.intention.QuickFixFactory
- import com.intellij.codeInspection.AbstractBaseJavaLocalInspectionTool
- import com.intellij.codeInspection.InspectionManager
- import com.intellij.codeInspection.LocalQuickFix
- import com.intellij.codeInspection.ProblemDescriptor
- import com.intellij.codeInspection.ProblemHighlightType
- import com.intellij.codeInspection.ProblemsHolder
- import com.intellij.lang.jvm.JvmModifier
- import com.intellij.psi.JavaElementVisitor
- import com.intellij.psi.PsiClass
- import com.intellij.psi.PsiElementVisitor
- import com.intellij.psi.PsiFile
- import com.intellij.psi.PsiModifier
- class SpongePluginClassInspection : AbstractBaseJavaLocalInspectionTool() {
- override fun getStaticDescription() = "Checks the plugin class is valid."
- override fun buildVisitor(holder: ProblemsHolder, isOnTheFly: Boolean): PsiElementVisitor {
- return if (SpongeModuleType.isInModule(holder.file)) {
- Visitor(holder)
- } else {
- PsiElementVisitor.EMPTY_VISITOR
- }
- }
- override fun processFile(file: PsiFile, manager: InspectionManager): List<ProblemDescriptor> {
- return if (SpongeModuleType.isInModule(file)) {
- super.processFile(file, manager)
- } else {
- emptyList()
- }
- }
- class Visitor(private val holder: ProblemsHolder) : JavaElementVisitor() {
- override fun visitClass(aClass: PsiClass) {
- if (!aClass.isSpongePluginClass()) {
- return
- }
- val ctorInjectAnnos = aClass.constructors.mapNotNull {
- val annotation = it.getAnnotation(SpongeConstants.INJECT_ANNOTATION) ?: return@mapNotNull null
- it to annotation
- }
- if (ctorInjectAnnos.size > 1) {
- val quickFixFactory = QuickFixFactory.getInstance()
- ctorInjectAnnos.forEach { (injectedMethod, injectAnno) ->
- holder.registerProblem(
- injectAnno,
- "There can only be one injected constructor.",
- quickFixFactory.createDeleteFix(injectAnno, "Remove this @Inject"),
- quickFixFactory.createDeleteFix(injectedMethod, "Remove this injected constructor"),
- )
- }
- }
- val hasInjectedCtor = ctorInjectAnnos.isNotEmpty()
- val emptyCtor = aClass.constructors.find { !it.hasParameters() }
- if (emptyCtor == null && !hasInjectedCtor && aClass.constructors.isNotEmpty()) {
- val classIdentifier = aClass.nameIdentifier
- if (classIdentifier != null) {
- holder.registerProblem(
- classIdentifier,
- "Plugin class must have an empty constructor or an @Inject constructor.",
- ProblemHighlightType.GENERIC_ERROR,
- *LocalQuickFix.notNullElements(LocalQuickFix.from(AddDefaultConstructorFix(aClass))),
- )
- }
- }
- if (!hasInjectedCtor && emptyCtor != null && emptyCtor.hasModifier(JvmModifier.PRIVATE)) {
- val ctorIdentifier = emptyCtor.nameIdentifier
- if (ctorIdentifier != null) {
- holder.registerProblem(
- ctorIdentifier,
- "Plugin class empty constructor must not be private.",
- ProblemHighlightType.GENERIC_ERROR,
- *LocalQuickFix.notNullElements(
- LocalQuickFix.from(ModifierFix(emptyCtor, PsiModifier.PACKAGE_LOCAL, true, false)),
- LocalQuickFix.from(ModifierFix(emptyCtor, PsiModifier.PROTECTED, true, false)),
- LocalQuickFix.from(ModifierFix(emptyCtor, PsiModifier.PUBLIC, true, false)),
- )
- )
- }
- }
- val pluginAnnotation = aClass.getAnnotation(SpongeConstants.PLUGIN_ANNOTATION)
- ?: aClass.getAnnotation(SpongeConstants.JVM_PLUGIN_ANNOTATION)
- ?: return
- val pluginIdValue = pluginAnnotation.findAttributeValue("id") ?: return
- val pluginId = pluginIdValue.constantStringValue ?: return
- if (!SpongeConstants.ID_PATTERN.matcher(pluginId).matches()) {
- holder.registerProblem(
- pluginIdValue,
- "Plugin IDs should be lowercase, and only contain characters from a-z, dashes or underscores," +
- " start with a lowercase letter, and not exceed 64 characters.",
- )
- }
- }
- }
- }