From beba1d82fcf6ae4fc5712957c79faf9570d9502a Mon Sep 17 00:00:00 2001 From: Dmitry Gridin Date: Wed, 10 Jul 2019 12:11:11 +0300 Subject: [PATCH] ReplaceJavaStaticMethodWithKotlinAnalogInspection: add CommentSaver for case with transform to extension --- .../idea/inspections/jdk2k/Transformation.kt | 9 ++++++- .../collections/equalsWithBlockComment.kt | 8 ++++++ .../equalsWithBlockComment.kt.after | 8 ++++++ .../collections/equalsWithEOLComment.kt | 9 +++++++ .../collections/equalsWithEOLComment.kt.after | 9 +++++++ .../math/absWithBlockComment.kt | 6 +++++ .../math/absWithBlockComment.kt.after | 6 +++++ .../math/absWithEOLComment.kt | 7 ++++++ .../math/absWithEOLComment.kt.after | 7 ++++++ .../math/absWithEOLComment2.kt | 5 ++++ .../math/absWithEOLComment2.kt.after | 7 ++++++ .../LocalInspectionTestGenerated.java | 25 +++++++++++++++++++ 12 files changed, 105 insertions(+), 1 deletion(-) create mode 100644 idea/testData/inspectionsLocal/replaceJavaStaticMethodWithKotlinAnalog/collections/equalsWithBlockComment.kt create mode 100644 idea/testData/inspectionsLocal/replaceJavaStaticMethodWithKotlinAnalog/collections/equalsWithBlockComment.kt.after create mode 100644 idea/testData/inspectionsLocal/replaceJavaStaticMethodWithKotlinAnalog/collections/equalsWithEOLComment.kt create mode 100644 idea/testData/inspectionsLocal/replaceJavaStaticMethodWithKotlinAnalog/collections/equalsWithEOLComment.kt.after create mode 100644 idea/testData/inspectionsLocal/replaceJavaStaticMethodWithKotlinAnalog/math/absWithBlockComment.kt create mode 100644 idea/testData/inspectionsLocal/replaceJavaStaticMethodWithKotlinAnalog/math/absWithBlockComment.kt.after create mode 100644 idea/testData/inspectionsLocal/replaceJavaStaticMethodWithKotlinAnalog/math/absWithEOLComment.kt create mode 100644 idea/testData/inspectionsLocal/replaceJavaStaticMethodWithKotlinAnalog/math/absWithEOLComment.kt.after create mode 100644 idea/testData/inspectionsLocal/replaceJavaStaticMethodWithKotlinAnalog/math/absWithEOLComment2.kt create mode 100644 idea/testData/inspectionsLocal/replaceJavaStaticMethodWithKotlinAnalog/math/absWithEOLComment2.kt.after diff --git a/idea/src/org/jetbrains/kotlin/idea/inspections/jdk2k/Transformation.kt b/idea/src/org/jetbrains/kotlin/idea/inspections/jdk2k/Transformation.kt index 798ec9e1f41..45e9608cb50 100644 --- a/idea/src/org/jetbrains/kotlin/idea/inspections/jdk2k/Transformation.kt +++ b/idea/src/org/jetbrains/kotlin/idea/inspections/jdk2k/Transformation.kt @@ -8,6 +8,7 @@ package org.jetbrains.kotlin.idea.inspections.jdk2k import org.jetbrains.kotlin.idea.caches.resolve.resolveImportReference import org.jetbrains.kotlin.idea.core.ShortenReferences import org.jetbrains.kotlin.idea.core.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 @@ -45,9 +46,15 @@ object ToExtensionFunctionWithNonNullableReceiver : Transformation { ?.run { if (this is KtOperationExpression) "($text)" else text } ?: valueArguments.first().text val argumentsText = valueArguments.drop(1).joinToString(separator = ", ") { it.text } - callExpression.getQualifiedExpressionForSelectorOrThis().replaced( + + val oldExpression = callExpression.getQualifiedExpressionForSelectorOrThis() + val commentSaver = CommentSaver(oldExpression) + + val replaced = oldExpression.replaced( psiFactory.createExpression("$receiverText.${replacement.kotlinFunctionShortName}$typeArguments($argumentsText)") ) + commentSaver.restore(replaced) + file.resolveImportReference(FqName(replacement.kotlinFunctionFqName)).firstOrNull()?.let { ImportInsertHelper.getInstance(callExpression.project).importDescriptor(file, it) } diff --git a/idea/testData/inspectionsLocal/replaceJavaStaticMethodWithKotlinAnalog/collections/equalsWithBlockComment.kt b/idea/testData/inspectionsLocal/replaceJavaStaticMethodWithKotlinAnalog/collections/equalsWithBlockComment.kt new file mode 100644 index 00000000000..6618af0d503 --- /dev/null +++ b/idea/testData/inspectionsLocal/replaceJavaStaticMethodWithKotlinAnalog/collections/equalsWithBlockComment.kt @@ -0,0 +1,8 @@ +// WITH_RUNTIME +import java.util.Arrays + +fun test() { + val a = arrayOf(1, 2, 3) + val b = arrayOf(1, 2, 3) + /* comment */Arrays.equals(a, /* comment2 */b) +} \ No newline at end of file diff --git a/idea/testData/inspectionsLocal/replaceJavaStaticMethodWithKotlinAnalog/collections/equalsWithBlockComment.kt.after b/idea/testData/inspectionsLocal/replaceJavaStaticMethodWithKotlinAnalog/collections/equalsWithBlockComment.kt.after new file mode 100644 index 00000000000..fac152dad15 --- /dev/null +++ b/idea/testData/inspectionsLocal/replaceJavaStaticMethodWithKotlinAnalog/collections/equalsWithBlockComment.kt.after @@ -0,0 +1,8 @@ +// WITH_RUNTIME +import java.util.Arrays + +fun test() { + val a = arrayOf(1, 2, 3) + val b = arrayOf(1, 2, 3) + /* comment *//* comment2 */a.contentEquals(b) +} \ No newline at end of file diff --git a/idea/testData/inspectionsLocal/replaceJavaStaticMethodWithKotlinAnalog/collections/equalsWithEOLComment.kt b/idea/testData/inspectionsLocal/replaceJavaStaticMethodWithKotlinAnalog/collections/equalsWithEOLComment.kt new file mode 100644 index 00000000000..755e473013b --- /dev/null +++ b/idea/testData/inspectionsLocal/replaceJavaStaticMethodWithKotlinAnalog/collections/equalsWithEOLComment.kt @@ -0,0 +1,9 @@ +// WITH_RUNTIME +import java.util.Arrays + +fun test() { + val a = arrayOf(1, 2, 3) + val b = arrayOf(1, 2, 3) + // comment + Arrays.equals(a, b) //comment2 +} \ No newline at end of file diff --git a/idea/testData/inspectionsLocal/replaceJavaStaticMethodWithKotlinAnalog/collections/equalsWithEOLComment.kt.after b/idea/testData/inspectionsLocal/replaceJavaStaticMethodWithKotlinAnalog/collections/equalsWithEOLComment.kt.after new file mode 100644 index 00000000000..9754b7c70cd --- /dev/null +++ b/idea/testData/inspectionsLocal/replaceJavaStaticMethodWithKotlinAnalog/collections/equalsWithEOLComment.kt.after @@ -0,0 +1,9 @@ +// WITH_RUNTIME +import java.util.Arrays + +fun test() { + val a = arrayOf(1, 2, 3) + val b = arrayOf(1, 2, 3) + // comment + a.contentEquals(b) //comment2 +} \ No newline at end of file diff --git a/idea/testData/inspectionsLocal/replaceJavaStaticMethodWithKotlinAnalog/math/absWithBlockComment.kt b/idea/testData/inspectionsLocal/replaceJavaStaticMethodWithKotlinAnalog/math/absWithBlockComment.kt new file mode 100644 index 00000000000..64be611019d --- /dev/null +++ b/idea/testData/inspectionsLocal/replaceJavaStaticMethodWithKotlinAnalog/math/absWithBlockComment.kt @@ -0,0 +1,6 @@ +// WITH_RUNTIME +import java.lang.Math.abs + +fun x() { + /*dd*/abs(5/*s*/) +} \ No newline at end of file diff --git a/idea/testData/inspectionsLocal/replaceJavaStaticMethodWithKotlinAnalog/math/absWithBlockComment.kt.after b/idea/testData/inspectionsLocal/replaceJavaStaticMethodWithKotlinAnalog/math/absWithBlockComment.kt.after new file mode 100644 index 00000000000..5b899c0fc21 --- /dev/null +++ b/idea/testData/inspectionsLocal/replaceJavaStaticMethodWithKotlinAnalog/math/absWithBlockComment.kt.after @@ -0,0 +1,6 @@ +// WITH_RUNTIME +import java.lang.Math.abs + +fun x() { + /*dd*/kotlin.math.abs(5/*s*/) +} \ No newline at end of file diff --git a/idea/testData/inspectionsLocal/replaceJavaStaticMethodWithKotlinAnalog/math/absWithEOLComment.kt b/idea/testData/inspectionsLocal/replaceJavaStaticMethodWithKotlinAnalog/math/absWithEOLComment.kt new file mode 100644 index 00000000000..cc4ce5afec3 --- /dev/null +++ b/idea/testData/inspectionsLocal/replaceJavaStaticMethodWithKotlinAnalog/math/absWithEOLComment.kt @@ -0,0 +1,7 @@ +// WITH_RUNTIME +import java.lang.Math.abs + +fun x() { + // comment + abs(5) +} \ No newline at end of file diff --git a/idea/testData/inspectionsLocal/replaceJavaStaticMethodWithKotlinAnalog/math/absWithEOLComment.kt.after b/idea/testData/inspectionsLocal/replaceJavaStaticMethodWithKotlinAnalog/math/absWithEOLComment.kt.after new file mode 100644 index 00000000000..e47838dd202 --- /dev/null +++ b/idea/testData/inspectionsLocal/replaceJavaStaticMethodWithKotlinAnalog/math/absWithEOLComment.kt.after @@ -0,0 +1,7 @@ +// WITH_RUNTIME +import java.lang.Math.abs + +fun x() { + // comment + kotlin.math.abs(5) +} \ No newline at end of file diff --git a/idea/testData/inspectionsLocal/replaceJavaStaticMethodWithKotlinAnalog/math/absWithEOLComment2.kt b/idea/testData/inspectionsLocal/replaceJavaStaticMethodWithKotlinAnalog/math/absWithEOLComment2.kt new file mode 100644 index 00000000000..d5ac60202e2 --- /dev/null +++ b/idea/testData/inspectionsLocal/replaceJavaStaticMethodWithKotlinAnalog/math/absWithEOLComment2.kt @@ -0,0 +1,5 @@ +// WITH_RUNTIME +fun x() { + // comment + Math.abs(5) +} \ No newline at end of file diff --git a/idea/testData/inspectionsLocal/replaceJavaStaticMethodWithKotlinAnalog/math/absWithEOLComment2.kt.after b/idea/testData/inspectionsLocal/replaceJavaStaticMethodWithKotlinAnalog/math/absWithEOLComment2.kt.after new file mode 100644 index 00000000000..9b7300105f0 --- /dev/null +++ b/idea/testData/inspectionsLocal/replaceJavaStaticMethodWithKotlinAnalog/math/absWithEOLComment2.kt.after @@ -0,0 +1,7 @@ +import kotlin.math.abs + +// WITH_RUNTIME +fun x() { + // comment + abs(5) +} \ No newline at end of file diff --git a/idea/tests/org/jetbrains/kotlin/idea/inspections/LocalInspectionTestGenerated.java b/idea/tests/org/jetbrains/kotlin/idea/inspections/LocalInspectionTestGenerated.java index d7f75db2605..cd86db4b0e7 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/inspections/LocalInspectionTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/inspections/LocalInspectionTestGenerated.java @@ -9101,6 +9101,16 @@ public class LocalInspectionTestGenerated extends AbstractLocalInspectionTest { runTest("idea/testData/inspectionsLocal/replaceJavaStaticMethodWithKotlinAnalog/collections/equals2.kt"); } + @TestMetadata("equalsWithBlockComment.kt") + public void testEqualsWithBlockComment() throws Exception { + runTest("idea/testData/inspectionsLocal/replaceJavaStaticMethodWithKotlinAnalog/collections/equalsWithBlockComment.kt"); + } + + @TestMetadata("equalsWithEOLComment.kt") + public void testEqualsWithEOLComment() throws Exception { + runTest("idea/testData/inspectionsLocal/replaceJavaStaticMethodWithKotlinAnalog/collections/equalsWithEOLComment.kt"); + } + @TestMetadata("equalsWithNullOther.kt") public void testEqualsWithNullOther() throws Exception { runTest("idea/testData/inspectionsLocal/replaceJavaStaticMethodWithKotlinAnalog/collections/equalsWithNullOther.kt"); @@ -9291,6 +9301,21 @@ public class LocalInspectionTestGenerated extends AbstractLocalInspectionTest { runTest("idea/testData/inspectionsLocal/replaceJavaStaticMethodWithKotlinAnalog/math/abs4.kt"); } + @TestMetadata("absWithBlockComment.kt") + public void testAbsWithBlockComment() throws Exception { + runTest("idea/testData/inspectionsLocal/replaceJavaStaticMethodWithKotlinAnalog/math/absWithBlockComment.kt"); + } + + @TestMetadata("absWithEOLComment.kt") + public void testAbsWithEOLComment() throws Exception { + runTest("idea/testData/inspectionsLocal/replaceJavaStaticMethodWithKotlinAnalog/math/absWithEOLComment.kt"); + } + + @TestMetadata("absWithEOLComment2.kt") + public void testAbsWithEOLComment2() throws Exception { + runTest("idea/testData/inspectionsLocal/replaceJavaStaticMethodWithKotlinAnalog/math/absWithEOLComment2.kt"); + } + @TestMetadata("acos.kt") public void testAcos() throws Exception { runTest("idea/testData/inspectionsLocal/replaceJavaStaticMethodWithKotlinAnalog/math/acos.kt");