Skip to content

Commit

Permalink
Revert "Replace some usages of PsiClassType.resolve() with PsiType.eq…
Browse files Browse the repository at this point in the history
…ualsToText()"

This reverts commit 9793e59.
  • Loading branch information
Earthcomputer committed Sep 25, 2023
1 parent 9c12df2 commit 2e509b4
Show file tree
Hide file tree
Showing 16 changed files with 60 additions and 32 deletions.
8 changes: 4 additions & 4 deletions src/main/kotlin/platform/bukkit/BukkitModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ class BukkitModule<out T : AbstractModuleType<*>>(facet: MinecraftFacet, type: T
chosenClass: PsiClass,
chosenName: String,
data: GenerationData?,
): PsiMethod {
): PsiMethod? {
val bukkitData = data as BukkitGenerationData

val method = generateBukkitStyleEventListenerMethod(
Expand All @@ -101,7 +101,7 @@ class BukkitModule<out T : AbstractModuleType<*>>(facet: MinecraftFacet, type: T
project,
BukkitConstants.HANDLER_ANNOTATION,
bukkitData.isIgnoreCanceled,
)
) ?: return null

if (bukkitData.eventPriority != "NORMAL") {
val list = method.modifierList
Expand Down Expand Up @@ -217,8 +217,8 @@ class BukkitModule<out T : AbstractModuleType<*>>(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)

Expand Down
4 changes: 2 additions & 2 deletions src/main/kotlin/platform/forge/ForgeModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -94,23 +95,27 @@ 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
}

PsiUtil.extractIterableTypeParameter(type, true)?.let { return checkArray(it, value) }
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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion src/main/kotlin/platform/mixin/handlers/InvokerHandler.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -53,7 +55,7 @@ class InvokerHandler : MixinMemberAnnotationHandler {
val name = getInvokerTargetName(annotation, member) ?: return emptyList()
val constructor = name == "<init>"
if (constructor &&
member.returnType?.equalsToText(targetClass.name.replace('/', '.').replace('$', '.')) != true
(member.returnType as? PsiClassType)?.resolve()?.fullQualifiedName?.replace('.', '/') != targetClass.name
) {
return emptyList()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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 }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions src/main/kotlin/platform/sponge/SpongeModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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? =
Expand Down
4 changes: 2 additions & 2 deletions src/main/kotlin/platform/velocity/VelocityModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 5 additions & 0 deletions src/main/kotlin/util/class-utils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
12 changes: 7 additions & 5 deletions src/main/kotlin/util/code-gen.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down

0 comments on commit 2e509b4

Please sign in to comment.