Compatibility fixes for IDEA <= 2019.1 :-/

This commit is contained in:
Chris Hodges 2019-05-05 20:06:12 +02:00
parent eab50f590b
commit 0e2007641c
2 changed files with 9 additions and 5 deletions

View File

@ -15,7 +15,7 @@ class AssumeThatInsteadOfReturnInspection : AbstractAssertJInspection() {
private const val MAX_RECURSION_DEPTH = 5 private const val MAX_RECURSION_DEPTH = 5
private const val MAX_STATEMENTS_COUNT = 50 private const val MAX_STATEMENTS_COUNT = 50
private val TEST_ANNOTATIONS = listOf( private val TEST_ANNOTATIONS = setOf(
"org.junit.Test", "org.junit.Test",
"org.junit.jupiter.api.Test", "org.junit.jupiter.api.Test",
"org.junit.jupiter.api.TestTemplate", "org.junit.jupiter.api.TestTemplate",
@ -52,7 +52,9 @@ class AssumeThatInsteadOfReturnInspection : AbstractAssertJInspection() {
return object : JavaElementVisitor() { return object : JavaElementVisitor() {
override fun visitMethod(method: PsiMethod) { override fun visitMethod(method: PsiMethod) {
super.visitMethod(method) super.visitMethod(method)
if (TEST_ANNOTATIONS.none(method::hasAnnotation)) { // Note: replace with if(TEST_ANNOTATIONS.none(method::hasAnnotation)) for IDEA >= 2018.2
val annotations = method.annotations.mapNotNull { it.qualifiedName }
if (annotations.none(TEST_ANNOTATIONS::contains)) {
return return
} }
val containingClass = method.containingClass ?: return val containingClass = method.containingClass ?: return

View File

@ -4,7 +4,7 @@ import com.intellij.codeInspection.ProblemHighlightType
import com.intellij.codeInspection.ProblemsHolder import com.intellij.codeInspection.ProblemsHolder
import com.intellij.psi.* import com.intellij.psi.*
import com.intellij.psi.util.PsiTreeUtil import com.intellij.psi.util.PsiTreeUtil
import com.siyeh.ig.psiutils.TrackingEquivalenceChecker import com.siyeh.ig.psiutils.EquivalenceChecker
import de.platon42.intellij.plugins.cajon.* import de.platon42.intellij.plugins.cajon.*
import de.platon42.intellij.plugins.cajon.quickfixes.JoinStatementsQuickFix import de.platon42.intellij.plugins.cajon.quickfixes.JoinStatementsQuickFix
@ -25,7 +25,8 @@ class JoinAssertThatStatementsInspection : AbstractAssertJInspection() {
var sameCount = 0 var sameCount = 0
var firstStatement: PsiStatement? = null var firstStatement: PsiStatement? = null
var lastStatement: PsiStatement? = null var lastStatement: PsiStatement? = null
val equivalenceChecker = TrackingEquivalenceChecker() // Note: replace with TrackingEquivalenceChecker() for IDEA >= 2019.1
val equivalenceChecker = EquivalenceChecker.getCanonicalPsiEquivalence()!!
for (statement in block.statements) { for (statement in block.statements) {
val assertThatCall = isLegitAssertThatCall(statement) val assertThatCall = isLegitAssertThatCall(statement)
var reset = true var reset = true
@ -36,9 +37,10 @@ class JoinAssertThatStatementsInspection : AbstractAssertJInspection() {
if (!reset) { if (!reset) {
val isSame = when (actualExpression) { val isSame = when (actualExpression) {
is PsiMethodCallExpression -> equivalenceChecker.expressionsAreEquivalent(actualExpression, lastActualExpression) is PsiMethodCallExpression -> equivalenceChecker.expressionsAreEquivalent(actualExpression, lastActualExpression)
// Note: replace with PsiTreeUtil.findChildrenOfAnyType(strict = false) for IDEA >= 2018.1
&& !KNOWN_METHODS_WITH_SIDE_EFFECTS.test(actualExpression)
&& PsiTreeUtil.findChildrenOfAnyType( && PsiTreeUtil.findChildrenOfAnyType(
actualExpression, actualExpression,
false,
PsiMethodCallExpression::class.java PsiMethodCallExpression::class.java
).none { KNOWN_METHODS_WITH_SIDE_EFFECTS.test(it) } ).none { KNOWN_METHODS_WITH_SIDE_EFFECTS.test(it) }
else -> equivalenceChecker.expressionsAreEquivalent(actualExpression, lastActualExpression) else -> equivalenceChecker.expressionsAreEquivalent(actualExpression, lastActualExpression)