Inspection to highlight usages of Collections.sort() and replace them with .sort() method from Kotlin stdlib
#KT-11023 Fixed
This commit is contained in:
committed by
Dmitry Jemerov
parent
4ac870500f
commit
2cdc246a27
@@ -0,0 +1,5 @@
|
|||||||
|
<html>
|
||||||
|
<body>
|
||||||
|
This inspection reports Java Collections static method call replaceable by Kotlin stdlib, e.g. Collections.sort(list).
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
@@ -2673,6 +2673,14 @@
|
|||||||
language="kotlin"
|
language="kotlin"
|
||||||
/>
|
/>
|
||||||
|
|
||||||
|
<localInspection implementationClass="org.jetbrains.kotlin.idea.inspections.JavaCollectionsStaticMethodInspection"
|
||||||
|
displayName="Java Collections static method call can be replaced with Kotlin stdlib"
|
||||||
|
groupPath="Kotlin"
|
||||||
|
groupName="Style issues"
|
||||||
|
enabledByDefault="true"
|
||||||
|
level="WEAK WARNING"
|
||||||
|
language="kotlin"
|
||||||
|
/>
|
||||||
|
|
||||||
<localInspection implementationClass="org.jetbrains.kotlin.idea.inspections.RecursiveEqualsCallInspection"
|
<localInspection implementationClass="org.jetbrains.kotlin.idea.inspections.RecursiveEqualsCallInspection"
|
||||||
displayName="Recursive equals call"
|
displayName="Recursive equals call"
|
||||||
|
|||||||
+109
@@ -0,0 +1,109 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2010-2017 JetBrains s.r.o.
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.jetbrains.kotlin.idea.inspections
|
||||||
|
|
||||||
|
import com.intellij.codeInspection.LocalQuickFix
|
||||||
|
import com.intellij.codeInspection.ProblemDescriptor
|
||||||
|
import com.intellij.codeInspection.ProblemHighlightType
|
||||||
|
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
|
||||||
|
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.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
|
||||||
|
|
||||||
|
class JavaCollectionsStaticMethodInspection : AbstractKotlinInspection() {
|
||||||
|
override fun buildVisitor(holder: ProblemsHolder, isOnTheFly: Boolean): PsiElementVisitor {
|
||||||
|
return object : KtVisitorVoid() {
|
||||||
|
override fun visitDotQualifiedExpression(expression: KtDotQualifiedExpression) {
|
||||||
|
super.visitDotQualifiedExpression(expression)
|
||||||
|
|
||||||
|
val callExpression = expression.callExpression ?: return
|
||||||
|
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
|
||||||
|
|
||||||
|
val resolvedCall = expression.getResolvedCall(context) ?: return
|
||||||
|
val descriptor = resolvedCall.resultingDescriptor as? JavaMethodDescriptor ?: return
|
||||||
|
val fqName = descriptor.importableFqName?.asString() ?: return
|
||||||
|
if (!canReplaceWithStdLib(expression, fqName, args)) return
|
||||||
|
|
||||||
|
val methodName = fqName.split(".").last()
|
||||||
|
holder.registerProblem(expression,
|
||||||
|
"Java Collections static method call can be replaced with Kotlin stdlib",
|
||||||
|
ProblemHighlightType.GENERIC_ERROR_OR_WARNING,
|
||||||
|
ReplaceWithStdLibFix(methodName, firstArg.text))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun canReplaceWithStdLib(expression: KtDotQualifiedExpression, fqName: String, args: List<KtValueArgument>): Boolean {
|
||||||
|
if (!fqName.startsWith("java.util.Collections.")) return false
|
||||||
|
val size = args.size
|
||||||
|
return when (fqName) {
|
||||||
|
"java.util.Collections.fill" -> checkApiVersion(ApiVersion.KOTLIN_1_2, expression) && size == 2
|
||||||
|
"java.util.Collections.reverse" -> size == 1
|
||||||
|
"java.util.Collections.shuffle" -> checkApiVersion(ApiVersion.KOTLIN_1_2, expression) && (size == 1 || size == 2)
|
||||||
|
"java.util.Collections.sort" -> {
|
||||||
|
size == 1 || (size == 2 && args.getOrNull(1)?.getArgumentExpression() is KtLambdaExpression)
|
||||||
|
}
|
||||||
|
else -> false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun checkApiVersion(requiredVersion: ApiVersion, expression: KtDotQualifiedExpression): Boolean {
|
||||||
|
val module = ModuleUtilCore.findModuleForPsiElement(expression) ?: return true
|
||||||
|
return module.languageVersionSettings.apiVersion >= requiredVersion
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private class ReplaceWithStdLibFix(private val methodName: String, private val receiver: String) : LocalQuickFix {
|
||||||
|
override fun getName() = "Replace with $receiver.$methodName"
|
||||||
|
|
||||||
|
override fun getFamilyName() = name
|
||||||
|
|
||||||
|
override fun applyFix(project: Project, descriptor: ProblemDescriptor) {
|
||||||
|
val expression = descriptor.psiElement as? KtDotQualifiedExpression ?: return
|
||||||
|
val callExpression = expression.callExpression ?: return
|
||||||
|
val valueArguments = callExpression.valueArguments
|
||||||
|
val firstArg = valueArguments.getOrNull(0)?.getArgumentExpression() ?: return
|
||||||
|
val secondArg = valueArguments.getOrNull(1)?.getArgumentExpression()
|
||||||
|
val factory = KtPsiFactory(project)
|
||||||
|
val newExpression = if (secondArg != null) {
|
||||||
|
if (methodName == "sort")
|
||||||
|
factory.createExpressionByPattern("$0.sortWith(Comparator $1)", firstArg, secondArg.text)
|
||||||
|
else
|
||||||
|
factory.createExpressionByPattern("$0.$methodName($1)", firstArg, secondArg)
|
||||||
|
}
|
||||||
|
else
|
||||||
|
factory.createExpressionByPattern("$0.$methodName()", firstArg)
|
||||||
|
expression.replace(newExpression)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
org.jetbrains.kotlin.idea.inspections.JavaCollectionsStaticMethodInspection
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
// RUNTIME_WITH_FULL_JDK
|
||||||
|
import java.util.Collections
|
||||||
|
|
||||||
|
fun test() {
|
||||||
|
val mutableList = mutableListOf(1, 2)
|
||||||
|
<caret>Collections.fill(mutableList, 3)
|
||||||
|
}
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
// RUNTIME_WITH_FULL_JDK
|
||||||
|
import java.util.Collections
|
||||||
|
|
||||||
|
fun test() {
|
||||||
|
val mutableList = mutableListOf(1, 2)
|
||||||
|
mutableList.fill(3)
|
||||||
|
}
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
// RUNTIME_WITH_FULL_JDK
|
||||||
|
import java.util.Collections
|
||||||
|
|
||||||
|
fun test() {
|
||||||
|
val mutableList = mutableListOf(1, 2)
|
||||||
|
<caret>Collections.reverse(mutableList)
|
||||||
|
}
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
// RUNTIME_WITH_FULL_JDK
|
||||||
|
import java.util.Collections
|
||||||
|
|
||||||
|
fun test() {
|
||||||
|
val mutableList = mutableListOf(1, 2)
|
||||||
|
mutableList.reverse()
|
||||||
|
}
|
||||||
+8
@@ -0,0 +1,8 @@
|
|||||||
|
// RUNTIME_WITH_FULL_JDK
|
||||||
|
// PROBLEM: none
|
||||||
|
import java.util.Collections
|
||||||
|
|
||||||
|
fun test() {
|
||||||
|
val list = listOf(1, 2)
|
||||||
|
<caret>Collections.reverse(list)
|
||||||
|
}
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
// RUNTIME_WITH_FULL_JDK
|
||||||
|
import java.util.Collections
|
||||||
|
|
||||||
|
fun test() {
|
||||||
|
val mutableList = mutableListOf(1, 2)
|
||||||
|
<caret>Collections.shuffle(mutableList)
|
||||||
|
}
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
// RUNTIME_WITH_FULL_JDK
|
||||||
|
import java.util.Collections
|
||||||
|
|
||||||
|
fun test() {
|
||||||
|
val mutableList = mutableListOf(1, 2)
|
||||||
|
mutableList.shuffle()
|
||||||
|
}
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
// RUNTIME_WITH_FULL_JDK
|
||||||
|
import java.util.*
|
||||||
|
|
||||||
|
fun test() {
|
||||||
|
val mutableList = mutableListOf(1, 2)
|
||||||
|
<caret>Collections.shuffle(mutableList, Random(1))
|
||||||
|
}
|
||||||
+7
@@ -0,0 +1,7 @@
|
|||||||
|
// RUNTIME_WITH_FULL_JDK
|
||||||
|
import java.util.*
|
||||||
|
|
||||||
|
fun test() {
|
||||||
|
val mutableList = mutableListOf(1, 2)
|
||||||
|
mutableList.shuffle(Random(1))
|
||||||
|
}
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
// RUNTIME_WITH_FULL_JDK
|
||||||
|
import java.util.Collections
|
||||||
|
|
||||||
|
fun test() {
|
||||||
|
val mutableList = mutableListOf(1, 2)
|
||||||
|
<caret>Collections.sort(mutableList)
|
||||||
|
}
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
// RUNTIME_WITH_FULL_JDK
|
||||||
|
import java.util.Collections
|
||||||
|
|
||||||
|
fun test() {
|
||||||
|
val mutableList = mutableListOf(1, 2)
|
||||||
|
mutableList.sort()
|
||||||
|
}
|
||||||
+8
@@ -0,0 +1,8 @@
|
|||||||
|
// RUNTIME_WITH_FULL_JDK
|
||||||
|
// PROBLEM: none
|
||||||
|
import java.util.Collections
|
||||||
|
|
||||||
|
fun test() {
|
||||||
|
val list = listOf(1, 2)
|
||||||
|
<caret>Collections.sort(list)
|
||||||
|
}
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
// RUNTIME_WITH_FULL_JDK
|
||||||
|
import java.util.Collections
|
||||||
|
|
||||||
|
fun test() {
|
||||||
|
val mutableList = mutableListOf(1, 2)
|
||||||
|
<caret>Collections.sort(mutableList, { a, b -> a.compareTo(b) })
|
||||||
|
}
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
// RUNTIME_WITH_FULL_JDK
|
||||||
|
import java.util.Collections
|
||||||
|
|
||||||
|
fun test() {
|
||||||
|
val mutableList = mutableListOf(1, 2)
|
||||||
|
mutableList.sortWith(Comparator { a, b -> a.compareTo(b) })
|
||||||
|
}
|
||||||
@@ -816,6 +816,63 @@ public class LocalInspectionTestGenerated extends AbstractLocalInspectionTest {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@TestMetadata("idea/testData/inspectionsLocal/javaCollectionsStaticMethod")
|
||||||
|
@TestDataPath("$PROJECT_ROOT")
|
||||||
|
@RunWith(JUnit3RunnerWithInners.class)
|
||||||
|
public static class JavaCollectionsStaticMethod extends AbstractLocalInspectionTest {
|
||||||
|
public void testAllFilesPresentInJavaCollectionsStaticMethod() throws Exception {
|
||||||
|
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/inspectionsLocal/javaCollectionsStaticMethod"), Pattern.compile("^([\\w\\-_]+)\\.kt$"), TargetBackend.ANY, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
@TestMetadata("fill.kt")
|
||||||
|
public void testFill() throws Exception {
|
||||||
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspectionsLocal/javaCollectionsStaticMethod/fill.kt");
|
||||||
|
doTest(fileName);
|
||||||
|
}
|
||||||
|
|
||||||
|
@TestMetadata("reverse.kt")
|
||||||
|
public void testReverse() throws Exception {
|
||||||
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspectionsLocal/javaCollectionsStaticMethod/reverse.kt");
|
||||||
|
doTest(fileName);
|
||||||
|
}
|
||||||
|
|
||||||
|
@TestMetadata("reverseImmutableList.kt")
|
||||||
|
public void testReverseImmutableList() throws Exception {
|
||||||
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspectionsLocal/javaCollectionsStaticMethod/reverseImmutableList.kt");
|
||||||
|
doTest(fileName);
|
||||||
|
}
|
||||||
|
|
||||||
|
@TestMetadata("shuffle.kt")
|
||||||
|
public void testShuffle() throws Exception {
|
||||||
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspectionsLocal/javaCollectionsStaticMethod/shuffle.kt");
|
||||||
|
doTest(fileName);
|
||||||
|
}
|
||||||
|
|
||||||
|
@TestMetadata("shuffleRandom.kt")
|
||||||
|
public void testShuffleRandom() throws Exception {
|
||||||
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspectionsLocal/javaCollectionsStaticMethod/shuffleRandom.kt");
|
||||||
|
doTest(fileName);
|
||||||
|
}
|
||||||
|
|
||||||
|
@TestMetadata("sort.kt")
|
||||||
|
public void testSort() throws Exception {
|
||||||
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspectionsLocal/javaCollectionsStaticMethod/sort.kt");
|
||||||
|
doTest(fileName);
|
||||||
|
}
|
||||||
|
|
||||||
|
@TestMetadata("sortImmutableList.kt")
|
||||||
|
public void testSortImmutableList() throws Exception {
|
||||||
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspectionsLocal/javaCollectionsStaticMethod/sortImmutableList.kt");
|
||||||
|
doTest(fileName);
|
||||||
|
}
|
||||||
|
|
||||||
|
@TestMetadata("sortWith.kt")
|
||||||
|
public void testSortWith() throws Exception {
|
||||||
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspectionsLocal/javaCollectionsStaticMethod/sortWith.kt");
|
||||||
|
doTest(fileName);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@TestMetadata("idea/testData/inspectionsLocal/kdocMissingDocumentation")
|
@TestMetadata("idea/testData/inspectionsLocal/kdocMissingDocumentation")
|
||||||
@TestDataPath("$PROJECT_ROOT")
|
@TestDataPath("$PROJECT_ROOT")
|
||||||
@RunWith(JUnit3RunnerWithInners.class)
|
@RunWith(JUnit3RunnerWithInners.class)
|
||||||
|
|||||||
Reference in New Issue
Block a user