Inspection to replace Java Collections methods: simplify type check
Related to KT-22038
This commit is contained in:
+9
-12
@@ -12,6 +12,7 @@ import com.intellij.codeInspection.ProblemsHolder
|
|||||||
import com.intellij.openapi.module.ModuleUtilCore
|
import com.intellij.openapi.module.ModuleUtilCore
|
||||||
import com.intellij.openapi.project.Project
|
import com.intellij.openapi.project.Project
|
||||||
import com.intellij.psi.PsiElementVisitor
|
import com.intellij.psi.PsiElementVisitor
|
||||||
|
import org.jetbrains.kotlin.builtins.KotlinBuiltIns
|
||||||
import org.jetbrains.kotlin.config.ApiVersion
|
import org.jetbrains.kotlin.config.ApiVersion
|
||||||
import org.jetbrains.kotlin.idea.caches.resolve.analyze
|
import org.jetbrains.kotlin.idea.caches.resolve.analyze
|
||||||
import org.jetbrains.kotlin.idea.imports.importableFqName
|
import org.jetbrains.kotlin.idea.imports.importableFqName
|
||||||
@@ -19,12 +20,11 @@ import org.jetbrains.kotlin.idea.intentions.callExpression
|
|||||||
import org.jetbrains.kotlin.idea.project.languageVersionSettings
|
import org.jetbrains.kotlin.idea.project.languageVersionSettings
|
||||||
import org.jetbrains.kotlin.load.java.descriptors.JavaMethodDescriptor
|
import org.jetbrains.kotlin.load.java.descriptors.JavaMethodDescriptor
|
||||||
import org.jetbrains.kotlin.psi.*
|
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.getResolvedCall
|
||||||
import org.jetbrains.kotlin.resolve.calls.callUtil.getType
|
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.resolve.lazy.BodyResolveMode
|
||||||
import org.jetbrains.kotlin.types.typeUtil.builtIns
|
import org.jetbrains.kotlin.types.KotlinType
|
||||||
import org.jetbrains.kotlin.types.typeUtil.isSubtypeOf
|
|
||||||
|
|
||||||
class JavaCollectionsStaticMethodInspection : AbstractKotlinInspection() {
|
class JavaCollectionsStaticMethodInspection : AbstractKotlinInspection() {
|
||||||
override fun buildVisitor(holder: ProblemsHolder, isOnTheFly: Boolean): PsiElementVisitor {
|
override fun buildVisitor(holder: ProblemsHolder, isOnTheFly: Boolean): PsiElementVisitor {
|
||||||
@@ -33,7 +33,7 @@ class JavaCollectionsStaticMethodInspection : AbstractKotlinInspection() {
|
|||||||
val args = callExpression.valueArguments
|
val args = callExpression.valueArguments
|
||||||
val firstArg = args.firstOrNull() ?: return
|
val firstArg = args.firstOrNull() ?: return
|
||||||
val context = expression.analyze(BodyResolveMode.PARTIAL)
|
val context = expression.analyze(BodyResolveMode.PARTIAL)
|
||||||
if (!firstArg.isMutableList(context)) return
|
if (firstArg.getArgumentExpression()?.getType(context)?.isMutableListOrSubtype() != true) return
|
||||||
|
|
||||||
val descriptor = expression.getResolvedCall(context)?.resultingDescriptor as? JavaMethodDescriptor ?: return
|
val descriptor = expression.getResolvedCall(context)?.resultingDescriptor as? JavaMethodDescriptor ?: return
|
||||||
val fqName = descriptor.importableFqName?.asString() ?: return
|
val fqName = descriptor.importableFqName?.asString() ?: return
|
||||||
@@ -70,14 +70,11 @@ class JavaCollectionsStaticMethodInspection : AbstractKotlinInspection() {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun KtValueArgument.isMutableList(context: BindingContext): Boolean {
|
private fun KotlinType.isMutableList() =
|
||||||
val type = getArgumentExpression()?.getType(context) ?: return false
|
constructor.declarationDescriptor?.fqNameSafe == KotlinBuiltIns.FQ_NAMES.mutableList
|
||||||
val constructor = type.constructor
|
|
||||||
val mutableListType = type.builtIns.mutableList.defaultType
|
private fun KotlinType.isMutableListOrSubtype(): Boolean {
|
||||||
if (constructor.declarationDescriptor?.defaultType?.isSubtypeOf(mutableListType) == true) return true
|
return isMutableList() || constructor.supertypes.reversed().any { it.isMutableList() }
|
||||||
return constructor.supertypes.reversed().any {
|
|
||||||
it.constructor.declarationDescriptor?.defaultType?.isSubtypeOf(mutableListType) == true
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private class ReplaceWithStdLibFix(private val methodName: String, private val receiver: String) : LocalQuickFix {
|
private class ReplaceWithStdLibFix(private val methodName: String, private val receiver: String) : LocalQuickFix {
|
||||||
|
|||||||
Reference in New Issue
Block a user