Add Java Arrays.asList, Set.of, List.of to ReplaceJavaStaticMethodWithKotlinAnalogInspection

Relates to #KT-27782 #KT-21641
This commit is contained in:
Dmitry Gridin
2019-04-12 12:05:52 +07:00
parent b5b5723ec3
commit ee304e92b5
13 changed files with 80 additions and 13 deletions
@@ -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 }
}
}
@@ -0,0 +1,6 @@
// WITH_RUNTIME
import java.util.Arrays
fun test() {
val a = Arrays.<caret>asList(1, 3, null)
}
@@ -0,0 +1,6 @@
// WITH_RUNTIME
import java.util.Arrays
fun test() {
val a = listOf(1, 3, null)
}
@@ -0,0 +1,6 @@
// WITH_RUNTIME
// MIN_JAVA_VERSION: 9
fun test() {
val a = java.util.Set.of<caret><String>()
}
@@ -0,0 +1,6 @@
// WITH_RUNTIME
// MIN_JAVA_VERSION: 9
fun test() {
val a = setOf<String>()
}
@@ -0,0 +1,6 @@
// WITH_RUNTIME
// MIN_JAVA_VERSION: 9
fun test() {
val a = java.util.Set.of<caret>("sfsf", 25)
}
@@ -0,0 +1,6 @@
// WITH_RUNTIME
// MIN_JAVA_VERSION: 9
fun test() {
val a = setOf("sfsf", 25)
}
@@ -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");
}
}