Fix for AssertThatStringIsEmpty, refactored method.

This commit is contained in:
Chris Hodges 2019-03-24 11:37:46 +01:00
parent 5fa61a3004
commit 458542de7c
5 changed files with 22 additions and 9 deletions

View File

@ -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
}
}

View File

@ -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

View File

@ -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() {
}
}
}
}

View File

@ -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("");
}
}

View File

@ -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("");
}
}