From 458542de7c4433240814b64b76716849c6a0cbe4 Mon Sep 17 00:00:00 2001 From: chrisly42 Date: Sun, 24 Mar 2019 11:37:46 +0100 Subject: [PATCH] Fix for AssertThatStringIsEmpty, refactored method. --- .../cajon/inspections/AbstractAssertJInspection.kt | 10 ++++++++++ .../AssertThatBooleanIsTrueOrFalseInspection.kt | 10 ++++------ .../inspections/AssertThatStringIsEmptyInspection.kt | 5 ++++- .../inspections/StringIsEmpty/StringIsEmptyAfter.java | 3 ++- .../inspections/StringIsEmpty/StringIsEmptyBefore.java | 3 ++- 5 files changed, 22 insertions(+), 9 deletions(-) diff --git a/src/main/java/de/platon42/intellij/plugins/cajon/inspections/AbstractAssertJInspection.kt b/src/main/java/de/platon42/intellij/plugins/cajon/inspections/AbstractAssertJInspection.kt index 58f6a01..1cd7b95 100644 --- a/src/main/java/de/platon42/intellij/plugins/cajon/inspections/AbstractAssertJInspection.kt +++ b/src/main/java/de/platon42/intellij/plugins/cajon/inspections/AbstractAssertJInspection.kt @@ -2,6 +2,8 @@ package de.platon42.intellij.plugins.cajon.inspections import com.intellij.codeInspection.AbstractBaseJavaLocalInspectionTool import com.intellij.psi.CommonClassNames +import com.intellij.psi.PsiCapturedWildcardType +import com.intellij.psi.PsiMethodCallExpression import com.siyeh.ig.callMatcher.CallMatcher open class AbstractAssertJInspection : AbstractBaseJavaLocalInspectionTool() { @@ -26,4 +28,12 @@ open class AbstractAssertJInspection : AbstractBaseJavaLocalInspectionTool() { override fun getGroupDisplayName(): String { return "AssertJ" } + + protected fun checkAssertedType(expression: PsiMethodCallExpression, prefix: String): Boolean { + var assertedType = expression.methodExpression.qualifierExpression?.type + if (assertedType is PsiCapturedWildcardType) { + assertedType = assertedType.upperBound + } + return assertedType?.canonicalText?.startsWith(prefix) ?: false + } } \ No newline at end of file diff --git a/src/main/java/de/platon42/intellij/plugins/cajon/inspections/AssertThatBooleanIsTrueOrFalseInspection.kt b/src/main/java/de/platon42/intellij/plugins/cajon/inspections/AssertThatBooleanIsTrueOrFalseInspection.kt index c8bb027..2305846 100644 --- a/src/main/java/de/platon42/intellij/plugins/cajon/inspections/AssertThatBooleanIsTrueOrFalseInspection.kt +++ b/src/main/java/de/platon42/intellij/plugins/cajon/inspections/AssertThatBooleanIsTrueOrFalseInspection.kt @@ -39,14 +39,12 @@ class AssertThatBooleanIsTrueOrFalseInspection : AbstractAssertJInspection() { if (!(normalBooleanTest || flippedBooleanTest)) { return } - var assertedType = expression.methodExpression.qualifierExpression?.type - if (assertedType is PsiCapturedWildcardType) { - assertedType = assertedType.upperBound + if (!checkAssertedType(expression, ABSTRACT_BOOLEAN_ASSERT_CLASSNAME)) { + return } - val assertedTypeIsBoolean = - assertedType?.canonicalText?.startsWith(ABSTRACT_BOOLEAN_ASSERT_CLASSNAME) ?: false + val equalToExpression = expression.argumentList.expressions[0]!! - if (!TypeConversionUtil.isBooleanType(equalToExpression.type) || !assertedTypeIsBoolean) { + if (!TypeConversionUtil.isBooleanType(equalToExpression.type)) { return } val constantEvaluationHelper = JavaPsiFacade.getInstance(holder.project).constantEvaluationHelper 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 17fda36..b604128 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 @@ -33,6 +33,10 @@ class AssertThatStringIsEmptyInspection : AbstractAssertJInspection() { return } + if (!checkAssertedType(expression, ABSTRACT_STRING_ASSERT_CLASSNAME)) { + return + } + val psiExpression = expression.argumentList.expressions[0] as? PsiLiteralExpression ?: return if (psiExpression.value == "") { @@ -47,5 +51,4 @@ class AssertThatStringIsEmptyInspection : AbstractAssertJInspection() { } } } - } \ No newline at end of file diff --git a/src/test/resources/inspections/StringIsEmpty/StringIsEmptyAfter.java b/src/test/resources/inspections/StringIsEmpty/StringIsEmptyAfter.java index e7031a5..7b17cad 100644 --- a/src/test/resources/inspections/StringIsEmpty/StringIsEmptyAfter.java +++ b/src/test/resources/inspections/StringIsEmpty/StringIsEmptyAfter.java @@ -6,6 +6,7 @@ public class StringIsEmpty { String string = "string"; assertThat(string).isEqualTo("foo"); - assertThat(string).isEmpty(); + assertThat(string).as("foo").isEmpty(); + assertThat(new Object()).isEqualTo(""); } } diff --git a/src/test/resources/inspections/StringIsEmpty/StringIsEmptyBefore.java b/src/test/resources/inspections/StringIsEmpty/StringIsEmptyBefore.java index 4e32519..f8b7dab 100644 --- a/src/test/resources/inspections/StringIsEmpty/StringIsEmptyBefore.java +++ b/src/test/resources/inspections/StringIsEmpty/StringIsEmptyBefore.java @@ -6,6 +6,7 @@ public class StringIsEmpty { String string = "string"; assertThat(string).isEqualTo("foo"); - assertThat(string).isEqualTo(""); + assertThat(string).as("foo").isEqualTo(""); + assertThat(new Object()).isEqualTo(""); } }