From ac568bc0a4dfbbcf75badfa469931887b1a8dfd9 Mon Sep 17 00:00:00 2001 From: LlamaLad7 Date: Sun, 24 Sep 2023 17:19:21 +0100 Subject: [PATCH 1/3] Fix: Resolve DefaultAnnotationParamInspectionSuppressor not working for `remap = true` on injector `@At`s. (#2134) --- ...aultAnnotationParamInspectionSuppressor.kt | 45 ++++++++++--------- 1 file changed, 23 insertions(+), 22 deletions(-) diff --git a/src/main/kotlin/platform/mixin/inspection/suppress/DefaultAnnotationParamInspectionSuppressor.kt b/src/main/kotlin/platform/mixin/inspection/suppress/DefaultAnnotationParamInspectionSuppressor.kt index 21861c45e..6242a5083 100644 --- a/src/main/kotlin/platform/mixin/inspection/suppress/DefaultAnnotationParamInspectionSuppressor.kt +++ b/src/main/kotlin/platform/mixin/inspection/suppress/DefaultAnnotationParamInspectionSuppressor.kt @@ -20,16 +20,14 @@ package com.demonwav.mcdev.platform.mixin.inspection.suppress +import com.demonwav.mcdev.platform.mixin.handlers.MixinAnnotationHandler import com.demonwav.mcdev.platform.mixin.util.MixinConstants.Annotations.ACCESSOR import com.demonwav.mcdev.platform.mixin.util.MixinConstants.Annotations.AT import com.demonwav.mcdev.platform.mixin.util.MixinConstants.Annotations.CONSTANT -import com.demonwav.mcdev.platform.mixin.util.MixinConstants.Annotations.INJECT import com.demonwav.mcdev.platform.mixin.util.MixinConstants.Annotations.INVOKER import com.demonwav.mcdev.platform.mixin.util.MixinConstants.Annotations.MIXIN -import com.demonwav.mcdev.platform.mixin.util.MixinConstants.Annotations.MODIFY_ARG -import com.demonwav.mcdev.platform.mixin.util.MixinConstants.Annotations.MODIFY_ARGS -import com.demonwav.mcdev.platform.mixin.util.MixinConstants.Annotations.MODIFY_VARIABLE -import com.demonwav.mcdev.platform.mixin.util.MixinConstants.Annotations.REDIRECT +import com.demonwav.mcdev.platform.mixin.util.MixinConstants.Annotations.OVERWRITE +import com.demonwav.mcdev.platform.mixin.util.MixinConstants.Annotations.SHADOW import com.demonwav.mcdev.util.constantValue import com.demonwav.mcdev.util.findAnnotation import com.demonwav.mcdev.util.mapFirstNotNull @@ -41,6 +39,7 @@ import com.intellij.psi.PsiElement import com.intellij.psi.PsiModifierListOwner import com.intellij.psi.PsiNameValuePair import com.intellij.psi.util.parentOfType +import com.intellij.psi.util.parentsOfType class DefaultAnnotationParamInspectionSuppressor : InspectionSuppressor { override fun isSuppressedFor(element: PsiElement, toolId: String): Boolean { @@ -55,21 +54,23 @@ class DefaultAnnotationParamInspectionSuppressor : InspectionSuppressor { return true } - if (name == "remap" && REMAP_SUPPRESSED.any(annotation::hasQualifiedName)) { + if (name == "remap" && annotation.hasRemap) { val currentRemap = annotation.findAttributeValue("remap")?.constantValue as? Boolean ?: return false - val parentRemap = generateSequence(annotation) { elem -> + var parents = annotation.parentsOfType(withSelf = false).filter { it.hasRemap } + parents += generateSequence(annotation) { elem -> elem.parent?.takeIf { elem !is PsiClass } } .filterIsInstance() .drop(1) // don't look at our own owner .mapNotNull { annotationOwner -> - REMAP_SUPPRESSED.mapFirstNotNull { - annotationOwner.findAnnotation(it)?.findDeclaredAttributeValue("remap")?.constantValue - as? Boolean + HAS_REMAP.mapFirstNotNull { + annotationOwner.findAnnotation(it) } } - .firstOrNull() ?: true + val parentRemap = parents.firstNotNullOfOrNull { + it.findDeclaredAttributeValue("remap")?.constantValue as? Boolean + } ?: true if (currentRemap != parentRemap) { return true } @@ -78,22 +79,22 @@ class DefaultAnnotationParamInspectionSuppressor : InspectionSuppressor { return false } + private val PsiAnnotation.hasRemap get() = qualifiedName?.let { it in HAS_REMAP } == true + override fun getSuppressActions(element: PsiElement?, toolId: String): Array = SuppressQuickFix.EMPTY_ARRAY companion object { private const val INSPECTION = "DefaultAnnotationParam" - private val REMAP_SUPPRESSED = setOf( - AT, - INJECT, - MODIFY_ARG, - MODIFY_ARGS, - MODIFY_VARIABLE, - REDIRECT, - ACCESSOR, - INVOKER, - MIXIN, - ) + private val HAS_REMAP = buildSet { + add(MIXIN) + add(AT) + add(ACCESSOR) + add(INVOKER) + add(OVERWRITE) + add(SHADOW) + addAll(MixinAnnotationHandler.getBuiltinHandlers().map { it.first }) + } private val CONSTANT_SUPPRESSED = setOf( "intValue", "floatValue", From 9c12df297c569609cf274f766ef4b907fb3c8657 Mon Sep 17 00:00:00 2001 From: Joe Date: Sun, 24 Sep 2023 18:57:37 +0100 Subject: [PATCH 2/3] Add yarn mappings to external annotations --- .../net/minecraft/client/annotations.xml | 3 ++ .../minecraft/client/option/annotations.xml | 36 +++++++++++++ .../client/resource/language/annotations.xml | 31 ++++++++++++ .../net/minecraft/text/annotations.xml | 50 +++++++++++++++++++ 4 files changed, 120 insertions(+) create mode 100644 externalAnnotations/net/minecraft/client/option/annotations.xml create mode 100644 externalAnnotations/net/minecraft/client/resource/language/annotations.xml create mode 100644 externalAnnotations/net/minecraft/text/annotations.xml diff --git a/externalAnnotations/net/minecraft/client/annotations.xml b/externalAnnotations/net/minecraft/client/annotations.xml index c329deaef..43474d3b5 100644 --- a/externalAnnotations/net/minecraft/client/annotations.xml +++ b/externalAnnotations/net/minecraft/client/annotations.xml @@ -31,6 +31,8 @@ + + @@ -55,4 +57,5 @@ + diff --git a/externalAnnotations/net/minecraft/client/option/annotations.xml b/externalAnnotations/net/minecraft/client/option/annotations.xml new file mode 100644 index 000000000..c3da6fa02 --- /dev/null +++ b/externalAnnotations/net/minecraft/client/option/annotations.xml @@ -0,0 +1,36 @@ + + + + + + + + + + + + + + + + + + diff --git a/externalAnnotations/net/minecraft/client/resource/language/annotations.xml b/externalAnnotations/net/minecraft/client/resource/language/annotations.xml new file mode 100644 index 000000000..6846f1736 --- /dev/null +++ b/externalAnnotations/net/minecraft/client/resource/language/annotations.xml @@ -0,0 +1,31 @@ + + + + + + + + + + + + + diff --git a/externalAnnotations/net/minecraft/text/annotations.xml b/externalAnnotations/net/minecraft/text/annotations.xml new file mode 100644 index 000000000..374328686 --- /dev/null +++ b/externalAnnotations/net/minecraft/text/annotations.xml @@ -0,0 +1,50 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + From 2e509b4344006cc9877f72b991573cdfbf0c55da Mon Sep 17 00:00:00 2001 From: Joe Date: Mon, 25 Sep 2023 01:51:17 +0100 Subject: [PATCH 3/3] Revert "Replace some usages of PsiClassType.resolve() with PsiType.equalsToText()" This reverts commit 9793e59853492b6d6b01bda5ab437c7438c18c12. --- src/main/kotlin/platform/bukkit/BukkitModule.kt | 8 ++++---- src/main/kotlin/platform/forge/ForgeModule.kt | 4 ++-- .../mcp/inspections/StackEmptyInspection.kt | 3 ++- .../config/inspection/ConfigValueInspection.kt | 13 +++++++++---- .../mixin/folding/MixinObjectCastFoldingBuilder.kt | 3 ++- .../platform/mixin/handlers/InvokerHandler.kt | 4 +++- .../mixin/inspection/MixinCancellableInspection.kt | 4 +++- .../injector/InjectCouldBeOverwriteInspection.kt | 9 ++++++--- .../InvalidInjectorMethodSignatureInspection.kt | 7 +++++-- .../WrongOperationParametersInspection.kt | 3 ++- src/main/kotlin/platform/sponge/SpongeModule.kt | 4 ++-- .../sponge/inspection/SpongeLoggingInspection.kt | 5 ++++- .../inspection/SpongeWrongGetterTypeInspection.kt | 4 ++-- src/main/kotlin/platform/velocity/VelocityModule.kt | 4 ++-- src/main/kotlin/util/class-utils.kt | 5 +++++ src/main/kotlin/util/code-gen.kt | 12 +++++++----- 16 files changed, 60 insertions(+), 32 deletions(-) diff --git a/src/main/kotlin/platform/bukkit/BukkitModule.kt b/src/main/kotlin/platform/bukkit/BukkitModule.kt index e2c427e4c..1a73b9488 100644 --- a/src/main/kotlin/platform/bukkit/BukkitModule.kt +++ b/src/main/kotlin/platform/bukkit/BukkitModule.kt @@ -92,7 +92,7 @@ class BukkitModule>(facet: MinecraftFacet, type: T chosenClass: PsiClass, chosenName: String, data: GenerationData?, - ): PsiMethod { + ): PsiMethod? { val bukkitData = data as BukkitGenerationData val method = generateBukkitStyleEventListenerMethod( @@ -101,7 +101,7 @@ class BukkitModule>(facet: MinecraftFacet, type: T project, BukkitConstants.HANDLER_ANNOTATION, bukkitData.isIgnoreCanceled, - ) + ) ?: return null if (bukkitData.eventPriority != "NORMAL") { val list = method.modifierList @@ -217,8 +217,8 @@ class BukkitModule>(facet: MinecraftFacet, type: T project: Project, annotationName: String, setIgnoreCancelled: Boolean, - ): PsiMethod { - val newMethod = createVoidMethodWithParameterType(project, chosenName, chosenClass) + ): PsiMethod? { + val newMethod = createVoidMethodWithParameterType(project, chosenName, chosenClass) ?: return null val modifierList = newMethod.modifierList val annotation = modifierList.addAnnotation(annotationName) diff --git a/src/main/kotlin/platform/forge/ForgeModule.kt b/src/main/kotlin/platform/forge/ForgeModule.kt index 35fb4b996..02ebdce1b 100644 --- a/src/main/kotlin/platform/forge/ForgeModule.kt +++ b/src/main/kotlin/platform/forge/ForgeModule.kt @@ -163,10 +163,10 @@ class ForgeModule internal constructor(facet: MinecraftFacet) : AbstractModule(f chosenClass: PsiClass, chosenName: String, data: GenerationData?, - ): PsiMethod { + ): PsiMethod? { val isFmlEvent = chosenClass.extendsOrImplements(ForgeConstants.FML_EVENT) - val method = createVoidMethodWithParameterType(project, chosenName, chosenClass) + val method = createVoidMethodWithParameterType(project, chosenName, chosenClass) ?: return null val modifierList = method.modifierList if (isFmlEvent) { diff --git a/src/main/kotlin/platform/mcp/inspections/StackEmptyInspection.kt b/src/main/kotlin/platform/mcp/inspections/StackEmptyInspection.kt index 22693193d..4b3cb7470 100644 --- a/src/main/kotlin/platform/mcp/inspections/StackEmptyInspection.kt +++ b/src/main/kotlin/platform/mcp/inspections/StackEmptyInspection.kt @@ -32,6 +32,7 @@ import com.intellij.openapi.project.Project import com.intellij.psi.JavaPsiFacade import com.intellij.psi.JavaTokenType import com.intellij.psi.PsiBinaryExpression +import com.intellij.psi.PsiClassType import com.intellij.psi.PsiExpression import com.intellij.psi.PsiField import com.intellij.psi.PsiReferenceExpression @@ -117,7 +118,7 @@ class StackEmptyInspection : BaseInspection() { } private fun isExpressionStack(expression: PsiExpression?): Boolean { - return expression?.type?.equalsToText(STACK_FQ_NAME) == true + return (expression?.type as? PsiClassType)?.resolve()?.fullQualifiedName == STACK_FQ_NAME } private fun isExpressionEmptyConstant(expression: PsiExpression?): Boolean { diff --git a/src/main/kotlin/platform/mixin/config/inspection/ConfigValueInspection.kt b/src/main/kotlin/platform/mixin/config/inspection/ConfigValueInspection.kt index 8d8cf2a1d..a19d2ff3c 100644 --- a/src/main/kotlin/platform/mixin/config/inspection/ConfigValueInspection.kt +++ b/src/main/kotlin/platform/mixin/config/inspection/ConfigValueInspection.kt @@ -40,6 +40,7 @@ import com.intellij.psi.CommonClassNames.JAVA_LANG_INTEGER import com.intellij.psi.CommonClassNames.JAVA_LANG_LONG import com.intellij.psi.CommonClassNames.JAVA_LANG_SHORT import com.intellij.psi.CommonClassNames.JAVA_LANG_STRING +import com.intellij.psi.CommonClassNames.JAVA_LANG_STRING_SHORT import com.intellij.psi.PsiArrayType import com.intellij.psi.PsiClassType import com.intellij.psi.PsiElementVisitor @@ -94,15 +95,18 @@ class ConfigValueInspection : MixinConfigInspection() { return true // Idk, it's fine I guess } - if (type.equalsToText(JAVA_LANG_STRING)) { + if (type.className == JAVA_LANG_STRING_SHORT && type.resolve()?.qualifiedName == JAVA_LANG_STRING) { return value is JsonStringLiteral } - if (type.equalsToText(CommonClassNames.JAVA_LANG_BOOLEAN)) { + if (type.className == "Boolean" && type.resolve()?.qualifiedName == CommonClassNames.JAVA_LANG_BOOLEAN) { return value is JsonBooleanLiteral || value is JsonNullLiteral } - if (qualifiedNumberNames.any(type::equalsToText)) { + if ( + shortNumberNames.contains(type.className) && + qualifiedNumberNames.contains(type.resolve()?.qualifiedName) + ) { return value is JsonNumberLiteral || value is JsonNullLiteral } @@ -110,7 +114,8 @@ class ConfigValueInspection : MixinConfigInspection() { return value is JsonObject } - private val qualifiedNumberNames = listOf( + private val shortNumberNames = setOf("Byte", "Character", "Double", "Float", "Integer", "Long", "Short") + private val qualifiedNumberNames = setOf( JAVA_LANG_BYTE, JAVA_LANG_CHARACTER, JAVA_LANG_DOUBLE, diff --git a/src/main/kotlin/platform/mixin/folding/MixinObjectCastFoldingBuilder.kt b/src/main/kotlin/platform/mixin/folding/MixinObjectCastFoldingBuilder.kt index ecf35c3b2..5478e1dd3 100644 --- a/src/main/kotlin/platform/mixin/folding/MixinObjectCastFoldingBuilder.kt +++ b/src/main/kotlin/platform/mixin/folding/MixinObjectCastFoldingBuilder.kt @@ -28,6 +28,7 @@ import com.intellij.openapi.editor.Document import com.intellij.openapi.util.TextRange import com.intellij.psi.CommonClassNames import com.intellij.psi.JavaRecursiveElementWalkingVisitor +import com.intellij.psi.PsiClassType import com.intellij.psi.PsiElement import com.intellij.psi.PsiJavaFile import com.intellij.psi.PsiTypeCastExpression @@ -73,7 +74,7 @@ class MixinObjectCastFoldingBuilder : CustomFoldingBuilder() { } val innerCast = expression.operand as? PsiTypeCastExpression ?: return - if (innerCast.type?.equalsToText(CommonClassNames.JAVA_LANG_OBJECT) == true) { + if ((innerCast.type as? PsiClassType)?.resolve()?.qualifiedName == CommonClassNames.JAVA_LANG_OBJECT) { // Fold the two casts val start = (expression as? CompositeElement)?.findChildByRole(ChildRole.LPARENTH) ?: return diff --git a/src/main/kotlin/platform/mixin/handlers/InvokerHandler.kt b/src/main/kotlin/platform/mixin/handlers/InvokerHandler.kt index f9c7f7b6d..ff9f2ba87 100644 --- a/src/main/kotlin/platform/mixin/handlers/InvokerHandler.kt +++ b/src/main/kotlin/platform/mixin/handlers/InvokerHandler.kt @@ -30,7 +30,9 @@ import com.demonwav.mcdev.util.constantStringValue import com.demonwav.mcdev.util.decapitalize import com.demonwav.mcdev.util.descriptor import com.demonwav.mcdev.util.findAnnotation +import com.demonwav.mcdev.util.fullQualifiedName import com.intellij.psi.PsiAnnotation +import com.intellij.psi.PsiClassType import com.intellij.psi.PsiMember import com.intellij.psi.PsiMethod import com.intellij.psi.SmartPsiElementPointer @@ -53,7 +55,7 @@ class InvokerHandler : MixinMemberAnnotationHandler { val name = getInvokerTargetName(annotation, member) ?: return emptyList() val constructor = name == "" if (constructor && - member.returnType?.equalsToText(targetClass.name.replace('/', '.').replace('$', '.')) != true + (member.returnType as? PsiClassType)?.resolve()?.fullQualifiedName?.replace('.', '/') != targetClass.name ) { return emptyList() } diff --git a/src/main/kotlin/platform/mixin/inspection/MixinCancellableInspection.kt b/src/main/kotlin/platform/mixin/inspection/MixinCancellableInspection.kt index 955320d51..85ef79174 100644 --- a/src/main/kotlin/platform/mixin/inspection/MixinCancellableInspection.kt +++ b/src/main/kotlin/platform/mixin/inspection/MixinCancellableInspection.kt @@ -23,6 +23,7 @@ package com.demonwav.mcdev.platform.mixin.inspection import com.demonwav.mcdev.platform.mixin.util.MixinConstants.Annotations.INJECT import com.demonwav.mcdev.platform.mixin.util.MixinConstants.Classes.CALLBACK_INFO import com.demonwav.mcdev.platform.mixin.util.MixinConstants.Classes.CALLBACK_INFO_RETURNABLE +import com.demonwav.mcdev.util.fullQualifiedName import com.intellij.codeInspection.LocalQuickFixAndIntentionActionOnPsiElement import com.intellij.codeInspection.ProblemHighlightType import com.intellij.codeInspection.ProblemsHolder @@ -57,7 +58,8 @@ class MixinCancellableInspection : MixinInspection() { val isCancellable = cancellableAttribute.value == true val ciParam = method.parameterList.parameters.firstOrNull { - it.type.equalsToText(CALLBACK_INFO) || it.type.equalsToText(CALLBACK_INFO_RETURNABLE) + val className = (it.type as? PsiClassType)?.fullQualifiedName ?: return@firstOrNull false + className == CALLBACK_INFO || className == CALLBACK_INFO_RETURNABLE } ?: return val ciType = (ciParam.type as? PsiClassType)?.resolve() ?: return diff --git a/src/main/kotlin/platform/mixin/inspection/injector/InjectCouldBeOverwriteInspection.kt b/src/main/kotlin/platform/mixin/inspection/injector/InjectCouldBeOverwriteInspection.kt index d620b84bf..9d96a33dc 100644 --- a/src/main/kotlin/platform/mixin/inspection/injector/InjectCouldBeOverwriteInspection.kt +++ b/src/main/kotlin/platform/mixin/inspection/injector/InjectCouldBeOverwriteInspection.kt @@ -360,8 +360,11 @@ class InjectCouldBeOverwriteInspection : MixinInspection() { } companion object { - private fun isCallbackInfoParam(param: PsiParameter) = - param.type.equalsToText(MixinConstants.Classes.CALLBACK_INFO) || - param.type.equalsToText(MixinConstants.Classes.CALLBACK_INFO_RETURNABLE) + private fun isCallbackInfoParam(param: PsiParameter): Boolean { + val type = (param.type as? PsiClassType)?.resolve() ?: return false + val qName = type.qualifiedName ?: return false + return qName == MixinConstants.Classes.CALLBACK_INFO || + qName == MixinConstants.Classes.CALLBACK_INFO_RETURNABLE + } } } diff --git a/src/main/kotlin/platform/mixin/inspection/injector/InvalidInjectorMethodSignatureInspection.kt b/src/main/kotlin/platform/mixin/inspection/injector/InvalidInjectorMethodSignatureInspection.kt index 29ede21c2..1b93da3c2 100644 --- a/src/main/kotlin/platform/mixin/inspection/injector/InvalidInjectorMethodSignatureInspection.kt +++ b/src/main/kotlin/platform/mixin/inspection/injector/InvalidInjectorMethodSignatureInspection.kt @@ -32,6 +32,7 @@ import com.demonwav.mcdev.platform.mixin.util.isAssignable import com.demonwav.mcdev.platform.mixin.util.isConstructor import com.demonwav.mcdev.platform.mixin.util.isMixinExtrasSugar import com.demonwav.mcdev.util.Parameter +import com.demonwav.mcdev.util.fullQualifiedName import com.demonwav.mcdev.util.synchronize import com.intellij.codeInsight.intention.FileModifier.SafeFieldForPreview import com.intellij.codeInsight.intention.QuickFixFactory @@ -42,6 +43,7 @@ import com.intellij.codeInspection.ProblemsHolder import com.intellij.openapi.project.Project import com.intellij.psi.JavaElementVisitor import com.intellij.psi.JavaPsiFacade +import com.intellij.psi.PsiClassType import com.intellij.psi.PsiElementVisitor import com.intellij.psi.PsiMethod import com.intellij.psi.PsiModifier @@ -305,8 +307,9 @@ class InvalidInjectorMethodSignatureInspection : MixinInspection() { val parameters = descriptor.psiElement as PsiParameterList // We want to preserve captured locals val locals = parameters.parameters.dropWhile { - !it.type.equalsToText(MixinConstants.Classes.CALLBACK_INFO) && - !it.type.equalsToText(MixinConstants.Classes.CALLBACK_INFO_RETURNABLE) + val fqname = (it.type as? PsiClassType)?.fullQualifiedName ?: return@dropWhile true + return@dropWhile fqname != MixinConstants.Classes.CALLBACK_INFO && + fqname != MixinConstants.Classes.CALLBACK_INFO_RETURNABLE }.drop(1) // the first element in the list is the CallbackInfo but we don't want it .takeWhile { !it.isMixinExtrasSugar } diff --git a/src/main/kotlin/platform/mixin/inspection/mixinextras/WrongOperationParametersInspection.kt b/src/main/kotlin/platform/mixin/inspection/mixinextras/WrongOperationParametersInspection.kt index 75f8dcf00..2ebca3ca7 100644 --- a/src/main/kotlin/platform/mixin/inspection/mixinextras/WrongOperationParametersInspection.kt +++ b/src/main/kotlin/platform/mixin/inspection/mixinextras/WrongOperationParametersInspection.kt @@ -32,6 +32,7 @@ import com.intellij.psi.JavaElementVisitor import com.intellij.psi.JavaPsiFacade import com.intellij.psi.PsiCallExpression import com.intellij.psi.PsiClass +import com.intellij.psi.PsiClassType import com.intellij.psi.PsiElement import com.intellij.psi.PsiExpressionList import com.intellij.psi.PsiField @@ -67,7 +68,7 @@ class WrongOperationParametersInspection : MixinInspection() { val (operationIndex, operationParam) = containingMethod.parameterList.parameters.asSequence() .withIndex() .firstOrNull { (_, param) -> - param.type.equalsToText(MixinConstants.MixinExtras.OPERATION) + (param.type as? PsiClassType)?.resolve()?.qualifiedName == MixinConstants.MixinExtras.OPERATION } ?: return val (expectedParamTypes, paramNames) = containingMethod.parameterList.parameters.asSequence() .take(operationIndex) diff --git a/src/main/kotlin/platform/sponge/SpongeModule.kt b/src/main/kotlin/platform/sponge/SpongeModule.kt index 50a58b8bd..8bd09dc7e 100644 --- a/src/main/kotlin/platform/sponge/SpongeModule.kt +++ b/src/main/kotlin/platform/sponge/SpongeModule.kt @@ -61,8 +61,8 @@ class SpongeModule(facet: MinecraftFacet) : AbstractModule(facet) { chosenClass: PsiClass, chosenName: String, data: GenerationData?, - ): PsiMethod { - val method = createVoidMethodWithParameterType(project, chosenName, chosenClass) + ): PsiMethod? { + val method = createVoidMethodWithParameterType(project, chosenName, chosenClass) ?: return null val modifierList = method.modifierList val listenerAnnotation = modifierList.addAnnotation("org.spongepowered.api.event.Listener") diff --git a/src/main/kotlin/platform/sponge/inspection/SpongeLoggingInspection.kt b/src/main/kotlin/platform/sponge/inspection/SpongeLoggingInspection.kt index 6158618d0..f18c3bfd0 100644 --- a/src/main/kotlin/platform/sponge/inspection/SpongeLoggingInspection.kt +++ b/src/main/kotlin/platform/sponge/inspection/SpongeLoggingInspection.kt @@ -22,12 +22,14 @@ package com.demonwav.mcdev.platform.sponge.inspection import com.demonwav.mcdev.platform.sponge.SpongeModuleType import com.demonwav.mcdev.util.Constants +import com.demonwav.mcdev.util.fullQualifiedName import com.intellij.codeInspection.AbstractBaseJavaLocalInspectionTool import com.intellij.codeInspection.InspectionManager import com.intellij.codeInspection.ProblemDescriptor import com.intellij.codeInspection.ProblemHighlightType import com.intellij.codeInspection.ProblemsHolder import com.intellij.psi.JavaElementVisitor +import com.intellij.psi.PsiClassType import com.intellij.psi.PsiElementVisitor import com.intellij.psi.PsiField import com.intellij.psi.PsiFile @@ -58,8 +60,9 @@ class SpongeLoggingInspection : AbstractBaseJavaLocalInspectionTool() { override fun visitField(field: PsiField) { val element = field.typeElement ?: return + val name = (field.type as? PsiClassType)?.fullQualifiedName ?: return - if (!field.type.equalsToText(Constants.JAVA_UTIL_LOGGER)) { + if (name != Constants.JAVA_UTIL_LOGGER) { return } diff --git a/src/main/kotlin/platform/sponge/inspection/SpongeWrongGetterTypeInspection.kt b/src/main/kotlin/platform/sponge/inspection/SpongeWrongGetterTypeInspection.kt index b577f2596..b3dc3cf3a 100644 --- a/src/main/kotlin/platform/sponge/inspection/SpongeWrongGetterTypeInspection.kt +++ b/src/main/kotlin/platform/sponge/inspection/SpongeWrongGetterTypeInspection.kt @@ -23,6 +23,7 @@ package com.demonwav.mcdev.platform.sponge.inspection import com.demonwav.mcdev.platform.sponge.util.SpongeConstants import com.demonwav.mcdev.platform.sponge.util.isValidSpongeListener import com.demonwav.mcdev.platform.sponge.util.resolveSpongeGetterTarget +import com.demonwav.mcdev.util.isJavaOptional import com.intellij.codeInspection.AbstractBaseUastLocalInspectionTool import com.intellij.codeInspection.InspectionManager import com.intellij.codeInspection.IntentionAndQuickFixAction @@ -35,7 +36,6 @@ import com.intellij.lang.jvm.actions.expectedParameter import com.intellij.lang.jvm.actions.updateMethodParametersRequest import com.intellij.openapi.editor.Editor import com.intellij.openapi.project.Project -import com.intellij.psi.CommonClassNames import com.intellij.psi.JavaPsiFacade import com.intellij.psi.PsiClassType import com.intellij.psi.PsiFile @@ -126,7 +126,7 @@ class SpongeWrongGetterTypeInspection : AbstractBaseUastLocalInspectionTool() { private fun isOptional(type: PsiType): Boolean { val typeClass = type as? PsiClassType ?: return false - return typeClass.equalsToText(CommonClassNames.JAVA_UTIL_OPTIONAL) && typeClass.hasParameters() + return typeClass.isJavaOptional() && typeClass.hasParameters() } private fun getFirstGenericType(typeElement: PsiType): PsiType? = diff --git a/src/main/kotlin/platform/velocity/VelocityModule.kt b/src/main/kotlin/platform/velocity/VelocityModule.kt index d84e52037..33c51bf40 100644 --- a/src/main/kotlin/platform/velocity/VelocityModule.kt +++ b/src/main/kotlin/platform/velocity/VelocityModule.kt @@ -51,8 +51,8 @@ class VelocityModule(facet: MinecraftFacet) : AbstractModule(facet) { chosenClass: PsiClass, chosenName: String, data: GenerationData?, - ): PsiMethod { - val method = createVoidMethodWithParameterType(project, chosenName, chosenClass) + ): PsiMethod? { + val method = createVoidMethodWithParameterType(project, chosenName, chosenClass) ?: return null val modifierList = method.modifierList val subscribeAnnotation = modifierList.addAnnotation(SUBSCRIBE_ANNOTATION) diff --git a/src/main/kotlin/util/class-utils.kt b/src/main/kotlin/util/class-utils.kt index 32048b19b..d3f3799b6 100644 --- a/src/main/kotlin/util/class-utils.kt +++ b/src/main/kotlin/util/class-utils.kt @@ -24,6 +24,7 @@ import com.intellij.codeInsight.daemon.impl.quickfix.AddMethodFix import com.intellij.navigation.AnonymousElementProvider import com.intellij.openapi.editor.Editor import com.intellij.openapi.project.Project +import com.intellij.psi.CommonClassNames import com.intellij.psi.JavaPsiFacade import com.intellij.psi.PsiClass import com.intellij.psi.PsiClassType @@ -313,6 +314,10 @@ private fun areReallyOnlyParametersErasureEqual( return true } +fun PsiClass.isJavaOptional(): Boolean = this.qualifiedName == CommonClassNames.JAVA_UTIL_OPTIONAL + +fun PsiClassType.isJavaOptional(): Boolean = this.fullQualifiedName == CommonClassNames.JAVA_UTIL_OPTIONAL + class ClassNameResolutionFailedException : Exception { constructor() : super() constructor(message: String) : super(message) diff --git a/src/main/kotlin/util/code-gen.kt b/src/main/kotlin/util/code-gen.kt index bfe89a8fa..8fe937b3c 100644 --- a/src/main/kotlin/util/code-gen.kt +++ b/src/main/kotlin/util/code-gen.kt @@ -26,19 +26,21 @@ import com.intellij.openapi.util.text.StringUtil import com.intellij.psi.JavaPsiFacade import com.intellij.psi.PsiAnnotation import com.intellij.psi.PsiClass +import com.intellij.psi.PsiClassType import com.intellij.psi.PsiMember import com.intellij.psi.PsiMethod import com.intellij.psi.PsiType +import com.intellij.psi.search.GlobalSearchScope -fun createVoidMethodWithParameterType(project: Project, name: String, paramType: PsiClass): PsiMethod { - val elementFactory = JavaPsiFacade.getElementFactory(project) - val newMethod = elementFactory.createMethod(name, PsiType.VOID) +fun createVoidMethodWithParameterType(project: Project, name: String, paramType: PsiClass): PsiMethod? { + val newMethod = JavaPsiFacade.getElementFactory(project).createMethod(name, PsiType.VOID) val list = newMethod.parameterList - val parameter = elementFactory + val qName = paramType.qualifiedName ?: return null + val parameter = JavaPsiFacade.getElementFactory(project) .createParameter( "event", - elementFactory.createType(paramType) + PsiClassType.getTypeByName(qName, project, GlobalSearchScope.allScope(project)), ) list.add(parameter)