Inspection to replace Java Collections methods: extend set of types

So #KT-22038 Fixed
This commit is contained in:
Toshiaki Kameyama
2018-01-17 03:30:50 +03:00
committed by Mikhail Glukhikh
parent bd8a4d78fa
commit be4739e65e
8 changed files with 75 additions and 6 deletions
@@ -12,7 +12,6 @@ import com.intellij.codeInspection.ProblemsHolder
import com.intellij.openapi.module.ModuleUtilCore
import com.intellij.openapi.project.Project
import com.intellij.psi.PsiElementVisitor
import org.jetbrains.kotlin.builtins.KotlinBuiltIns
import org.jetbrains.kotlin.config.ApiVersion
import org.jetbrains.kotlin.idea.caches.resolve.analyze
import org.jetbrains.kotlin.idea.imports.importableFqName
@@ -20,10 +19,12 @@ import org.jetbrains.kotlin.idea.intentions.callExpression
import org.jetbrains.kotlin.idea.project.languageVersionSettings
import org.jetbrains.kotlin.load.java.descriptors.JavaMethodDescriptor
import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.resolve.BindingContext
import org.jetbrains.kotlin.resolve.calls.callUtil.getResolvedCall
import org.jetbrains.kotlin.resolve.calls.callUtil.getType
import org.jetbrains.kotlin.resolve.descriptorUtil.fqNameSafe
import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode
import org.jetbrains.kotlin.types.typeUtil.builtIns
import org.jetbrains.kotlin.types.typeUtil.isSubtypeOf
class JavaCollectionsStaticMethodInspection : AbstractKotlinInspection() {
override fun buildVisitor(holder: ProblemsHolder, isOnTheFly: Boolean): PsiElementVisitor {
@@ -32,11 +33,9 @@ class JavaCollectionsStaticMethodInspection : AbstractKotlinInspection() {
val args = callExpression.valueArguments
val firstArg = args.firstOrNull() ?: return
val context = expression.analyze(BodyResolveMode.PARTIAL)
if (KotlinBuiltIns.FQ_NAMES.mutableList !=
firstArg.getArgumentExpression()?.getType(context)?.constructor?.declarationDescriptor?.fqNameSafe) return
if (!firstArg.isMutableList(context)) return
val resolvedCall = expression.getResolvedCall(context) ?: return
val descriptor = resolvedCall.resultingDescriptor as? JavaMethodDescriptor ?: return
val descriptor = expression.getResolvedCall(context)?.resultingDescriptor as? JavaMethodDescriptor ?: return
val fqName = descriptor.importableFqName?.asString() ?: return
if (!canReplaceWithStdLib(expression, fqName, args)) return
@@ -69,6 +68,16 @@ class JavaCollectionsStaticMethodInspection : AbstractKotlinInspection() {
}
private fun KtValueArgument.isMutableList(context: BindingContext): Boolean {
val type = getArgumentExpression()?.getType(context) ?: return false
val constructor = type.constructor
val mutableListType = type.builtIns.mutableList.defaultType
if (constructor.declarationDescriptor?.defaultType?.isSubtypeOf(mutableListType) == true) return true
return constructor.supertypes.reversed().any {
it.constructor.declarationDescriptor?.defaultType?.isSubtypeOf(mutableListType) == true
}
}
private class ReplaceWithStdLibFix(private val methodName: String, private val receiver: String) : LocalQuickFix {
override fun getName() = "Replace with $receiver.$methodName"
@@ -0,0 +1,7 @@
// RUNTIME_WITH_FULL_JDK
import java.util.*
fun test () {
val list: ArrayList<Int> = ArrayList()
<caret>Collections.sort(list)
}
@@ -0,0 +1,7 @@
// RUNTIME_WITH_FULL_JDK
import java.util.*
fun test () {
val list: ArrayList<Int> = ArrayList()
list.sort()
}
@@ -0,0 +1,7 @@
// RUNTIME_WITH_FULL_JDK
import java.util.*
fun test () {
val list: LinkedList<String> = LinkedList()
<caret>Collections.sort(list)
}
@@ -0,0 +1,7 @@
// RUNTIME_WITH_FULL_JDK
import java.util.*
fun test () {
val list: LinkedList<String> = LinkedList()
list.sort()
}
@@ -0,0 +1,7 @@
// RUNTIME_WITH_FULL_JDK
import java.util.*
fun test () {
val list: Vector<String> = Vector()
<caret>Collections.sort(list)
}
@@ -0,0 +1,7 @@
// RUNTIME_WITH_FULL_JDK
import java.util.*
fun test () {
val list: Vector<String> = Vector()
list.sort()
}
@@ -1545,12 +1545,30 @@ public class LocalInspectionTestGenerated extends AbstractLocalInspectionTest {
doTest(fileName);
}
@TestMetadata("sortArrayList.kt")
public void testSortArrayList() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspectionsLocal/javaCollectionsStaticMethod/sortArrayList.kt");
doTest(fileName);
}
@TestMetadata("sortImmutableList.kt")
public void testSortImmutableList() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspectionsLocal/javaCollectionsStaticMethod/sortImmutableList.kt");
doTest(fileName);
}
@TestMetadata("sortLinkedList.kt")
public void testSortLinkedList() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspectionsLocal/javaCollectionsStaticMethod/sortLinkedList.kt");
doTest(fileName);
}
@TestMetadata("sortVector.kt")
public void testSortVector() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspectionsLocal/javaCollectionsStaticMethod/sortVector.kt");
doTest(fileName);
}
@TestMetadata("sortWith.kt")
public void testSortWith() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspectionsLocal/javaCollectionsStaticMethod/sortWith.kt");