Optimized code. More kotlin-style stuff. Made detecting of Boolean.TRUE/FALSE available to all constant evaluations.

This commit is contained in:
Chris Hodges 2019-04-08 11:54:22 +02:00
parent bdabcc7255
commit c1d8ade7b1
15 changed files with 154 additions and 127 deletions

View File

@ -5,7 +5,7 @@ plugins {
} }
group 'de.platon42' group 'de.platon42'
version '0.3' version '0.4'
repositories { repositories {
mavenCentral() mavenCentral()
@ -41,23 +41,27 @@ intellij {
patchPluginXml { patchPluginXml {
changeNotes """ changeNotes """
<h4>V0.3 (07-Apr-19)</h4> <h4>V0.4 (xx-Apr-19)</h4>
<ul> <ul>
<li>Yo.
</ul>
<h4>V0.3 (07-Apr-19)</h4>
<ul>
<li>New inspection AssertThatBinaryExpressionIsTrueOrFalse that will find and fix common binary expressions and equals() statements (more than 150 combinations) inside assertThat(). <li>New inspection AssertThatBinaryExpressionIsTrueOrFalse that will find and fix common binary expressions and equals() statements (more than 150 combinations) inside assertThat().
<li>Merged AssertThatObjectIsNull and AssertThatObjectIsNotNull to AssertThatObjectIsNullOrNotNull. <li>Merged AssertThatObjectIsNull and AssertThatObjectIsNotNull to AssertThatObjectIsNullOrNotNull.
<li>Support for hasSizeLessThan(), hasSizeLessThanOrEqualTo(), hasSizeGreaterThanOrEqualTo(), and hasSizeGreaterThan() for AssertThatSizeInspection (with AssertJ >=13.2.0). <li>Support for hasSizeLessThan(), hasSizeLessThanOrEqualTo(), hasSizeGreaterThanOrEqualTo(), and hasSizeGreaterThan() for AssertThatSizeInspection (with AssertJ >=13.2.0).
<li>Really fixed highlighting for JUnit conversion. Sorry. <li>Really fixed highlighting for JUnit conversion. Sorry.
</ul> </ul>
<h4>V0.2 (01-Apr-19)</h4> <h4>V0.2 (01-Apr-19)</h4>
<ul> <ul>
<li>Fixed descriptions and quick fix texts. <li>Fixed descriptions and quick fix texts.
<li>Fixed highlighting of found problems and also 'Run inspection by Name' returning nothing. <li>Fixed highlighting of found problems and also 'Run inspection by Name' returning nothing.
</ul> </ul>
<h4>V0.1 (31-Mar-19)</h4> <h4>V0.1 (31-Mar-19)</h4>
<ul> <ul>
<li>Initial release. <li>Initial release.
</ul> </ul>
""" """
} }
test { test {

View File

@ -0,0 +1,17 @@
package de.platon42.intellij.plugins.cajon
import com.intellij.psi.PsiExpression
import com.intellij.psi.PsiMethodCallExpression
val PsiMethodCallExpression.qualifierExpression: PsiExpression get() = this.methodExpression.qualifierExpression!!
val PsiMethodCallExpression.firstArg: PsiExpression get() = this.argumentList.expressions[0]!!
fun PsiMethodCallExpression.replaceQualifier(qualifier: PsiExpression) {
this.qualifierExpression.replace(qualifier)
}
fun PsiMethodCallExpression.replaceQualifierFromMethodCall(oldMethodCall: PsiMethodCallExpression) {
this.qualifierExpression.replace(oldMethodCall.qualifierExpression)
}
fun PsiMethodCallExpression.getArg(n: Int): PsiExpression = this.argumentList.expressions[n]

View File

@ -6,6 +6,8 @@ import com.intellij.psi.*
import com.intellij.psi.search.GlobalSearchScope import com.intellij.psi.search.GlobalSearchScope
import com.intellij.psi.util.PsiTypesUtil import com.intellij.psi.util.PsiTypesUtil
import com.siyeh.ig.callMatcher.CallMatcher import com.siyeh.ig.callMatcher.CallMatcher
import de.platon42.intellij.plugins.cajon.getArg
import de.platon42.intellij.plugins.cajon.qualifierExpression
import de.platon42.intellij.plugins.cajon.quickfixes.ReplaceSimpleMethodCallQuickFix import de.platon42.intellij.plugins.cajon.quickfixes.ReplaceSimpleMethodCallQuickFix
import org.jetbrains.annotations.NonNls import org.jetbrains.annotations.NonNls
@ -110,8 +112,8 @@ open class AbstractAssertJInspection : AbstractBaseJavaLocalInspectionTool() {
} }
protected fun checkAssertedType(expression: PsiMethodCallExpression, classname: String): Boolean { protected fun checkAssertedType(expression: PsiMethodCallExpression, classname: String): Boolean {
var assertedType = expression.methodExpression.qualifierExpression?.type ?: return false var assertedType = expression.qualifierExpression.type ?: return false
if (assertedType is PsiCapturedWildcardType) { while (assertedType is PsiCapturedWildcardType) {
assertedType = assertedType.upperBound assertedType = assertedType.upperBound
} }
val assertedClass = PsiTypesUtil.getPsiClass(assertedType) ?: return false val assertedClass = PsiTypesUtil.getPsiClass(assertedType) ?: return false
@ -131,18 +133,26 @@ open class AbstractAssertJInspection : AbstractBaseJavaLocalInspectionTool() {
val originalMethod = getOriginalMethodName(expression) ?: return val originalMethod = getOriginalMethodName(expression) ?: return
val description = REPLACE_DESCRIPTION_TEMPLATE.format(originalMethod, replacementMethod) val description = REPLACE_DESCRIPTION_TEMPLATE.format(originalMethod, replacementMethod)
val message = SIMPLIFY_MESSAGE_TEMPLATE.format(originalMethod, replacementMethod) val message = SIMPLIFY_MESSAGE_TEMPLATE.format(originalMethod, replacementMethod)
holder.registerProblem( val quickFix = ReplaceSimpleMethodCallQuickFix(description, replacementMethod)
expression, holder.registerProblem(expression, message, quickFix)
message,
ReplaceSimpleMethodCallQuickFix(description, replacementMethod)
)
} }
protected fun calculateConstantParameterValue(expression: PsiMethodCallExpression, argIndex: Int): Any? { protected fun calculateConstantParameterValue(expression: PsiMethodCallExpression, argIndex: Int): Any? {
if (argIndex >= expression.argumentList.expressionCount) return null if (argIndex >= expression.argumentList.expressionCount) return null
val valueExpression = expression.argumentList.expressions[argIndex] ?: return null val valueExpression = expression.getArg(argIndex)
val constantEvaluationHelper = JavaPsiFacade.getInstance(expression.project).constantEvaluationHelper val constantEvaluationHelper = JavaPsiFacade.getInstance(expression.project).constantEvaluationHelper
return constantEvaluationHelper.computeConstantExpression(valueExpression) val value = constantEvaluationHelper.computeConstantExpression(valueExpression)
if (value == null) {
val field = (valueExpression as? PsiReferenceExpression)?.resolve() as? PsiField
if (field?.containingClass?.qualifiedName == CommonClassNames.JAVA_LANG_BOOLEAN) {
return when (field.name) {
"TRUE" -> true
"FALSE" -> false
else -> null
}
}
}
return value
} }
protected fun hasAssertJMethod(element: PsiElement, classAndMethod: String): Boolean { protected fun hasAssertJMethod(element: PsiElement, classAndMethod: String): Boolean {

View File

@ -5,6 +5,7 @@ import com.intellij.psi.*
import com.intellij.psi.tree.IElementType import com.intellij.psi.tree.IElementType
import com.intellij.psi.util.PsiTreeUtil import com.intellij.psi.util.PsiTreeUtil
import com.intellij.psi.util.TypeConversionUtil import com.intellij.psi.util.TypeConversionUtil
import de.platon42.intellij.plugins.cajon.firstArg
import de.platon42.intellij.plugins.cajon.quickfixes.SplitBinaryExpressionMethodCallQuickFix import de.platon42.intellij.plugins.cajon.quickfixes.SplitBinaryExpressionMethodCallQuickFix
import de.platon42.intellij.plugins.cajon.quickfixes.SplitEqualsExpressionMethodCallQuickFix import de.platon42.intellij.plugins.cajon.quickfixes.SplitEqualsExpressionMethodCallQuickFix
@ -26,19 +27,17 @@ class AssertThatBinaryExpressionIsTrueOrFalseInspection : AbstractAssertJInspect
Mapping(JavaTokenType.LE, "isLessThanOrEqualTo()", "isGreaterThan()") Mapping(JavaTokenType.LE, "isLessThanOrEqualTo()", "isGreaterThan()")
) )
private val PRIMITIVE_MAPPINGS_SWAPPED = listOf(
Mapping(JavaTokenType.EQEQ, "isEqualTo()", "isNotEqualTo()"),
Mapping(JavaTokenType.NE, "isNotEqualTo()", "isEqualTo()"),
Mapping(JavaTokenType.GT, "isLessThan()", "isGreaterThanOrEqualTo()"),
Mapping(JavaTokenType.GE, "isLessThanOrEqualTo()", "isGreaterThan()"),
Mapping(JavaTokenType.LT, "isGreaterThan()", "isLessThanOrEqualTo()"),
Mapping(JavaTokenType.LE, "isGreaterThanOrEqualTo()", "isLessThan()")
)
private val OBJECT_MAPPINGS = listOf( private val OBJECT_MAPPINGS = listOf(
Mapping(JavaTokenType.EQEQ, "isSameAs()", "isNotSameAs()"), Mapping(JavaTokenType.EQEQ, "isSameAs()", "isNotSameAs()"),
Mapping(JavaTokenType.NE, "isNotSameAs()", "isSameAs()") Mapping(JavaTokenType.NE, "isNotSameAs()", "isSameAs()")
) )
private val SWAP_BINARY_OPERATOR = mapOf<IElementType, IElementType>(
JavaTokenType.GT to JavaTokenType.LT,
JavaTokenType.GE to JavaTokenType.LE,
JavaTokenType.LT to JavaTokenType.GT,
JavaTokenType.LE to JavaTokenType.GE
)
} }
override fun getDisplayName() = DISPLAY_NAME override fun getDisplayName() = DISPLAY_NAME
@ -55,14 +54,11 @@ class AssertThatBinaryExpressionIsTrueOrFalseInspection : AbstractAssertJInspect
val expectedCallExpression = PsiTreeUtil.findChildOfType(statement, PsiMethodCallExpression::class.java) ?: return val expectedCallExpression = PsiTreeUtil.findChildOfType(statement, PsiMethodCallExpression::class.java) ?: return
val isInverted = getExpectedResult(expectedCallExpression) ?: return val isInverted = getExpectedResult(expectedCallExpression) ?: return
val assertThatArgument = expression.argumentList.expressions[0] ?: return val assertThatArgument = expression.firstArg
if (assertThatArgument is PsiMethodCallExpression && OBJECT_EQUALS.test(assertThatArgument)) { if (assertThatArgument is PsiMethodCallExpression && OBJECT_EQUALS.test(assertThatArgument)) {
val replacementMethod = if (isInverted) "isNotEqualTo()" else "isEqualTo()" val replacementMethod = if (isInverted) "isNotEqualTo()" else "isEqualTo()"
holder.registerProblem( val quickFix = SplitEqualsExpressionMethodCallQuickFix(SPLIT_EQUALS_EXPRESSION_DESCRIPTION, replacementMethod)
expression, holder.registerProblem(expression, EQUALS_MORE_MEANINGFUL_MESSAGE, quickFix)
EQUALS_MORE_MEANINGFUL_MESSAGE,
SplitEqualsExpressionMethodCallQuickFix(SPLIT_EQUALS_EXPRESSION_DESCRIPTION, replacementMethod)
)
return return
} }
@ -71,8 +67,8 @@ class AssertThatBinaryExpressionIsTrueOrFalseInspection : AbstractAssertJInspect
val leftType = binaryExpression.lOperand.type ?: return val leftType = binaryExpression.lOperand.type ?: return
val rightType = binaryExpression.rOperand?.type ?: return val rightType = binaryExpression.rOperand?.type ?: return
val isLeftNull = TypeConversionUtil.isNullType(leftType) val bothTypes = listOf(leftType, rightType)
val isRightNull = TypeConversionUtil.isNullType(rightType) val (isLeftNull, isRightNull) = bothTypes.map(TypeConversionUtil::isNullType)
if (isLeftNull && isRightNull) { if (isLeftNull && isRightNull) {
return return
} else if (isLeftNull || isRightNull) { } else if (isLeftNull || isRightNull) {
@ -86,15 +82,17 @@ class AssertThatBinaryExpressionIsTrueOrFalseInspection : AbstractAssertJInspect
return return
} }
val isPrimitive = TypeConversionUtil.isPrimitiveAndNotNull(leftType) && TypeConversionUtil.isPrimitiveAndNotNull(rightType) val isPrimitive = bothTypes.all(TypeConversionUtil::isPrimitiveAndNotNull)
val isNumericType = TypeConversionUtil.isNumericType(leftType) && TypeConversionUtil.isNumericType(rightType) val isNumericType = bothTypes.all(TypeConversionUtil::isNumericType)
val constantEvaluationHelper = JavaPsiFacade.getInstance(expression.project).constantEvaluationHelper val constantEvaluationHelper = JavaPsiFacade.getInstance(expression.project).constantEvaluationHelper
val swapExpectedAndActual = constantEvaluationHelper.computeConstantExpression(binaryExpression.lOperand) != null val swapExpectedAndActual = constantEvaluationHelper.computeConstantExpression(binaryExpression.lOperand) != null
val tokenType = binaryExpression.operationSign.tokenType val tokenType = binaryExpression.operationSign.tokenType.let {
if (swapExpectedAndActual) SWAP_BINARY_OPERATOR.getOrDefault(it, it) else it
} ?: return
val mappingToUse = val mappingToUse =
if (isPrimitive || isNumericType) { if (isPrimitive || isNumericType) {
if (swapExpectedAndActual) PRIMITIVE_MAPPINGS_SWAPPED else PRIMITIVE_MAPPINGS PRIMITIVE_MAPPINGS
} else { } else {
OBJECT_MAPPINGS OBJECT_MAPPINGS
} }
@ -127,11 +125,8 @@ class AssertThatBinaryExpressionIsTrueOrFalseInspection : AbstractAssertJInspect
pickRightOperand: Boolean = false, pickRightOperand: Boolean = false,
noExpectedExpression: Boolean = false noExpectedExpression: Boolean = false
) { ) {
holder.registerProblem( val quickFix = SplitBinaryExpressionMethodCallQuickFix(SPLIT_BINARY_EXPRESSION_DESCRIPTION, replacementMethod, pickRightOperand, noExpectedExpression)
expression, holder.registerProblem(expression, BINARY_MORE_MEANINGFUL_MESSAGE, quickFix)
BINARY_MORE_MEANINGFUL_MESSAGE,
SplitBinaryExpressionMethodCallQuickFix(SPLIT_BINARY_EXPRESSION_DESCRIPTION, replacementMethod, pickRightOperand, noExpectedExpression)
)
} }
} }
} }

