diff --git a/src/main/java/de/platon42/intellij/plugins/cajon/Extensions.kt b/src/main/java/de/platon42/intellij/plugins/cajon/Extensions.kt index 23057a9..b8834be 100644 --- a/src/main/java/de/platon42/intellij/plugins/cajon/Extensions.kt +++ b/src/main/java/de/platon42/intellij/plugins/cajon/Extensions.kt @@ -12,6 +12,8 @@ import de.platon42.intellij.plugins.cajon.inspections.AbstractAssertJInspection val PsiMethodCallExpression.qualifierExpression: PsiExpression get() = methodExpression.qualifierExpression!! val PsiMethodCallExpression.firstArg: PsiExpression get() = getArg(0) +fun PsiElement.hasAssertThat() = text.contains("assertThat") + fun PsiMethodCallExpression.replaceQualifier(qualifier: PsiElement) { qualifierExpression.replace(qualifier) } diff --git a/src/main/java/de/platon42/intellij/plugins/cajon/inspections/AssertThatBinaryExpressionInspection.kt b/src/main/java/de/platon42/intellij/plugins/cajon/inspections/AssertThatBinaryExpressionInspection.kt index 2036890..3e71f04 100644 --- a/src/main/java/de/platon42/intellij/plugins/cajon/inspections/AssertThatBinaryExpressionInspection.kt +++ b/src/main/java/de/platon42/intellij/plugins/cajon/inspections/AssertThatBinaryExpressionInspection.kt @@ -20,9 +20,12 @@ class AssertThatBinaryExpressionInspection : AbstractAssertJInspection() { override fun buildVisitor(holder: ProblemsHolder, isOnTheFly: Boolean): PsiElementVisitor { return object : JavaElementVisitor() { - override fun visitExpressionStatement(statement: PsiExpressionStatement?) { + override fun visitExpressionStatement(statement: PsiExpressionStatement) { super.visitExpressionStatement(statement) - val staticMethodCall = statement?.findStaticMethodCall() ?: return + if (!statement.hasAssertThat()) { + return + } + val staticMethodCall = statement.findStaticMethodCall() ?: return if (!ASSERT_THAT_BOOLEAN.test(staticMethodCall)) { return } diff --git a/src/main/java/de/platon42/intellij/plugins/cajon/inspections/AssertThatBooleanConditionInspection.kt b/src/main/java/de/platon42/intellij/plugins/cajon/inspections/AssertThatBooleanConditionInspection.kt index 6d6f38b..38e5759 100644 --- a/src/main/java/de/platon42/intellij/plugins/cajon/inspections/AssertThatBooleanConditionInspection.kt +++ b/src/main/java/de/platon42/intellij/plugins/cajon/inspections/AssertThatBooleanConditionInspection.kt @@ -5,11 +5,8 @@ import com.intellij.psi.JavaElementVisitor import com.intellij.psi.PsiElementVisitor import com.intellij.psi.PsiMethodCallExpression import com.intellij.psi.util.TypeConversionUtil +import de.platon42.intellij.plugins.cajon.* import de.platon42.intellij.plugins.cajon.AssertJClassNames.Companion.ABSTRACT_BOOLEAN_ASSERT_CLASSNAME -import de.platon42.intellij.plugins.cajon.MethodNames -import de.platon42.intellij.plugins.cajon.calculateConstantParameterValue -import de.platon42.intellij.plugins.cajon.firstArg -import de.platon42.intellij.plugins.cajon.map class AssertThatBooleanConditionInspection : AbstractAssertJInspection() { @@ -23,6 +20,9 @@ class AssertThatBooleanConditionInspection : AbstractAssertJInspection() { return object : JavaElementVisitor() { override fun visitMethodCallExpression(expression: PsiMethodCallExpression) { super.visitMethodCallExpression(expression) + if (!expression.hasAssertThat()) { + return + } val matchingCalls = listOf( IS_EQUAL_TO_OBJECT, IS_EQUAL_TO_BOOLEAN, IS_NOT_EQUAL_TO_OBJECT, IS_NOT_EQUAL_TO_BOOLEAN diff --git a/src/main/java/de/platon42/intellij/plugins/cajon/inspections/AssertThatEnumerableIsEmptyInspection.kt b/src/main/java/de/platon42/intellij/plugins/cajon/inspections/AssertThatEnumerableIsEmptyInspection.kt index b1869b9..dc86576 100644 --- a/src/main/java/de/platon42/intellij/plugins/cajon/inspections/AssertThatEnumerableIsEmptyInspection.kt +++ b/src/main/java/de/platon42/intellij/plugins/cajon/inspections/AssertThatEnumerableIsEmptyInspection.kt @@ -7,6 +7,7 @@ import com.intellij.psi.PsiMethodCallExpression import com.intellij.psi.PsiStatement import de.platon42.intellij.plugins.cajon.MethodNames import de.platon42.intellij.plugins.cajon.calculateConstantParameterValue +import de.platon42.intellij.plugins.cajon.hasAssertThat class AssertThatEnumerableIsEmptyInspection : AbstractAssertJInspection() { @@ -20,6 +21,9 @@ class AssertThatEnumerableIsEmptyInspection : AbstractAssertJInspection() { return object : JavaElementVisitor() { override fun visitMethodCallExpression(expression: PsiMethodCallExpression) { super.visitMethodCallExpression(expression) + if (!expression.hasAssertThat()) { + return + } val isLastExpression = expression.parent is PsiStatement if (!(HAS_SIZE.test(expression) && isLastExpression)) { return diff --git a/src/main/java/de/platon42/intellij/plugins/cajon/inspections/AssertThatGuavaOptionalInspection.kt b/src/main/java/de/platon42/intellij/plugins/cajon/inspections/AssertThatGuavaOptionalInspection.kt index 455ef97..d97f216 100644 --- a/src/main/java/de/platon42/intellij/plugins/cajon/inspections/AssertThatGuavaOptionalInspection.kt +++ b/src/main/java/de/platon42/intellij/plugins/cajon/inspections/AssertThatGuavaOptionalInspection.kt @@ -19,9 +19,12 @@ class AssertThatGuavaOptionalInspection : AbstractAssertJInspection() { override fun buildVisitor(holder: ProblemsHolder, isOnTheFly: Boolean): PsiElementVisitor { return object : JavaElementVisitor() { - override fun visitExpressionStatement(statement: PsiExpressionStatement?) { + override fun visitExpressionStatement(statement: PsiExpressionStatement) { super.visitExpressionStatement(statement) - val staticMethodCall = statement?.findStaticMethodCall() ?: return + if (!statement.hasAssertThat()) { + return + } + val staticMethodCall = statement.findStaticMethodCall() ?: return if (!checkPreconditions(staticMethodCall)) { return @@ -53,6 +56,9 @@ class AssertThatGuavaOptionalInspection : AbstractAssertJInspection() { override fun visitMethodCallExpression(expression: PsiMethodCallExpression) { super.visitMethodCallExpression(expression) + if (!expression.hasAssertThat()) { + return + } val staticMethodCall = expression.findStaticMethodCall() ?: return if (!checkPreconditions(staticMethodCall)) { return diff --git a/src/main/java/de/platon42/intellij/plugins/cajon/inspections/AssertThatInstanceOfInspection.kt b/src/main/java/de/platon42/intellij/plugins/cajon/inspections/AssertThatInstanceOfInspection.kt index 43eb833..ce3d533 100644 --- a/src/main/java/de/platon42/intellij/plugins/cajon/inspections/AssertThatInstanceOfInspection.kt +++ b/src/main/java/de/platon42/intellij/plugins/cajon/inspections/AssertThatInstanceOfInspection.kt @@ -18,9 +18,12 @@ class AssertThatInstanceOfInspection : AbstractAssertJInspection() { override fun buildVisitor(holder: ProblemsHolder, isOnTheFly: Boolean): PsiElementVisitor { return object : JavaElementVisitor() { - override fun visitExpressionStatement(statement: PsiExpressionStatement?) { + override fun visitExpressionStatement(statement: PsiExpressionStatement) { super.visitExpressionStatement(statement) - val staticMethodCall = statement?.findStaticMethodCall() ?: return + if (!statement.hasAssertThat()) { + return + } + val staticMethodCall = statement.findStaticMethodCall() ?: return if (!ASSERT_THAT_BOOLEAN.test(staticMethodCall)) { return } diff --git a/src/main/java/de/platon42/intellij/plugins/cajon/inspections/AssertThatInvertedBooleanConditionInspection.kt b/src/main/java/de/platon42/intellij/plugins/cajon/inspections/AssertThatInvertedBooleanConditionInspection.kt index 12c0e5c..0dba5b5 100644 --- a/src/main/java/de/platon42/intellij/plugins/cajon/inspections/AssertThatInvertedBooleanConditionInspection.kt +++ b/src/main/java/de/platon42/intellij/plugins/cajon/inspections/AssertThatInvertedBooleanConditionInspection.kt @@ -2,10 +2,7 @@ package de.platon42.intellij.plugins.cajon.inspections import com.intellij.codeInspection.ProblemsHolder import com.intellij.psi.* -import de.platon42.intellij.plugins.cajon.findOutmostMethodCall -import de.platon42.intellij.plugins.cajon.findStaticMethodCall -import de.platon42.intellij.plugins.cajon.firstArg -import de.platon42.intellij.plugins.cajon.getExpectedBooleanResult +import de.platon42.intellij.plugins.cajon.* import de.platon42.intellij.plugins.cajon.quickfixes.InvertUnaryStatementQuickFix class AssertThatInvertedBooleanConditionInspection : AbstractAssertJInspection() { @@ -21,6 +18,9 @@ class AssertThatInvertedBooleanConditionInspection : AbstractAssertJInspection() return object : JavaElementVisitor() { override fun visitMethodCallExpression(expression: PsiMethodCallExpression) { super.visitMethodCallExpression(expression) + if (!expression.hasAssertThat()) { + return + } val staticMethodCall = expression.findStaticMethodCall() ?: return if (!ASSERT_THAT_BOOLEAN.test(staticMethodCall)) { return diff --git a/src/main/java/de/platon42/intellij/plugins/cajon/inspections/AssertThatJava8OptionalInspection.kt b/src/main/java/de/platon42/intellij/plugins/cajon/inspections/AssertThatJava8OptionalInspection.kt index ab46c3d..9dd0546 100644 --- a/src/main/java/de/platon42/intellij/plugins/cajon/inspections/AssertThatJava8OptionalInspection.kt +++ b/src/main/java/de/platon42/intellij/plugins/cajon/inspections/AssertThatJava8OptionalInspection.kt @@ -21,9 +21,12 @@ class AssertThatJava8OptionalInspection : AbstractAssertJInspection() { override fun buildVisitor(holder: ProblemsHolder, isOnTheFly: Boolean): PsiElementVisitor { return object : JavaElementVisitor() { - override fun visitExpressionStatement(statement: PsiExpressionStatement?) { + override fun visitExpressionStatement(statement: PsiExpressionStatement) { super.visitExpressionStatement(statement) - val staticMethodCall = statement?.findStaticMethodCall() ?: return + if (!statement.hasAssertThat()) { + return + } + val staticMethodCall = statement.findStaticMethodCall() ?: return if (!ASSERT_THAT_ANY.test(staticMethodCall)) { return } @@ -52,6 +55,9 @@ class AssertThatJava8OptionalInspection : AbstractAssertJInspection() { override fun visitMethodCallExpression(expression: PsiMethodCallExpression) { super.visitMethodCallExpression(expression) + if (!expression.hasAssertThat()) { + return + } val staticMethodCall = expression.findStaticMethodCall() ?: return if (!ASSERT_THAT_JAVA8_OPTIONAL.test(staticMethodCall)) { return diff --git a/src/main/java/de/platon42/intellij/plugins/cajon/inspections/AssertThatObjectIsNullOrNotNullInspection.kt b/src/main/java/de/platon42/intellij/plugins/cajon/inspections/AssertThatObjectIsNullOrNotNullInspection.kt index 30296e8..301ac74 100644 --- a/src/main/java/de/platon42/intellij/plugins/cajon/inspections/AssertThatObjectIsNullOrNotNullInspection.kt +++ b/src/main/java/de/platon42/intellij/plugins/cajon/inspections/AssertThatObjectIsNullOrNotNullInspection.kt @@ -4,6 +4,7 @@ import com.intellij.codeInspection.ProblemsHolder import com.intellij.psi.* import de.platon42.intellij.plugins.cajon.MethodNames import de.platon42.intellij.plugins.cajon.firstArg +import de.platon42.intellij.plugins.cajon.hasAssertThat import de.platon42.intellij.plugins.cajon.map class AssertThatObjectIsNullOrNotNullInspection : AbstractAssertJInspection() { @@ -18,6 +19,9 @@ class AssertThatObjectIsNullOrNotNullInspection : AbstractAssertJInspection() { return object : JavaElementVisitor() { override fun visitMethodCallExpression(expression: PsiMethodCallExpression) { super.visitMethodCallExpression(expression) + if (!expression.hasAssertThat()) { + return + } val isNotEqualTo = IS_NOT_EQUAL_TO_OBJECT.test(expression) val isEqualTo = IS_EQUAL_TO_OBJECT.test(expression) val isLastExpression = expression.parent is PsiStatement diff --git a/src/main/java/de/platon42/intellij/plugins/cajon/inspections/AssertThatSizeInspection.kt b/src/main/java/de/platon42/intellij/plugins/cajon/inspections/AssertThatSizeInspection.kt index e84d1d1..eaa71ff 100644 --- a/src/main/java/de/platon42/intellij/plugins/cajon/inspections/AssertThatSizeInspection.kt +++ b/src/main/java/de/platon42/intellij/plugins/cajon/inspections/AssertThatSizeInspection.kt @@ -72,9 +72,12 @@ class AssertThatSizeInspection : AbstractAssertJInspection() { override fun buildVisitor(holder: ProblemsHolder, isOnTheFly: Boolean): PsiElementVisitor { return object : JavaElementVisitor() { - override fun visitExpressionStatement(statement: PsiExpressionStatement?) { + override fun visitExpressionStatement(statement: PsiExpressionStatement) { super.visitExpressionStatement(statement) - val staticMethodCall = statement?.findStaticMethodCall() ?: return + if (!statement.hasAssertThat()) { + return + } + val staticMethodCall = statement.findStaticMethodCall() ?: return if (!ASSERT_THAT_INT.test(staticMethodCall)) { return } @@ -109,6 +112,9 @@ class AssertThatSizeInspection : AbstractAssertJInspection() { override fun visitMethodCallExpression(expression: PsiMethodCallExpression) { super.visitMethodCallExpression(expression) + if (!expression.hasAssertThat()) { + return + } val isHasSize = HAS_SIZE.test(expression) if (!(isHasSize)) { return diff --git a/src/main/java/de/platon42/intellij/plugins/cajon/inspections/AssertThatStringExpressionInspection.kt b/src/main/java/de/platon42/intellij/plugins/cajon/inspections/AssertThatStringExpressionInspection.kt index 904296e..03dba76 100644 --- a/src/main/java/de/platon42/intellij/plugins/cajon/inspections/AssertThatStringExpressionInspection.kt +++ b/src/main/java/de/platon42/intellij/plugins/cajon/inspections/AssertThatStringExpressionInspection.kt @@ -46,9 +46,12 @@ class AssertThatStringExpressionInspection : AbstractAssertJInspection() { override fun buildVisitor(holder: ProblemsHolder, isOnTheFly: Boolean): PsiElementVisitor { return object : JavaElementVisitor() { - override fun visitExpressionStatement(statement: PsiExpressionStatement?) { + override fun visitExpressionStatement(statement: PsiExpressionStatement) { super.visitExpressionStatement(statement) - val staticMethodCall = statement?.findStaticMethodCall() ?: return + if (!statement.hasAssertThat()) { + return + } + val staticMethodCall = statement.findStaticMethodCall() ?: return if (!ASSERT_THAT_BOOLEAN.test(staticMethodCall)) { return } diff --git a/src/main/java/de/platon42/intellij/plugins/cajon/inspections/AssertThatStringIsEmptyInspection.kt b/src/main/java/de/platon42/intellij/plugins/cajon/inspections/AssertThatStringIsEmptyInspection.kt index 10c0a92..2f67226 100644 --- a/src/main/java/de/platon42/intellij/plugins/cajon/inspections/AssertThatStringIsEmptyInspection.kt +++ b/src/main/java/de/platon42/intellij/plugins/cajon/inspections/AssertThatStringIsEmptyInspection.kt @@ -8,6 +8,7 @@ import com.intellij.psi.PsiStatement import de.platon42.intellij.plugins.cajon.AssertJClassNames.Companion.ABSTRACT_CHAR_SEQUENCE_ASSERT_CLASSNAME import de.platon42.intellij.plugins.cajon.MethodNames import de.platon42.intellij.plugins.cajon.calculateConstantParameterValue +import de.platon42.intellij.plugins.cajon.hasAssertThat class AssertThatStringIsEmptyInspection : AbstractAssertJInspection() { @@ -21,6 +22,9 @@ class AssertThatStringIsEmptyInspection : AbstractAssertJInspection() { return object : JavaElementVisitor() { override fun visitMethodCallExpression(expression: PsiMethodCallExpression) { super.visitMethodCallExpression(expression) + if (!expression.hasAssertThat()) { + return + } val isEqual = IS_EQUAL_TO_OBJECT.test(expression) val hasSize = HAS_SIZE.test(expression) val isLastExpression = expression.parent is PsiStatement diff --git a/src/main/java/de/platon42/intellij/plugins/cajon/inspections/JoinAssertThatStatementsInspection.kt b/src/main/java/de/platon42/intellij/plugins/cajon/inspections/JoinAssertThatStatementsInspection.kt index fc79c28..a2a01a6 100644 --- a/src/main/java/de/platon42/intellij/plugins/cajon/inspections/JoinAssertThatStatementsInspection.kt +++ b/src/main/java/de/platon42/intellij/plugins/cajon/inspections/JoinAssertThatStatementsInspection.kt @@ -19,15 +19,14 @@ class JoinAssertThatStatementsInspection : AbstractAssertJInspection() { override fun buildVisitor(holder: ProblemsHolder, isOnTheFly: Boolean): PsiElementVisitor { return object : JavaElementVisitor() { - override fun visitCodeBlock(block: PsiCodeBlock?) { + override fun visitCodeBlock(block: PsiCodeBlock) { super.visitCodeBlock(block) - val statements = block?.statements ?: return var lastActualExpression: PsiExpression? = null var sameCount = 0 var firstStatement: PsiStatement? = null var lastStatement: PsiStatement? = null val equivalenceChecker = TrackingEquivalenceChecker() - for (statement in statements) { + for (statement in block.statements) { val assertThatCall = isLegitAssertThatCall(statement) var reset = true var actualExpression: PsiExpression? = null @@ -81,6 +80,9 @@ class JoinAssertThatStatementsInspection : AbstractAssertJInspection() { private fun isLegitAssertThatCall(statement: PsiStatement?): PsiMethodCallExpression? { if ((statement is PsiExpressionStatement) && (statement.expression is PsiMethodCallExpression)) { + if (!statement.hasAssertThat()) { + return null + } val assertThatCall = PsiTreeUtil.findChildrenOfType(statement, PsiMethodCallExpression::class.java).find { ALL_ASSERT_THAT_MATCHERS.test(it) } return assertThatCall?.takeIf { it.findFluentCallTo(EXTRACTING_CALL_MATCHERS) == null } }