From 2e6d1d554e9b558fa3cdc210485038529d8e8613 Mon Sep 17 00:00:00 2001 From: Toshiaki Kameyama Date: Thu, 15 Dec 2022 11:07:09 +0900 Subject: [PATCH] [kotlin] Java methods should be replaced with Kotlin analog: parenthesize the receiver as needed ^KTIJ-23953 Fixed closes https://github.com/JetBrains/intellij-community/pull/2275 GitOrigin-RevId: d6234badaeecc2cd526297231a7931fa8f1f85c8 --- .../idea/inspections/jdk2k/Transformation.kt | 18 +++++++++--------- .../LocalInspectionTestGenerated.java | 10 ++++++++++ .../compare/addParentheses.kt | 5 +++++ .../compare/addParentheses.kt.after | 5 +++++ .../toString/addParentheses.kt | 5 +++++ .../toString/addParentheses.kt.after | 5 +++++ 6 files changed, 39 insertions(+), 9 deletions(-) create mode 100644 plugins/kotlin/idea/tests/testData/inspectionsLocal/replaceJavaStaticMethodWithKotlinAnalog/compare/addParentheses.kt create mode 100644 plugins/kotlin/idea/tests/testData/inspectionsLocal/replaceJavaStaticMethodWithKotlinAnalog/compare/addParentheses.kt.after create mode 100644 plugins/kotlin/idea/tests/testData/inspectionsLocal/replaceJavaStaticMethodWithKotlinAnalog/toString/addParentheses.kt create mode 100644 plugins/kotlin/idea/tests/testData/inspectionsLocal/replaceJavaStaticMethodWithKotlinAnalog/toString/addParentheses.kt.after diff --git a/plugins/kotlin/idea/src/org/jetbrains/kotlin/idea/inspections/jdk2k/Transformation.kt b/plugins/kotlin/idea/src/org/jetbrains/kotlin/idea/inspections/jdk2k/Transformation.kt index 801b91479e52..c7903a3b8a60 100644 --- a/plugins/kotlin/idea/src/org/jetbrains/kotlin/idea/inspections/jdk2k/Transformation.kt +++ b/plugins/kotlin/idea/src/org/jetbrains/kotlin/idea/inspections/jdk2k/Transformation.kt @@ -2,15 +2,13 @@ package org.jetbrains.kotlin.idea.inspections.jdk2k +import org.jetbrains.kotlin.idea.base.psi.replaced import org.jetbrains.kotlin.idea.caches.resolve.resolveImportReference import org.jetbrains.kotlin.idea.core.ShortenReferences -import org.jetbrains.kotlin.idea.base.psi.replaced import org.jetbrains.kotlin.idea.util.CommentSaver import org.jetbrains.kotlin.idea.util.ImportInsertHelper import org.jetbrains.kotlin.name.FqName -import org.jetbrains.kotlin.psi.KtCallExpression -import org.jetbrains.kotlin.psi.KtOperationExpression -import org.jetbrains.kotlin.psi.KtPsiFactory +import org.jetbrains.kotlin.psi.* import org.jetbrains.kotlin.psi.psiUtil.getQualifiedExpressionForSelectorOrThis import org.jetbrains.kotlin.resolve.BindingContext import org.jetbrains.kotlin.resolve.calls.util.getType @@ -39,17 +37,19 @@ object ToExtensionFunctionWithNonNullableReceiver : Transformation { val psiFactory = KtPsiFactory(callExpression.project) val valueArguments = callExpression.valueArguments val typeArguments = callExpression.typeArgumentList?.text ?: "" - val receiverText = valueArguments.first().getArgumentExpression() - ?.run { if (this is KtOperationExpression) "($text)" else text } - ?: valueArguments.first().text + val receiverText = valueArguments.first().let { it.getArgumentExpression()?.text ?: it.text } val argumentsText = valueArguments.drop(1).joinToString(separator = ", ") { it.text } val oldExpression = callExpression.getQualifiedExpressionForSelectorOrThis() val commentSaver = CommentSaver(oldExpression) val replaced = oldExpression.replaced( - psiFactory.createExpression("$receiverText.${replacement.kotlinFunctionShortName}$typeArguments($argumentsText)") - ) + psiFactory.createExpression("($receiverText).${replacement.kotlinFunctionShortName}$typeArguments($argumentsText)") + ) as KtDotQualifiedExpression + val receiver = replaced.receiverExpression as KtParenthesizedExpression + if (KtPsiUtil.areParenthesesUseless(receiver)) { + receiver.expression?.let { receiver.replace(it) } + } commentSaver.restore(replaced) file.resolveImportReference(FqName(replacement.kotlinFunctionFqName)).firstOrNull()?.let { diff --git a/plugins/kotlin/idea/tests/test/org/jetbrains/kotlin/idea/inspections/LocalInspectionTestGenerated.java b/plugins/kotlin/idea/tests/test/org/jetbrains/kotlin/idea/inspections/LocalInspectionTestGenerated.java index 7982084f8c71..7ebf501e3128 100644 --- a/plugins/kotlin/idea/tests/test/org/jetbrains/kotlin/idea/inspections/LocalInspectionTestGenerated.java +++ b/plugins/kotlin/idea/tests/test/org/jetbrains/kotlin/idea/inspections/LocalInspectionTestGenerated.java @@ -12312,6 +12312,11 @@ public abstract class LocalInspectionTestGenerated extends AbstractLocalInspecti KotlinTestUtils.runTest(this::doTest, this, testDataFilePath); } + @TestMetadata("addParentheses.kt") + public void testAddParentheses() throws Exception { + runTest("testData/inspectionsLocal/replaceJavaStaticMethodWithKotlinAnalog/compare/addParentheses.kt"); + } + @TestMetadata("byte.kt") public void testByte() throws Exception { runTest("testData/inspectionsLocal/replaceJavaStaticMethodWithKotlinAnalog/compare/byte.kt"); @@ -12629,6 +12634,11 @@ public abstract class LocalInspectionTestGenerated extends AbstractLocalInspecti KotlinTestUtils.runTest(this::doTest, this, testDataFilePath); } + @TestMetadata("addParentheses.kt") + public void testAddParentheses() throws Exception { + runTest("testData/inspectionsLocal/replaceJavaStaticMethodWithKotlinAnalog/toString/addParentheses.kt"); + } + @TestMetadata("byteToString.kt") public void testByteToString() throws Exception { runTest("testData/inspectionsLocal/replaceJavaStaticMethodWithKotlinAnalog/toString/byteToString.kt"); diff --git a/plugins/kotlin/idea/tests/testData/inspectionsLocal/replaceJavaStaticMethodWithKotlinAnalog/compare/addParentheses.kt b/plugins/kotlin/idea/tests/testData/inspectionsLocal/replaceJavaStaticMethodWithKotlinAnalog/compare/addParentheses.kt new file mode 100644 index 000000000000..546c5ed650b8 --- /dev/null +++ b/plugins/kotlin/idea/tests/testData/inspectionsLocal/replaceJavaStaticMethodWithKotlinAnalog/compare/addParentheses.kt @@ -0,0 +1,5 @@ +// WITH_STDLIB + +fun foo(b: Boolean) { + val t = Integer.compare(if (b) 1 else 10, 6) +} \ No newline at end of file diff --git a/plugins/kotlin/idea/tests/testData/inspectionsLocal/replaceJavaStaticMethodWithKotlinAnalog/compare/addParentheses.kt.after b/plugins/kotlin/idea/tests/testData/inspectionsLocal/replaceJavaStaticMethodWithKotlinAnalog/compare/addParentheses.kt.after new file mode 100644 index 000000000000..08b038ee93e7 --- /dev/null +++ b/plugins/kotlin/idea/tests/testData/inspectionsLocal/replaceJavaStaticMethodWithKotlinAnalog/compare/addParentheses.kt.after @@ -0,0 +1,5 @@ +// WITH_STDLIB + +fun foo(b: Boolean) { + val t = (if (b) 1 else 10).compareTo(6) +} \ No newline at end of file diff --git a/plugins/kotlin/idea/tests/testData/inspectionsLocal/replaceJavaStaticMethodWithKotlinAnalog/toString/addParentheses.kt b/plugins/kotlin/idea/tests/testData/inspectionsLocal/replaceJavaStaticMethodWithKotlinAnalog/toString/addParentheses.kt new file mode 100644 index 000000000000..7016b7fc28a7 --- /dev/null +++ b/plugins/kotlin/idea/tests/testData/inspectionsLocal/replaceJavaStaticMethodWithKotlinAnalog/toString/addParentheses.kt @@ -0,0 +1,5 @@ +// WITH_STDLIB + +fun test(b: Boolean) { + Integer.toString(if (b) 1 else 0) +} \ No newline at end of file diff --git a/plugins/kotlin/idea/tests/testData/inspectionsLocal/replaceJavaStaticMethodWithKotlinAnalog/toString/addParentheses.kt.after b/plugins/kotlin/idea/tests/testData/inspectionsLocal/replaceJavaStaticMethodWithKotlinAnalog/toString/addParentheses.kt.after new file mode 100644 index 000000000000..7adbe92dd58b --- /dev/null +++ b/plugins/kotlin/idea/tests/testData/inspectionsLocal/replaceJavaStaticMethodWithKotlinAnalog/toString/addParentheses.kt.after @@ -0,0 +1,5 @@ +// WITH_STDLIB + +fun test(b: Boolean) { + (if (b) 1 else 0).toString() +} \ No newline at end of file