View File

@ -1,8 +1,11 @@
package de.platon42.intellij.plugins.cajon.inspections package de.platon42.intellij.plugins.cajon.inspections
import com.intellij.codeInspection.ProblemsHolder import com.intellij.codeInspection.ProblemsHolder
import com.intellij.psi.* import com.intellij.psi.JavaElementVisitor
import com.intellij.psi.PsiElementVisitor
import com.intellij.psi.PsiMethodCallExpression
import com.intellij.psi.util.TypeConversionUtil import com.intellij.psi.util.TypeConversionUtil
import de.platon42.intellij.plugins.cajon.firstArg
class AssertThatBooleanIsTrueOrFalseInspection : AbstractAssertJInspection() { class AssertThatBooleanIsTrueOrFalseInspection : AbstractAssertJInspection() {
@ -16,34 +19,23 @@ class AssertThatBooleanIsTrueOrFalseInspection : AbstractAssertJInspection() {
return object : JavaElementVisitor() { return object : JavaElementVisitor() {
override fun visitMethodCallExpression(expression: PsiMethodCallExpression) { override fun visitMethodCallExpression(expression: PsiMethodCallExpression) {
super.visitMethodCallExpression(expression) super.visitMethodCallExpression(expression)
val isEqualToObject = IS_EQUAL_TO_OBJECT.test(expression) val matchingCalls = listOf(
val isEqualToBoolean = IS_EQUAL_TO_BOOLEAN.test(expression) IS_EQUAL_TO_OBJECT, IS_EQUAL_TO_BOOLEAN,
val isNotEqualToObject = IS_NOT_EQUAL_TO_OBJECT.test(expression) IS_NOT_EQUAL_TO_OBJECT, IS_NOT_EQUAL_TO_BOOLEAN
val isNotEqualToBoolean = IS_NOT_EQUAL_TO_BOOLEAN.test(expression) ).map { it.test(expression) }
val normalBooleanTest = isEqualToObject || isEqualToBoolean if (matchingCalls.none { it }) {
val flippedBooleanTest = isNotEqualToObject || isNotEqualToBoolean
if (!(normalBooleanTest || flippedBooleanTest)) {
return return
} }
if (!checkAssertedType(expression, ABSTRACT_BOOLEAN_ASSERT_CLASSNAME)) { if (!checkAssertedType(expression, ABSTRACT_BOOLEAN_ASSERT_CLASSNAME)) {
return return
} }
val equalToExpression = expression.argumentList.expressions[0] ?: return val equalToExpression = expression.firstArg
if (!TypeConversionUtil.isBooleanType(equalToExpression.type)) { if (!TypeConversionUtil.isBooleanType(equalToExpression.type)) {
return return
} }
var value = calculateConstantParameterValue(expression, 0) val expectedResult = calculateConstantParameterValue(expression, 0)as? Boolean ?: return
if (value == null) { val flippedBooleanTest = matchingCalls.drop(2).any { it }
val field = (equalToExpression as? PsiReferenceExpression)?.resolve() as? PsiField
if (field?.containingClass?.qualifiedName == CommonClassNames.JAVA_LANG_BOOLEAN) {
when {
field.name == "TRUE" -> value = true
field.name == "FALSE" -> value = false
}
}
}
val expectedResult = value as? Boolean ?: return
val replacementMethod = if (expectedResult xor flippedBooleanTest) "isTrue()" else "isFalse()" val replacementMethod = if (expectedResult xor flippedBooleanTest) "isTrue()" else "isFalse()"
registerSimplifyMethod(holder, expression, replacementMethod) registerSimplifyMethod(holder, expression, replacementMethod)

View File

@ -17,8 +17,7 @@ class AssertThatEnumerableIsEmptyInspection : AbstractAssertJInspection() {
return object : JavaElementVisitor() { return object : JavaElementVisitor() {
override fun visitMethodCallExpression(expression: PsiMethodCallExpression) { override fun visitMethodCallExpression(expression: PsiMethodCallExpression) {
super.visitMethodCallExpression(expression) super.visitMethodCallExpression(expression)
val hasSize = HAS_SIZE.test(expression) if (!HAS_SIZE.test(expression)) {
if (!hasSize) {
return return
} }

View File

@ -5,6 +5,7 @@ import com.intellij.psi.JavaElementVisitor
import com.intellij.psi.PsiElementVisitor import com.intellij.psi.PsiElementVisitor
import com.intellij.psi.PsiMethodCallExpression import com.intellij.psi.PsiMethodCallExpression
import com.intellij.psi.PsiType import com.intellij.psi.PsiType
import de.platon42.intellij.plugins.cajon.firstArg
class AssertThatObjectIsNullOrNotNullInspection : AbstractAssertJInspection() { class AssertThatObjectIsNullOrNotNullInspection : AbstractAssertJInspection() {
@ -24,7 +25,7 @@ class AssertThatObjectIsNullOrNotNullInspection : AbstractAssertJInspection() {
return return
} }
if (expression.argumentList.expressions[0].type == PsiType.NULL) { if (expression.firstArg.type == PsiType.NULL) {
registerSimplifyMethod(holder, expression, if (isEqualTo) "isNull()" else "isNotNull()") registerSimplifyMethod(holder, expression, if (isEqualTo) "isNull()" else "isNotNull()")
} }
} }

View File

@ -3,6 +3,7 @@ package de.platon42.intellij.plugins.cajon.inspections
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 de.platon42.intellij.plugins.cajon.firstArg
import de.platon42.intellij.plugins.cajon.quickfixes.ReplaceSizeMethodCallQuickFix import de.platon42.intellij.plugins.cajon.quickfixes.ReplaceSizeMethodCallQuickFix
class AssertThatSizeInspection : AbstractAssertJInspection() { class AssertThatSizeInspection : AbstractAssertJInspection() {
@ -21,7 +22,7 @@ class AssertThatSizeInspection : AbstractAssertJInspection() {
if (!ASSERT_THAT_INT.test(expression)) { if (!ASSERT_THAT_INT.test(expression)) {
return return
} }
val actualExpression = expression.argumentList.expressions[0] ?: return val actualExpression = expression.firstArg
if (isArrayLength(actualExpression) || isCollectionSize(actualExpression)) { if (isArrayLength(actualExpression) || isCollectionSize(actualExpression)) {
val statement = PsiTreeUtil.getParentOfType(expression, PsiStatement::class.java) ?: return val statement = PsiTreeUtil.getParentOfType(expression, PsiStatement::class.java) ?: return
@ -32,7 +33,7 @@ class AssertThatSizeInspection : AbstractAssertJInspection() {
registerSizeMethod(holder, expression, expectedCallExpression, "isEmpty()", noExpectedExpression = true) registerSizeMethod(holder, expression, expectedCallExpression, "isEmpty()", noExpectedExpression = true)
return return
} }
val equalToExpression = expectedCallExpression.argumentList.expressions[0] val equalToExpression = expectedCallExpression.firstArg
if (isCollectionSize(equalToExpression) || isArrayLength(equalToExpression)) { if (isCollectionSize(equalToExpression) || isArrayLength(equalToExpression)) {
registerSizeMethod(holder, expression, expectedCallExpression, "hasSameSizeAs()", expectedIsCollection = true) registerSizeMethod(holder, expression, expectedCallExpression, "hasSameSizeAs()", expectedIsCollection = true)
return return
@ -56,10 +57,10 @@ class AssertThatSizeInspection : AbstractAssertJInspection() {
// new stuff in AssertJ 13.2.0 // new stuff in AssertJ 13.2.0
if (hasAssertJMethod(expression, "AbstractIterableAssert.hasSizeLessThan")) { if (hasAssertJMethod(expression, "AbstractIterableAssert.hasSizeLessThan")) {
val matchedMethod = listOf( val matchedMethod = listOf(
Pair(IS_GREATER_THAN_INT, "hasSizeGreaterThan()"), IS_GREATER_THAN_INT to "hasSizeGreaterThan()",
Pair(IS_GREATER_THAN_OR_EQUAL_TO_INT, "hasSizeGreaterThanOrEqualTo()"), IS_GREATER_THAN_OR_EQUAL_TO_INT to "hasSizeGreaterThanOrEqualTo()",
Pair(IS_LESS_THAN_OR_EQUAL_TO_INT, "hasSizeLessThanOrEqualTo()"), IS_LESS_THAN_OR_EQUAL_TO_INT to "hasSizeLessThanOrEqualTo()",
Pair(IS_LESS_THAN_INT, "hasSizeLessThan()") IS_LESS_THAN_INT to "hasSizeLessThan()"
).find { it.first.test(expectedCallExpression) }?.second ).find { it.first.test(expectedCallExpression) }?.second
if (matchedMethod != null) { if (matchedMethod != null) {
registerSizeMethod(holder, expression, expectedCallExpression, matchedMethod) registerSizeMethod(holder, expression, expectedCallExpression, matchedMethod)
@ -89,11 +90,8 @@ class AssertThatSizeInspection : AbstractAssertJInspection() {
val originalMethod = getOriginalMethodName(expectedCallExpression) ?: return val originalMethod = getOriginalMethodName(expectedCallExpression) ?: return
val description = REPLACE_DESCRIPTION_TEMPLATE.format(originalMethod, replacementMethod) val description = REPLACE_DESCRIPTION_TEMPLATE.format(originalMethod, replacementMethod)
val message = MORE_CONCISE_MESSAGE_TEMPLATE.format(replacementMethod, originalMethod) val message = MORE_CONCISE_MESSAGE_TEMPLATE.format(replacementMethod, originalMethod)
holder.registerProblem( val quickfix = ReplaceSizeMethodCallQuickFix(description, replacementMethod, noExpectedExpression, expectedIsCollection)
expression, holder.registerProblem(expression, message, quickfix)
message,
ReplaceSizeMethodCallQuickFix(description, replacementMethod, noExpectedExpression, expectedIsCollection)
)
} }
} }
} }

View File

@ -142,11 +142,8 @@ class JUnitAssertToAssertJInspection : AbstractJUnitAssertInspection() {
val originalMethod = getOriginalMethodName(expression) ?: return val originalMethod = getOriginalMethodName(expression) ?: return
val description = REPLACE_DESCRIPTION_TEMPLATE.format(originalMethod, replacementMethod) val description = REPLACE_DESCRIPTION_TEMPLATE.format(originalMethod, replacementMethod)
val message = CONVERT_MESSAGE_TEMPLATE.format(originalMethod) val message = CONVERT_MESSAGE_TEMPLATE.format(originalMethod)
holder.registerProblem( val quickFix = ReplaceJUnitAssertMethodCallQuickFix(description, hasExpected, replacementMethod)
expression, holder.registerProblem(expression, message, quickFix)
message,
ReplaceJUnitAssertMethodCallQuickFix(description, hasExpected, replacementMethod)
)
} }
private fun registerDeltaReplacementMethod( private fun registerDeltaReplacementMethod(
@ -157,11 +154,8 @@ class JUnitAssertToAssertJInspection : AbstractJUnitAssertInspection() {
val originalMethod = getOriginalMethodName(expression) ?: return val originalMethod = getOriginalMethodName(expression) ?: return
val description = REPLACE_DESCRIPTION_TEMPLATE.format(originalMethod, replacementMethod) val description = REPLACE_DESCRIPTION_TEMPLATE.format(originalMethod, replacementMethod)
val message = CONVERT_MESSAGE_TEMPLATE.format(originalMethod) val message = CONVERT_MESSAGE_TEMPLATE.format(originalMethod)
holder.registerProblem( val quickFix = ReplaceJUnitDeltaAssertMethodCallQuickFix(description, replacementMethod)
expression, holder.registerProblem(expression, message, quickFix)
message,
ReplaceJUnitDeltaAssertMethodCallQuickFix(description, replacementMethod)
)
} }
private class Mapping( private class Mapping(

View File

@ -6,13 +6,14 @@ import com.intellij.psi.JavaPsiFacade
import com.intellij.psi.PsiMethodCallExpression import com.intellij.psi.PsiMethodCallExpression
import com.intellij.psi.codeStyle.CodeStyleManager import com.intellij.psi.codeStyle.CodeStyleManager
import com.intellij.psi.codeStyle.JavaCodeStyleManager import com.intellij.psi.codeStyle.JavaCodeStyleManager
import de.platon42.intellij.plugins.cajon.firstArg
import de.platon42.intellij.plugins.cajon.replaceQualifier
class ReplaceJUnitAssertMethodCallQuickFix(description: String, private val hasExpected: Boolean, private val replacementMethod: String) : class ReplaceJUnitAssertMethodCallQuickFix(description: String, private val hasExpected: Boolean, private val replacementMethod: String) :
AbstractCommonQuickFix(description) { AbstractCommonQuickFix(description) {
override fun applyFix(project: Project, descriptor: ProblemDescriptor) { override fun applyFix(project: Project, descriptor: ProblemDescriptor) {
val element = descriptor.startElement val element = descriptor.startElement
val factory = JavaPsiFacade.getElementFactory(element.project)
val methodCallExpression = element as? PsiMethodCallExpression ?: return val methodCallExpression = element as? PsiMethodCallExpression ?: return
val args = methodCallExpression.argumentList val args = methodCallExpression.argumentList
val count = args.expressionCount val count = args.expressionCount
@ -20,31 +21,32 @@ class ReplaceJUnitAssertMethodCallQuickFix(description: String, private val hasE
val (expectedExpression, messageExpression) = if (hasExpected) { val (expectedExpression, messageExpression) = if (hasExpected) {
val expected = args.expressions[count - 2] ?: return val expected = args.expressions[count - 2] ?: return
val message = if (count > 2) args.expressions[0] else null val message = if (count > 2) args.expressions[0] else null
Pair(expected, message) expected to message
} else { } else {
val message = if (count > 1) args.expressions[0] else null val message = if (count > 1) args.expressions[0] else null
Pair(null, message) null to message
} }
val factory = JavaPsiFacade.getElementFactory(element.project)
val expectedMethodCall = factory.createExpressionFromText( val expectedMethodCall = factory.createExpressionFromText(
"a.${if (hasExpected) replacementMethod.replace("()", "(e)") else replacementMethod}", element "a.${if (hasExpected) replacementMethod.replace("()", "(e)") else replacementMethod}", element
) as PsiMethodCallExpression ) as PsiMethodCallExpression
if (hasExpected) { if (hasExpected) {
expectedMethodCall.argumentList.expressions[0].replace(expectedExpression!!) expectedMethodCall.firstArg.replace(expectedExpression!!)
} }
val newMethodCall = factory.createExpressionFromText( val newMethodCall = factory.createExpressionFromText(
"org.assertj.core.api.Assertions.assertThat(a)", element "org.assertj.core.api.Assertions.assertThat(a)", element
) as PsiMethodCallExpression ) as PsiMethodCallExpression
newMethodCall.argumentList.expressions[0].replace(actualExpression) newMethodCall.firstArg.replace(actualExpression)
if (messageExpression != null) { if (messageExpression != null) {
val asExpression = factory.createExpressionFromText("a.as(desc)", element) as PsiMethodCallExpression val asExpression = factory.createExpressionFromText("a.as(desc)", element) as PsiMethodCallExpression
asExpression.argumentList.expressions[0].replace(messageExpression) asExpression.firstArg.replace(messageExpression)
asExpression.methodExpression.qualifierExpression!!.replace(newMethodCall) asExpression.replaceQualifier(newMethodCall)
expectedMethodCall.methodExpression.qualifierExpression!!.replace(asExpression) expectedMethodCall.replaceQualifier(asExpression)
} else { } else {
expectedMethodCall.methodExpression.qualifierExpression!!.replace(newMethodCall) expectedMethodCall.replaceQualifier(newMethodCall)
} }
val assertThatMethod = newMethodCall.resolveMethod() ?: return val assertThatMethod = newMethodCall.resolveMethod() ?: return

View File

@ -6,12 +6,14 @@ import com.intellij.psi.JavaPsiFacade
import com.intellij.psi.PsiMethodCallExpression import com.intellij.psi.PsiMethodCallExpression
import com.intellij.psi.codeStyle.CodeStyleManager import com.intellij.psi.codeStyle.CodeStyleManager
import com.intellij.psi.codeStyle.JavaCodeStyleManager import com.intellij.psi.codeStyle.JavaCodeStyleManager
import de.platon42.intellij.plugins.cajon.firstArg
import de.platon42.intellij.plugins.cajon.getArg
import de.platon42.intellij.plugins.cajon.replaceQualifier
class ReplaceJUnitDeltaAssertMethodCallQuickFix(description: String, private val replacementMethod: String) : AbstractCommonQuickFix(description) { class ReplaceJUnitDeltaAssertMethodCallQuickFix(description: String, private val replacementMethod: String) : AbstractCommonQuickFix(description) {
override fun applyFix(project: Project, descriptor: ProblemDescriptor) { override fun applyFix(project: Project, descriptor: ProblemDescriptor) {
val element = descriptor.startElement val element = descriptor.startElement
val factory = JavaPsiFacade.getElementFactory(element.project)
val methodCallExpression = element as? PsiMethodCallExpression ?: return val methodCallExpression = element as? PsiMethodCallExpression ?: return
val args = methodCallExpression.argumentList val args = methodCallExpression.argumentList
val count = args.expressionCount val count = args.expressionCount
@ -20,31 +22,32 @@ class ReplaceJUnitDeltaAssertMethodCallQuickFix(description: String, private val
val expectedExpression = args.expressions[count - 3] ?: return val expectedExpression = args.expressions[count - 3] ?: return
val deltaExpression = args.expressions[count - 1] ?: return val deltaExpression = args.expressions[count - 1] ?: return
val factory = JavaPsiFacade.getElementFactory(element.project)
val offsetMethodCall = factory.createExpressionFromText( val offsetMethodCall = factory.createExpressionFromText(
"org.assertj.core.data.Offset.offset(c)", element "org.assertj.core.data.Offset.offset(c)", element
) as PsiMethodCallExpression ) as PsiMethodCallExpression
offsetMethodCall.argumentList.expressions[0].replace(deltaExpression) offsetMethodCall.firstArg.replace(deltaExpression)
val expectedMethodCall = factory.createExpressionFromText( val expectedMethodCall = factory.createExpressionFromText(
"a.${replacementMethod.removeSuffix("()")}(e, offs)", element "a.${replacementMethod.removeSuffix("()")}(e, offs)", element
) as PsiMethodCallExpression ) as PsiMethodCallExpression
expectedMethodCall.argumentList.expressions[0].replace(expectedExpression) expectedMethodCall.firstArg.replace(expectedExpression)
expectedMethodCall.argumentList.expressions[1].replace(offsetMethodCall) expectedMethodCall.getArg(1).replace(offsetMethodCall)
val newMethodCall = factory.createExpressionFromText( val newMethodCall = factory.createExpressionFromText(
"org.assertj.core.api.Assertions.assertThat(a)", element "org.assertj.core.api.Assertions.assertThat(a)", element
) as PsiMethodCallExpression ) as PsiMethodCallExpression
newMethodCall.argumentList.expressions[0].replace(actualExpression) newMethodCall.firstArg.replace(actualExpression)
if (messageExpression != null) { if (messageExpression != null) {
val asExpression = factory.createExpressionFromText("a.as(desc)", element) as PsiMethodCallExpression val asExpression = factory.createExpressionFromText("a.as(desc)", element) as PsiMethodCallExpression
asExpression.argumentList.expressions[0].replace(messageExpression) asExpression.firstArg.replace(messageExpression)
asExpression.methodExpression.qualifierExpression!!.replace(newMethodCall) asExpression.replaceQualifier(newMethodCall)
expectedMethodCall.methodExpression.qualifierExpression!!.replace(asExpression) expectedMethodCall.replaceQualifier(asExpression)
} else { } else {
expectedMethodCall.methodExpression.qualifierExpression!!.replace(newMethodCall) expectedMethodCall.replaceQualifier(newMethodCall)
} }
val assertThatMethod = newMethodCall.resolveMethod() ?: return val assertThatMethod = newMethodCall.resolveMethod() ?: return

View File

@ -4,17 +4,18 @@ import com.intellij.codeInspection.ProblemDescriptor
import com.intellij.openapi.project.Project import com.intellij.openapi.project.Project
import com.intellij.psi.JavaPsiFacade import com.intellij.psi.JavaPsiFacade
import com.intellij.psi.PsiMethodCallExpression import com.intellij.psi.PsiMethodCallExpression
import de.platon42.intellij.plugins.cajon.replaceQualifierFromMethodCall
class ReplaceSimpleMethodCallQuickFix(description: String, private val replacementMethod: String) : AbstractCommonQuickFix(description) { class ReplaceSimpleMethodCallQuickFix(description: String, private val replacementMethod: String) : AbstractCommonQuickFix(description) {
override fun applyFix(project: Project, descriptor: ProblemDescriptor) { override fun applyFix(project: Project, descriptor: ProblemDescriptor) {
val element = descriptor.startElement val element = descriptor.startElement
val factory = JavaPsiFacade.getElementFactory(element.project)
val methodCallExpression = element as? PsiMethodCallExpression ?: return val methodCallExpression = element as? PsiMethodCallExpression ?: return
val oldQualifier = methodCallExpression.methodExpression.qualifierExpression ?: return
val factory = JavaPsiFacade.getElementFactory(element.project)
val expectedExpression = val expectedExpression =
factory.createExpressionFromText("a.$replacementMethod", element) as PsiMethodCallExpression factory.createExpressionFromText("a.$replacementMethod", element) as PsiMethodCallExpression
expectedExpression.methodExpression.qualifierExpression!!.replace(oldQualifier) expectedExpression.replaceQualifierFromMethodCall(methodCallExpression)
element.replace(expectedExpression) element.replace(expectedExpression)
} }
} }

View File

@ -4,6 +4,9 @@ import com.intellij.codeInspection.ProblemDescriptor
import com.intellij.openapi.project.Project import com.intellij.openapi.project.Project
import com.intellij.psi.* import com.intellij.psi.*
import com.intellij.psi.util.PsiTreeUtil import com.intellij.psi.util.PsiTreeUtil
import de.platon42.intellij.plugins.cajon.firstArg
import de.platon42.intellij.plugins.cajon.qualifierExpression
import de.platon42.intellij.plugins.cajon.replaceQualifierFromMethodCall
class ReplaceSizeMethodCallQuickFix( class ReplaceSizeMethodCallQuickFix(
description: String, description: String,
@ -14,21 +17,22 @@ class ReplaceSizeMethodCallQuickFix(
override fun applyFix(project: Project, descriptor: ProblemDescriptor) { override fun applyFix(project: Project, descriptor: ProblemDescriptor) {
val element = descriptor.startElement val element = descriptor.startElement
val factory = JavaPsiFacade.getElementFactory(element.project)
val methodCallExpression = element as? PsiMethodCallExpression ?: return val methodCallExpression = element as? PsiMethodCallExpression ?: return
val assertExpression = methodCallExpression.argumentList.expressions[0] ?: return val assertExpression = methodCallExpression.firstArg
replaceCollectionSizeOrArrayLength(assertExpression) replaceCollectionSizeOrArrayLength(assertExpression)
val statement = PsiTreeUtil.getParentOfType(element, PsiStatement::class.java) ?: return val statement = PsiTreeUtil.getParentOfType(element, PsiStatement::class.java) ?: return
val oldExpectedExpression = PsiTreeUtil.findChildOfType(statement, PsiMethodCallExpression::class.java) ?: return val oldExpectedExpression = PsiTreeUtil.findChildOfType(statement, PsiMethodCallExpression::class.java) ?: return
val factory = JavaPsiFacade.getElementFactory(element.project)
val expectedExpression = val expectedExpression =
factory.createExpressionFromText("a.${if (noExpectedExpression) replacementMethod else replacementMethod.replace("()", "(e)")}", element) as PsiMethodCallExpression factory.createExpressionFromText("a.${if (noExpectedExpression) replacementMethod else replacementMethod.replace("()", "(e)")}", element) as PsiMethodCallExpression
if (!noExpectedExpression) { if (!noExpectedExpression) {
if (expectedIsCollection) { if (expectedIsCollection) {
replaceCollectionSizeOrArrayLength(oldExpectedExpression.argumentList.expressions[0]) replaceCollectionSizeOrArrayLength(oldExpectedExpression.firstArg)
} }
expectedExpression.argumentList.expressions[0].replace(oldExpectedExpression.argumentList.expressions[0]) expectedExpression.firstArg.replace(oldExpectedExpression.firstArg)
} }
expectedExpression.methodExpression.qualifierExpression!!.replace(oldExpectedExpression.methodExpression.qualifierExpression!!) expectedExpression.replaceQualifierFromMethodCall(oldExpectedExpression)
oldExpectedExpression.replace(expectedExpression) oldExpectedExpression.replace(expectedExpression)
} }
@ -36,7 +40,7 @@ class ReplaceSizeMethodCallQuickFix(
assertExpression.replace( assertExpression.replace(
when (assertExpression) { when (assertExpression) {
is PsiReferenceExpression -> assertExpression.qualifierExpression!! is PsiReferenceExpression -> assertExpression.qualifierExpression!!
is PsiMethodCallExpression -> assertExpression.methodExpression.qualifierExpression!! is PsiMethodCallExpression -> assertExpression.qualifierExpression
else -> return else -> return
} }
) )

View File

@ -7,6 +7,8 @@ import com.intellij.psi.PsiBinaryExpression
import com.intellij.psi.PsiMethodCallExpression import com.intellij.psi.PsiMethodCallExpression
import com.intellij.psi.PsiStatement import com.intellij.psi.PsiStatement
import com.intellij.psi.util.PsiTreeUtil import com.intellij.psi.util.PsiTreeUtil
import de.platon42.intellij.plugins.cajon.firstArg
import de.platon42.intellij.plugins.cajon.replaceQualifierFromMethodCall
class SplitBinaryExpressionMethodCallQuickFix( class SplitBinaryExpressionMethodCallQuickFix(
description: String, description: String,
@ -17,20 +19,21 @@ class SplitBinaryExpressionMethodCallQuickFix(
override fun applyFix(project: Project, descriptor: ProblemDescriptor) { override fun applyFix(project: Project, descriptor: ProblemDescriptor) {
val element = descriptor.startElement val element = descriptor.startElement
val factory = JavaPsiFacade.getElementFactory(element.project)
val methodCallExpression = element as? PsiMethodCallExpression ?: return val methodCallExpression = element as? PsiMethodCallExpression ?: return
val binaryExpression = methodCallExpression.argumentList.expressions[0] as? PsiBinaryExpression ?: return val binaryExpression = methodCallExpression.firstArg as? PsiBinaryExpression ?: return
val expectedArgument = (if (pickRightOperand) binaryExpression.lOperand else binaryExpression.rOperand)?.copy() ?: return val expectedArgument = (if (pickRightOperand) binaryExpression.lOperand else binaryExpression.rOperand)?.copy() ?: return
binaryExpression.replace(if (pickRightOperand) binaryExpression.rOperand!! else binaryExpression.lOperand) binaryExpression.replace(if (pickRightOperand) binaryExpression.rOperand!! else binaryExpression.lOperand)
val statement = PsiTreeUtil.getParentOfType(element, PsiStatement::class.java) ?: return val statement = PsiTreeUtil.getParentOfType(element, PsiStatement::class.java) ?: return
val oldExpectedExpression = PsiTreeUtil.findChildOfType(statement, PsiMethodCallExpression::class.java) ?: return val oldExpectedExpression = PsiTreeUtil.findChildOfType(statement, PsiMethodCallExpression::class.java) ?: return
val factory = JavaPsiFacade.getElementFactory(element.project)
val expectedExpression = val expectedExpression =
factory.createExpressionFromText("a.${if (noExpectedExpression) replacementMethod else replacementMethod.replace("()", "(e)")}", element) as PsiMethodCallExpression factory.createExpressionFromText("a.${if (noExpectedExpression) replacementMethod else replacementMethod.replace("()", "(e)")}", element) as PsiMethodCallExpression
if (!noExpectedExpression) { if (!noExpectedExpression) {
expectedExpression.argumentList.expressions[0].replace(expectedArgument) expectedExpression.firstArg.replace(expectedArgument)
} }
expectedExpression.methodExpression.qualifierExpression!!.replace(oldExpectedExpression.methodExpression.qualifierExpression!!) expectedExpression.replaceQualifierFromMethodCall(oldExpectedExpression)
oldExpectedExpression.replace(expectedExpression) oldExpectedExpression.replace(expectedExpression)
} }
} }

View File

@ -6,23 +6,27 @@ import com.intellij.psi.JavaPsiFacade
import com.intellij.psi.PsiMethodCallExpression import com.intellij.psi.PsiMethodCallExpression
import com.intellij.psi.PsiStatement import com.intellij.psi.PsiStatement
import com.intellij.psi.util.PsiTreeUtil import com.intellij.psi.util.PsiTreeUtil
import de.platon42.intellij.plugins.cajon.firstArg
import de.platon42.intellij.plugins.cajon.qualifierExpression
import de.platon42.intellij.plugins.cajon.replaceQualifierFromMethodCall
class SplitEqualsExpressionMethodCallQuickFix(description: String, private val replacementMethod: String) : AbstractCommonQuickFix(description) { class SplitEqualsExpressionMethodCallQuickFix(description: String, private val replacementMethod: String) : AbstractCommonQuickFix(description) {
override fun applyFix(project: Project, descriptor: ProblemDescriptor) { override fun applyFix(project: Project, descriptor: ProblemDescriptor) {
val element = descriptor.startElement val element = descriptor.startElement
val factory = JavaPsiFacade.getElementFactory(element.project)
val methodCallExpression = element as? PsiMethodCallExpression ?: return val methodCallExpression = element as? PsiMethodCallExpression ?: return
val equalsMethodCall = methodCallExpression.argumentList.expressions[0] as? PsiMethodCallExpression ?: return val equalsMethodCall = methodCallExpression.firstArg as? PsiMethodCallExpression ?: return
val expectedArgument = equalsMethodCall.argumentList.expressions[0].copy() val expectedArgument = equalsMethodCall.firstArg.copy()
equalsMethodCall.replace(equalsMethodCall.methodExpression.qualifierExpression!!) equalsMethodCall.replace(equalsMethodCall.qualifierExpression)
val statement = PsiTreeUtil.getParentOfType(element, PsiStatement::class.java) ?: return val statement = PsiTreeUtil.getParentOfType(element, PsiStatement::class.java) ?: return
val oldExpectedExpression = PsiTreeUtil.findChildOfType(statement, PsiMethodCallExpression::class.java) ?: return val oldExpectedExpression = PsiTreeUtil.findChildOfType(statement, PsiMethodCallExpression::class.java) ?: return
val factory = JavaPsiFacade.getElementFactory(element.project)
val expectedExpression = val expectedExpression =
factory.createExpressionFromText("a.${replacementMethod.replace("()", "(e)")}", element) as PsiMethodCallExpression factory.createExpressionFromText("a.${replacementMethod.replace("()", "(e)")}", element) as PsiMethodCallExpression
expectedExpression.argumentList.expressions[0].replace(expectedArgument) expectedExpression.firstArg.replace(expectedArgument)
expectedExpression.methodExpression.qualifierExpression!!.replace(oldExpectedExpression.methodExpression.qualifierExpression!!) expectedExpression.replaceQualifierFromMethodCall(oldExpectedExpression)
oldExpectedExpression.replace(expectedExpression) oldExpectedExpression.replace(expectedExpression)
} }
} }