diff --git a/idea/src/org/jetbrains/kotlin/idea/inspections/ReplaceJavaStaticMethodWithKotlinAnalogInspection.kt b/idea/src/org/jetbrains/kotlin/idea/inspections/ReplaceJavaStaticMethodWithKotlinAnalogInspection.kt index aef10809026..9cf91b4d3be 100644 --- a/idea/src/org/jetbrains/kotlin/idea/inspections/ReplaceJavaStaticMethodWithKotlinAnalogInspection.kt +++ b/idea/src/org/jetbrains/kotlin/idea/inspections/ReplaceJavaStaticMethodWithKotlinAnalogInspection.kt @@ -89,16 +89,19 @@ class ReplaceJavaStaticMethodWithKotlinAnalogInspection : AbstractKotlinInspecti val file = dotQualified.containingKtFile val psiFactory = KtPsiFactory(call) val valueArguments = call.valueArguments + val typeArguments = call.typeArgumentList?.text ?: "" if (replacement.toExtensionFunction) { val receiverText = valueArguments.first().text val argumentsText = valueArguments.drop(1).joinToString(separator = ", ") { it.text } - dotQualified.replaced(psiFactory.createExpression("$receiverText.${replacement.kotlinFunctionShortName}($argumentsText)")) + dotQualified.replaced(psiFactory.createExpression("$receiverText.${replacement.kotlinFunctionShortName}$typeArguments($argumentsText)")) file.resolveImportReference(FqName(replacement.kotlinFunctionFqName)).firstOrNull()?.let { ImportInsertHelper.getInstance(project).importDescriptor(file, it) } } else { val argumentsText = valueArguments.joinToString(separator = ", ") { it.text } - val replaced = dotQualified.replaced(psiFactory.createExpression("${replacement.kotlinFunctionFqName}($argumentsText)")) + val replaced = dotQualified.replaced( + psiFactory.createExpression("${replacement.kotlinFunctionFqName}$typeArguments($argumentsText)") + ) ShortenReferences.DEFAULT.process(replaced) } } @@ -176,8 +179,21 @@ class ReplaceJavaStaticMethodWithKotlinAnalogInspection : AbstractKotlinInspecti Replacement("java.lang.Math.copySign", "kotlin.math.withSign", toExtensionFunction = true) ) - private val JAVA_ARRAYS = listOf(Replacement("java.util.Arrays.copyOf", "kotlin.collections.copyOf", toExtensionFunction = true)) + private val JAVA_COLLECTIONS = listOf( + Replacement("java.util.Arrays.copyOf", "kotlin.collections.copyOf", toExtensionFunction = true), + Replacement("java.util.Arrays.asList", "kotlin.collections.listOf"), + Replacement("java.util.Arrays.asList", "kotlin.collections.mutableListOf"), + Replacement("java.util.Set.of", "kotlin.collections.setOf"), + Replacement("java.util.Set.of", "kotlin.collections.mutableSetOf"), + Replacement("java.util.List.of", "kotlin.collections.listOf"), + Replacement("java.util.List.of", "kotlin.collections.mutableListOf") + ) - private val REPLACEMENTS = (JAVA_MATH + JAVA_SYSTEM + JAVA_IO + JAVA_PRIMITIVES + JAVA_ARRAYS).groupBy { it.javaMethodShortName } + private val REPLACEMENTS = (JAVA_MATH + + JAVA_SYSTEM + + JAVA_IO + + JAVA_PRIMITIVES + + JAVA_COLLECTIONS + ).groupBy { it.javaMethodShortName } } } diff --git a/idea/testData/inspectionsLocal/replaceJavaStaticMethodWithKotlinAnalog/collections/asList.kt b/idea/testData/inspectionsLocal/replaceJavaStaticMethodWithKotlinAnalog/collections/asList.kt new file mode 100644 index 00000000000..604eb001e37 --- /dev/null +++ b/idea/testData/inspectionsLocal/replaceJavaStaticMethodWithKotlinAnalog/collections/asList.kt @@ -0,0 +1,6 @@ +// WITH_RUNTIME +import java.util.Arrays + +fun test() { + val a = Arrays.asList(1, 3, null) +} \ No newline at end of file diff --git a/idea/testData/inspectionsLocal/replaceJavaStaticMethodWithKotlinAnalog/collections/asList.kt.after b/idea/testData/inspectionsLocal/replaceJavaStaticMethodWithKotlinAnalog/collections/asList.kt.after new file mode 100644 index 00000000000..63a52096845 --- /dev/null +++ b/idea/testData/inspectionsLocal/replaceJavaStaticMethodWithKotlinAnalog/collections/asList.kt.after @@ -0,0 +1,6 @@ +// WITH_RUNTIME +import java.util.Arrays + +fun test() { + val a = listOf(1, 3, null) +} \ No newline at end of file diff --git a/idea/testData/inspectionsLocal/replaceJavaStaticMethodWithKotlinAnalog/arrays/simple.kt b/idea/testData/inspectionsLocal/replaceJavaStaticMethodWithKotlinAnalog/collections/copyOf.kt similarity index 100% rename from idea/testData/inspectionsLocal/replaceJavaStaticMethodWithKotlinAnalog/arrays/simple.kt rename to idea/testData/inspectionsLocal/replaceJavaStaticMethodWithKotlinAnalog/collections/copyOf.kt diff --git a/idea/testData/inspectionsLocal/replaceJavaStaticMethodWithKotlinAnalog/arrays/simple.kt.after b/idea/testData/inspectionsLocal/replaceJavaStaticMethodWithKotlinAnalog/collections/copyOf.kt.after similarity index 100% rename from idea/testData/inspectionsLocal/replaceJavaStaticMethodWithKotlinAnalog/arrays/simple.kt.after rename to idea/testData/inspectionsLocal/replaceJavaStaticMethodWithKotlinAnalog/collections/copyOf.kt.after diff --git a/idea/testData/inspectionsLocal/replaceJavaStaticMethodWithKotlinAnalog/arrays/nonArraysCopyOf.kt b/idea/testData/inspectionsLocal/replaceJavaStaticMethodWithKotlinAnalog/collections/nonArraysCopyOf.kt similarity index 100% rename from idea/testData/inspectionsLocal/replaceJavaStaticMethodWithKotlinAnalog/arrays/nonArraysCopyOf.kt rename to idea/testData/inspectionsLocal/replaceJavaStaticMethodWithKotlinAnalog/collections/nonArraysCopyOf.kt diff --git a/idea/testData/inspectionsLocal/replaceJavaStaticMethodWithKotlinAnalog/arrays/qualified.kt b/idea/testData/inspectionsLocal/replaceJavaStaticMethodWithKotlinAnalog/collections/qualified.kt similarity index 100% rename from idea/testData/inspectionsLocal/replaceJavaStaticMethodWithKotlinAnalog/arrays/qualified.kt rename to idea/testData/inspectionsLocal/replaceJavaStaticMethodWithKotlinAnalog/collections/qualified.kt diff --git a/idea/testData/inspectionsLocal/replaceJavaStaticMethodWithKotlinAnalog/arrays/qualified.kt.after b/idea/testData/inspectionsLocal/replaceJavaStaticMethodWithKotlinAnalog/collections/qualified.kt.after similarity index 100% rename from idea/testData/inspectionsLocal/replaceJavaStaticMethodWithKotlinAnalog/arrays/qualified.kt.after rename to idea/testData/inspectionsLocal/replaceJavaStaticMethodWithKotlinAnalog/collections/qualified.kt.after diff --git a/idea/testData/inspectionsLocal/replaceJavaStaticMethodWithKotlinAnalog/collections/setOf.kt b/idea/testData/inspectionsLocal/replaceJavaStaticMethodWithKotlinAnalog/collections/setOf.kt new file mode 100644 index 00000000000..539b7280f25 --- /dev/null +++ b/idea/testData/inspectionsLocal/replaceJavaStaticMethodWithKotlinAnalog/collections/setOf.kt @@ -0,0 +1,6 @@ +// WITH_RUNTIME +// MIN_JAVA_VERSION: 9 + +fun test() { + val a = java.util.Set.of() +} \ No newline at end of file diff --git a/idea/testData/inspectionsLocal/replaceJavaStaticMethodWithKotlinAnalog/collections/setOf.kt.after b/idea/testData/inspectionsLocal/replaceJavaStaticMethodWithKotlinAnalog/collections/setOf.kt.after new file mode 100644 index 00000000000..57a9be11bbe --- /dev/null +++ b/idea/testData/inspectionsLocal/replaceJavaStaticMethodWithKotlinAnalog/collections/setOf.kt.after @@ -0,0 +1,6 @@ +// WITH_RUNTIME +// MIN_JAVA_VERSION: 9 + +fun test() { + val a = setOf() +} \ No newline at end of file diff --git a/idea/testData/inspectionsLocal/replaceJavaStaticMethodWithKotlinAnalog/collections/setOf2.kt b/idea/testData/inspectionsLocal/replaceJavaStaticMethodWithKotlinAnalog/collections/setOf2.kt new file mode 100644 index 00000000000..236d038205e --- /dev/null +++ b/idea/testData/inspectionsLocal/replaceJavaStaticMethodWithKotlinAnalog/collections/setOf2.kt @@ -0,0 +1,6 @@ +// WITH_RUNTIME +// MIN_JAVA_VERSION: 9 + +fun test() { + val a = java.util.Set.of("sfsf", 25) +} \ No newline at end of file diff --git a/idea/testData/inspectionsLocal/replaceJavaStaticMethodWithKotlinAnalog/collections/setOf2.kt.after b/idea/testData/inspectionsLocal/replaceJavaStaticMethodWithKotlinAnalog/collections/setOf2.kt.after new file mode 100644 index 00000000000..2c6ea878621 --- /dev/null +++ b/idea/testData/inspectionsLocal/replaceJavaStaticMethodWithKotlinAnalog/collections/setOf2.kt.after @@ -0,0 +1,6 @@ +// WITH_RUNTIME +// MIN_JAVA_VERSION: 9 + +fun test() { + val a = setOf("sfsf", 25) +} \ 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 b507fbd4124..57b2d48a1e2 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/inspections/LocalInspectionTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/inspections/LocalInspectionTestGenerated.java @@ -7150,31 +7150,46 @@ public class LocalInspectionTestGenerated extends AbstractLocalInspectionTest { KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/inspectionsLocal/replaceJavaStaticMethodWithKotlinAnalog"), Pattern.compile("^([\\w\\-_]+)\\.(kt|kts)$"), TargetBackend.ANY, true); } - @TestMetadata("idea/testData/inspectionsLocal/replaceJavaStaticMethodWithKotlinAnalog/arrays") + @TestMetadata("idea/testData/inspectionsLocal/replaceJavaStaticMethodWithKotlinAnalog/collections") @TestDataPath("$PROJECT_ROOT") @RunWith(JUnit3RunnerWithInners.class) - public static class Arrays extends AbstractLocalInspectionTest { + public static class Collections extends AbstractLocalInspectionTest { private void runTest(String testDataFilePath) throws Exception { KotlinTestUtils.runTest(this::doTest, TargetBackend.ANY, testDataFilePath); } - public void testAllFilesPresentInArrays() throws Exception { - KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/inspectionsLocal/replaceJavaStaticMethodWithKotlinAnalog/arrays"), Pattern.compile("^([\\w\\-_]+)\\.(kt|kts)$"), TargetBackend.ANY, true); + public void testAllFilesPresentInCollections() throws Exception { + KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/inspectionsLocal/replaceJavaStaticMethodWithKotlinAnalog/collections"), Pattern.compile("^([\\w\\-_]+)\\.(kt|kts)$"), TargetBackend.ANY, true); + } + + @TestMetadata("asList.kt") + public void testAsList() throws Exception { + runTest("idea/testData/inspectionsLocal/replaceJavaStaticMethodWithKotlinAnalog/collections/asList.kt"); + } + + @TestMetadata("copyOf.kt") + public void testCopyOf() throws Exception { + runTest("idea/testData/inspectionsLocal/replaceJavaStaticMethodWithKotlinAnalog/collections/copyOf.kt"); } @TestMetadata("nonArraysCopyOf.kt") public void testNonArraysCopyOf() throws Exception { - runTest("idea/testData/inspectionsLocal/replaceJavaStaticMethodWithKotlinAnalog/arrays/nonArraysCopyOf.kt"); + runTest("idea/testData/inspectionsLocal/replaceJavaStaticMethodWithKotlinAnalog/collections/nonArraysCopyOf.kt"); } @TestMetadata("qualified.kt") public void testQualified() throws Exception { - runTest("idea/testData/inspectionsLocal/replaceJavaStaticMethodWithKotlinAnalog/arrays/qualified.kt"); + runTest("idea/testData/inspectionsLocal/replaceJavaStaticMethodWithKotlinAnalog/collections/qualified.kt"); } - @TestMetadata("simple.kt") - public void testSimple() throws Exception { - runTest("idea/testData/inspectionsLocal/replaceJavaStaticMethodWithKotlinAnalog/arrays/simple.kt"); + @TestMetadata("setOf.kt") + public void testSetOf() throws Exception { + runTest("idea/testData/inspectionsLocal/replaceJavaStaticMethodWithKotlinAnalog/collections/setOf.kt"); + } + + @TestMetadata("setOf2.kt") + public void testSetOf2() throws Exception { + runTest("idea/testData/inspectionsLocal/replaceJavaStaticMethodWithKotlinAnalog/collections/setOf2.kt"); } }