diff --git a/README.md b/README.md
index 29f37f5..9b05a29 100644
--- a/README.md
+++ b/README.md
@@ -39,6 +39,9 @@ Then AssertJ would tell you the contents of the collection on failure.
> from: assertThat(charSequence/string).isEqualTo("");
> from: assertThat(charSequence/string).hasSize(0);
> to: assertThat(charSequence/string).isEmpty();
+- AssertThatEnumerableIsEmpty
+ > from: assertThat(enumerable).hasSize(0);
+ > to: assertThat(charSequence/string).isEmpty();
## TODO
- AssertThatArrayHasLiteralSize
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 26fbc6b..b6875f9 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
@@ -27,19 +27,22 @@ open class AbstractAssertJInspection : AbstractBaseJavaLocalInspectionTool() {
const val ABSTRACT_BOOLEAN_ASSERT_CLASSNAME = "org.assertj.core.api.AbstractBooleanAssert"
const val ABSTRACT_STRING_ASSERT_CLASSNAME = "org.assertj.core.api.AbstractStringAssert"
const val ABSTRACT_CHAR_SEQUENCE_ASSERT_CLASSNAME = "org.assertj.core.api.AbstractCharSequenceAssert"
- const val IS_EQUAL_TO = "isEqualTo"
- const val IS_NOT_EQUAL_TO = "isNotEqualTo"
- const val HAS_SIZE = "hasSize"
+ const val ABSTRACT_ENUMERABLE_ASSERT_CLASSNAME = "org.assertj.core.api.EnumerableAssert"
- val IS_EQUAL_TO_OBJECT = CallMatcher.instanceCall(ABSTRACT_ASSERT_CLASSNAME, IS_EQUAL_TO)
+ const val IS_EQUAL_TO_METHOD = "isEqualTo"
+ const val IS_NOT_EQUAL_TO_METHOD = "isNotEqualTo"
+ const val HAS_SIZE_METHOD = "hasSize"
+
+ val IS_EQUAL_TO_OBJECT = CallMatcher.instanceCall(ABSTRACT_ASSERT_CLASSNAME, IS_EQUAL_TO_METHOD)
.parameterTypes(CommonClassNames.JAVA_LANG_OBJECT)!!
- val IS_NOT_EQUAL_TO_OBJECT = CallMatcher.instanceCall(ABSTRACT_ASSERT_CLASSNAME, IS_NOT_EQUAL_TO)
+ val IS_NOT_EQUAL_TO_OBJECT = CallMatcher.instanceCall(ABSTRACT_ASSERT_CLASSNAME, IS_NOT_EQUAL_TO_METHOD)
.parameterTypes(CommonClassNames.JAVA_LANG_OBJECT)!!
- val IS_EQUAL_TO_BOOLEAN = CallMatcher.instanceCall(ABSTRACT_BOOLEAN_ASSERT_CLASSNAME, IS_EQUAL_TO)
+ val IS_EQUAL_TO_BOOLEAN = CallMatcher.instanceCall(ABSTRACT_BOOLEAN_ASSERT_CLASSNAME, IS_EQUAL_TO_METHOD)
.parameterTypes("boolean")!!
- val IS_NOT_EQUAL_TO_BOOLEAN = CallMatcher.instanceCall(ABSTRACT_BOOLEAN_ASSERT_CLASSNAME, IS_NOT_EQUAL_TO)
- .parameterTypes("boolean")!!
- val CHAR_SEQUENCE_HAS_SIZE = CallMatcher.instanceCall(ABSTRACT_CHAR_SEQUENCE_ASSERT_CLASSNAME, HAS_SIZE)
+ val IS_NOT_EQUAL_TO_BOOLEAN =
+ CallMatcher.instanceCall(ABSTRACT_BOOLEAN_ASSERT_CLASSNAME, IS_NOT_EQUAL_TO_METHOD)
+ .parameterTypes("boolean")!!
+ val HAS_SIZE = CallMatcher.instanceCall(ABSTRACT_ENUMERABLE_ASSERT_CLASSNAME, HAS_SIZE_METHOD)
.parameterTypes("int")!!
}
@@ -77,4 +80,10 @@ open class AbstractAssertJInspection : AbstractBaseJavaLocalInspectionTool() {
ReplaceSimpleMethodCallQuickFix(description, replacementMethod)
)
}
+
+ protected fun calculateConstantParameterValue(expression: PsiMethodCallExpression, argIndex: Int): Any? {
+ val valueExpression = expression.argumentList.expressions[argIndex] ?: return null
+ val constantEvaluationHelper = JavaPsiFacade.getInstance(expression.project).constantEvaluationHelper
+ return constantEvaluationHelper.computeConstantExpression(valueExpression)
+ }
}
\ 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 92935ad..da9b19b 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
@@ -31,22 +31,21 @@ class AssertThatBooleanIsTrueOrFalseInspection : AbstractAssertJInspection() {
return
}
- val equalToExpression = expression.argumentList.expressions[0]!!
+ val equalToExpression = expression.argumentList.expressions[0] ?: return
if (!TypeConversionUtil.isBooleanType(equalToExpression.type)) {
return
}
- val constantEvaluationHelper = JavaPsiFacade.getInstance(holder.project).constantEvaluationHelper
- var result = constantEvaluationHelper.computeConstantExpression(equalToExpression)
- if (result == null) {
+ var value = calculateConstantParameterValue(expression, 0)
+ if (value == null) {
val field = (equalToExpression as? PsiReferenceExpression)?.resolve() as? PsiField
if (field?.containingClass?.qualifiedName == CommonClassNames.JAVA_LANG_BOOLEAN) {
when {
- field.name == "TRUE" -> result = true
- field.name == "FALSE" -> result = false
+ field.name == "TRUE" -> value = true
+ field.name == "FALSE" -> value = false
}
}
}
- val expectedResult = result as? Boolean ?: return
+ val expectedResult = value as? Boolean ?: return
val replacementMethod = if (expectedResult xor flippedBooleanTest) "isTrue()" else "isFalse()"
registerSimplifyMethod(holder, expression, replacementMethod)
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
new file mode 100644
index 0000000..c1fe957
--- /dev/null
+++ b/src/main/java/de/platon42/intellij/plugins/cajon/inspections/AssertThatEnumerableIsEmptyInspection.kt
@@ -0,0 +1,34 @@
+package de.platon42.intellij.plugins.cajon.inspections
+
+import com.intellij.codeInspection.ProblemsHolder
+import com.intellij.psi.JavaElementVisitor
+import com.intellij.psi.PsiElementVisitor
+import com.intellij.psi.PsiMethodCallExpression
+import org.jetbrains.annotations.NonNls
+
+class AssertThatEnumerableIsEmptyInspection : AbstractAssertJInspection() {
+
+ companion object {
+ @NonNls
+ private val DISPLAY_NAME = "Asserting an enumerable is empty"
+ }
+
+ override fun getDisplayName() = DISPLAY_NAME
+
+ override fun buildVisitor(holder: ProblemsHolder, isOnTheFly: Boolean): PsiElementVisitor {
+ return object : JavaElementVisitor() {
+ override fun visitMethodCallExpression(expression: PsiMethodCallExpression) {
+ super.visitMethodCallExpression(expression)
+ val hasSize = HAS_SIZE.test(expression)
+ if (!hasSize) {
+ return
+ }
+
+ val value = calculateConstantParameterValue(expression, 0) ?: return
+ if (value == 0) {
+ registerSimplifyMethod(holder, expression, "isEmpty()")
+ }
+ }
+ }
+ }
+}
\ No newline at end of file
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 f6af10c..bb7bf8f 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
@@ -3,7 +3,6 @@ package de.platon42.intellij.plugins.cajon.inspections
import com.intellij.codeInspection.ProblemsHolder
import com.intellij.psi.JavaElementVisitor
import com.intellij.psi.PsiElementVisitor
-import com.intellij.psi.PsiLiteralExpression
import com.intellij.psi.PsiMethodCallExpression
import org.jetbrains.annotations.NonNls
@@ -21,7 +20,7 @@ class AssertThatStringIsEmptyInspection : AbstractAssertJInspection() {
override fun visitMethodCallExpression(expression: PsiMethodCallExpression) {
super.visitMethodCallExpression(expression)
val isEqual = IS_EQUAL_TO_OBJECT.test(expression)
- val hasSize = CHAR_SEQUENCE_HAS_SIZE.test(expression)
+ val hasSize = HAS_SIZE.test(expression)
if (!(isEqual || hasSize)) {
return
}
@@ -30,17 +29,9 @@ class AssertThatStringIsEmptyInspection : AbstractAssertJInspection() {
return
}
- if (isEqual) {
- val psiExpression = expression.argumentList.expressions[0] as? PsiLiteralExpression ?: return
-
- if (psiExpression.value == "") {
- registerSimplifyMethod(holder, expression, "isEmpty()")
- }
- } else {
- val psiExpression = expression.argumentList.expressions[0] as? PsiLiteralExpression ?: return
- if (psiExpression.value == 0) {
- registerSimplifyMethod(holder, expression, "isEmpty()")
- }
+ val value = calculateConstantParameterValue(expression, 0) ?: return
+ if ((isEqual && (value == "")) || (hasSize && (value == 0))) {
+ registerSimplifyMethod(holder, expression, "isEmpty()")
}
}
}
diff --git a/src/main/resources/META-INF/plugin.xml b/src/main/resources/META-INF/plugin.xml
index 7c8bb09..385e11e 100644
--- a/src/main/resources/META-INF/plugin.xml
+++ b/src/main/resources/META-INF/plugin.xml
@@ -27,6 +27,9 @@
implementationClass="de.platon42.intellij.plugins.cajon.inspections.AssertThatBooleanIsTrueOrFalseInspection"/>
+
diff --git a/src/main/resources/inspectionDescriptions/AssertThatBooleanIsTrueOrFalse.html b/src/main/resources/inspectionDescriptions/AssertThatBooleanIsTrueOrFalse.html
index d87d6de..e020310 100644
--- a/src/main/resources/inspectionDescriptions/AssertThatBooleanIsTrueOrFalse.html
+++ b/src/main/resources/inspectionDescriptions/AssertThatBooleanIsTrueOrFalse.html
@@ -3,5 +3,6 @@
Turns assertThat(booleanExpression).isEqualTo(true/false) or assertThat(booleanExpression).isNotEqualTo(true/false)
into assertThat(booleanExpression).isTrue() or assertThat(booleanExpression).isFalse().
+
Also works with Boolean.TRUE/FALSE.