User: rednesto Date: 15 Sep 23 12:00 Revision: 1f32ebe7f4a462a3c306f1b9527669190413c582 Summary: Fix inspection fixes taht may use invalid elements Fixes minecraft-dev/mcdev-error-report#1464 TeamCity URL: https://ci.mcdev.io/viewModification.html?tab=vcsModificationFiles&modId=8724&personal=false Index: src/main/kotlin/platform/bukkit/inspection/BukkitListenerImplementedInspection.kt =================================================================== --- src/main/kotlin/platform/bukkit/inspection/BukkitListenerImplementedInspection.kt (revision 274598997db38fc0a82af3e751ebad9a3cc1f9d2) +++ src/main/kotlin/platform/bukkit/inspection/BukkitListenerImplementedInspection.kt (revision 1f32ebe7f4a462a3c306f1b9527669190413c582) @@ -30,6 +30,7 @@ import com.siyeh.ig.BaseInspectionVisitor import com.siyeh.ig.InspectionGadgetsFix import org.jetbrains.annotations.Nls +import org.jetbrains.kotlin.psi.psiUtil.createSmartPointer class BukkitListenerImplementedInspection : BaseInspection() { @@ -44,10 +45,10 @@ "All Bukkit @EventHandler methods must reside in a class that implements Listener." override fun buildFix(vararg infos: Any): InspectionGadgetsFix { + val classPointer = (infos[0] as PsiClass).createSmartPointer() return object : InspectionGadgetsFix() { override fun doFix(project: Project, descriptor: ProblemDescriptor) { - val psiClass = infos[0] as PsiClass - psiClass.addImplements(BukkitConstants.LISTENER_CLASS) + classPointer.element?.addImplements(BukkitConstants.LISTENER_CLASS) } @Nls @@ -61,11 +62,7 @@ override fun buildVisitor(): BaseInspectionVisitor { return object : BaseInspectionVisitor() { override fun visitClass(aClass: PsiClass) { - if ( - aClass.methods.none { - it.modifierList.findAnnotation(BukkitConstants.HANDLER_ANNOTATION) != null - } - ) { + if (aClass.methods.none { it.hasAnnotation(BukkitConstants.HANDLER_ANNOTATION) }) { return } Index: src/main/kotlin/platform/bungeecord/inspection/BungeeCordListenerImplementedInspection.kt =================================================================== --- src/main/kotlin/platform/bungeecord/inspection/BungeeCordListenerImplementedInspection.kt (revision 274598997db38fc0a82af3e751ebad9a3cc1f9d2) +++ src/main/kotlin/platform/bungeecord/inspection/BungeeCordListenerImplementedInspection.kt (revision 1f32ebe7f4a462a3c306f1b9527669190413c582) @@ -30,6 +30,7 @@ import com.siyeh.ig.BaseInspectionVisitor import com.siyeh.ig.InspectionGadgetsFix import org.jetbrains.annotations.Nls +import org.jetbrains.kotlin.psi.psiUtil.createSmartPointer class BungeeCordListenerImplementedInspection : BaseInspection() { @@ -43,10 +44,10 @@ "All BungeeCord @EventHandler methods must reside in a class that implements Listener." override fun buildFix(vararg infos: Any): InspectionGadgetsFix { + val classPointer = (infos[0] as PsiClass).createSmartPointer() return object : InspectionGadgetsFix() { override fun doFix(project: Project, descriptor: ProblemDescriptor) { - val psiClass = infos[0] as PsiClass - psiClass.addImplements(BungeeCordConstants.LISTENER_CLASS) + classPointer.element?.addImplements(BungeeCordConstants.LISTENER_CLASS) } @Nls Index: src/main/kotlin/platform/mcp/inspections/StackEmptyInspection.kt =================================================================== --- src/main/kotlin/platform/mcp/inspections/StackEmptyInspection.kt (revision 274598997db38fc0a82af3e751ebad9a3cc1f9d2) +++ src/main/kotlin/platform/mcp/inspections/StackEmptyInspection.kt (revision 1f32ebe7f4a462a3c306f1b9527669190413c582) @@ -40,6 +40,7 @@ import com.siyeh.ig.BaseInspectionVisitor import com.siyeh.ig.InspectionGadgetsFix import org.jetbrains.annotations.Nls +import org.jetbrains.kotlin.psi.psiUtil.createSmartPointer class StackEmptyInspection : BaseInspection() { companion object { @@ -63,14 +64,17 @@ " the stack should still be considered empty. Instead, isEmpty() should be called." override fun buildFix(vararg infos: Any): InspectionGadgetsFix { + val compareExpressionPointer = (infos[0] as PsiExpression).createSmartPointer() + val binaryExpressionPointer = (infos[2] as PsiBinaryExpression).createSmartPointer() return object : InspectionGadgetsFix() { override fun getFamilyName() = "Replace with .isEmpty()" override fun doFix(project: Project, descriptor: ProblemDescriptor) { - val compareExpression = infos[0] as PsiExpression - val binaryExpression = infos[2] as PsiBinaryExpression val elementFactory = JavaPsiFacade.getElementFactory(project) + val compareExpression = compareExpressionPointer.element ?: return + val binaryExpression = binaryExpressionPointer.element ?: return + val mappedIsEmpty = compareExpression.findModule()?.getMappedMethod(IS_EMPTY_SRG)?.name ?: "isEmpty" var expressionText = "${compareExpression.text}.$mappedIsEmpty()"