KT 13962: Intention to replace Java collection constructor calls with function calls from stdlib (ArrayList() → arrayListOf())

This commit is contained in:
Austaon
2019-03-15 14:05:47 +01:00
committed by Mikhail Glukhikh
parent bbeb65905e
commit 0e716fd528
18 changed files with 161 additions and 0 deletions
@@ -1747,6 +1747,11 @@
<category>Kotlin</category>
</intentionAction>
<intentionAction>
<className>org.jetbrains.kotlin.idea.intentions.ConvertCollectionConstructorToFunction</className>
<category>Kotlin</category>
</intentionAction>
<lang.inspectionSuppressor language="kotlin" implementationClass="org.jetbrains.kotlin.idea.inspections.KotlinInspectionSuppressor"/>
<localInspection implementationClass="org.jetbrains.kotlin.idea.intentions.ObjectLiteralToLambdaInspection"
@@ -0,0 +1,3 @@
class Person(val name: String)
val persons = <spot>arrayListOf()</spot>
@@ -0,0 +1,3 @@
class Person(val name: String)
val persons = <spot>ArrayList()</spot>
@@ -0,0 +1,5 @@
<html>
<body>
This intention reports explicit calls of <b>Collection constructor</b> calls which can be replaced by function calls from the stdlib.
</body>
</html>
@@ -0,0 +1,42 @@
/*
* Copyright 2010-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
* that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.idea.intentions
import com.intellij.openapi.editor.Editor
import org.jetbrains.kotlin.idea.caches.resolve.resolveToCall
import org.jetbrains.kotlin.psi.KtCallExpression
import org.jetbrains.kotlin.psi.KtPsiFactory
import org.jetbrains.kotlin.resolve.descriptorUtil.fqNameSafe
class ConvertCollectionConstructorToFunction : SelfTargetingIntention<KtCallExpression>(
KtCallExpression::class.java, "Convert Collection constructor to function"
) {
private val functionMap = hashMapOf(
"java.util.ArrayList.<init>" to "arrayListOf",
"kotlin.collections.ArrayList.<init>" to "arrayListOf",
"java.util.HashMap.<init>" to "hashMapOf",
"kotlin.collections.HashMap.<init>" to "arrayListOf",
"java.util.HashSet.<init>" to "hashSetOf",
"kotlin.collections.HashSet.<init>" to "arrayListOf",
"java.util.LinkedHashMap.<init>" to "linkedMapOf",
"kotlin.collections.LinkedHashMap.<init>" to "arrayListOf",
"java.util.LinkedHashSet.<init>" to "linkedSetOf",
"kotlin.collections.LinkedHashSet.<init>" to "arrayListOf"
)
override fun isApplicableTo(element: KtCallExpression, caretOffset: Int): Boolean {
val fq = element.resolveToCall()?.resultingDescriptor?.fqNameSafe?.asString() ?: return false
return functionMap.containsKey(fq) && element.valueArguments.size == 0
}
override fun applyTo(element: KtCallExpression, editor: Editor?) {
val fq = element.resolveToCall()?.resultingDescriptor?.fqNameSafe?.asString() ?: return
val toCall = functionMap[fq] ?: return
val convertedCall = KtPsiFactory(element).createIdentifier(toCall)
element.calleeExpression?.replace(convertedCall)
}
}
@@ -0,0 +1 @@
org.jetbrains.kotlin.idea.intentions.ConvertCollectionConstructorToFunction
@@ -0,0 +1,5 @@
// WITH_RUNTIME
// IS_APPLICABLE: false
fun foo() {
var list: ArrayList<Int> = <caret>ArrayList(3)
}
@@ -0,0 +1,4 @@
// WITH_RUNTIME
fun foo() {
var list: ArrayList<Int> = <caret>ArrayList()
}
@@ -0,0 +1,4 @@
// WITH_RUNTIME
fun foo() {
var list: ArrayList<Int> = arrayListOf()
}
@@ -0,0 +1,4 @@
// WITH_RUNTIME
fun foo() {
var list = <caret>ArrayList<Int>()
}
@@ -0,0 +1,4 @@
// WITH_RUNTIME
fun foo() {
var list = arrayListOf<Int>()
}
@@ -0,0 +1,6 @@
// WITH_RUNTIME
import java.util.HashMap
fun foo() {
var list: HashMap<Int, Int> = <caret>HashMap()
}
@@ -0,0 +1,6 @@
// WITH_RUNTIME
import java.util.HashMap
fun foo() {
var list: HashMap<Int, Int> = hashMapOf()
}
@@ -0,0 +1,6 @@
// WITH_RUNTIME
import java.util.HashSet
fun foo() {
var list: HashSet<Int> = <caret>HashSet()
}
@@ -0,0 +1,6 @@
// WITH_RUNTIME
import java.util.HashSet
fun foo() {
var list: HashSet<Int> = hashSetOf()
}
@@ -0,0 +1,7 @@
// WITH_RUNTIME
import java.util.LinkedHashSet
fun foo() {
var list: LinkedHashSet<Int> = <caret>LinkedHashSet()
}
@@ -0,0 +1,7 @@
// WITH_RUNTIME
import java.util.LinkedHashSet
fun foo() {
var list: LinkedHashSet<Int> = linkedSetOf()
}
@@ -4913,6 +4913,49 @@ public class IntentionTestGenerated extends AbstractIntentionTest {
}
}
@TestMetadata("idea/testData/intentions/convertCollectionConstructorToFunction")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class ConvertCollectionConstructorToFunction extends AbstractIntentionTest {
private void runTest(String testDataFilePath) throws Exception {
KotlinTestUtils.runTest(this::doTest, TargetBackend.ANY, testDataFilePath);
}
public void testAllFilesPresentInConvertCollectionConstructorToFunction() throws Exception {
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/intentions/convertCollectionConstructorToFunction"), Pattern.compile("^([\\w\\-_]+)\\.(kt|kts)$"), TargetBackend.ANY, true);
}
@TestMetadata("keepArrayListCallWithArgument.kt")
public void testKeepArrayListCallWithArgument() throws Exception {
runTest("idea/testData/intentions/convertCollectionConstructorToFunction/keepArrayListCallWithArgument.kt");
}
@TestMetadata("replaceArrayListCall.kt")
public void testReplaceArrayListCall() throws Exception {
runTest("idea/testData/intentions/convertCollectionConstructorToFunction/replaceArrayListCall.kt");
}
@TestMetadata("replaceArrayListCallWithType.kt")
public void testReplaceArrayListCallWithType() throws Exception {
runTest("idea/testData/intentions/convertCollectionConstructorToFunction/replaceArrayListCallWithType.kt");
}
@TestMetadata("replaceHashMapCall.kt")
public void testReplaceHashMapCall() throws Exception {
runTest("idea/testData/intentions/convertCollectionConstructorToFunction/replaceHashMapCall.kt");
}
@TestMetadata("replaceHashSetCall.kt")
public void testReplaceHashSetCall() throws Exception {
runTest("idea/testData/intentions/convertCollectionConstructorToFunction/replaceHashSetCall.kt");
}
@TestMetadata("replaceLinkedHashSetCall.kt")
public void testReplaceLinkedHashSetCall() throws Exception {
runTest("idea/testData/intentions/convertCollectionConstructorToFunction/replaceLinkedHashSetCall.kt");
}
}
@TestMetadata("idea/testData/intentions/convertEnumToSealedClass")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)