From 29eb594309d24013fb012174d72104a1bf23ec90 Mon Sep 17 00:00:00 2001 From: Mikhail Glukhikh Date: Mon, 29 Jan 2018 15:51:15 +0300 Subject: [PATCH] Inspection to replace Java Collections methods: simplify type check Related to KT-22038 --- ...vaCollectionsStaticMethodCallInspection.kt | 21 ++++++++----------- 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/idea/src/org/jetbrains/kotlin/idea/inspections/JavaCollectionsStaticMethodCallInspection.kt b/idea/src/org/jetbrains/kotlin/idea/inspections/JavaCollectionsStaticMethodCallInspection.kt index 97ab016831e..750121515a9 100644 --- a/idea/src/org/jetbrains/kotlin/idea/inspections/JavaCollectionsStaticMethodCallInspection.kt +++ b/idea/src/org/jetbrains/kotlin/idea/inspections/JavaCollectionsStaticMethodCallInspection.kt @@ -12,6 +12,7 @@ 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 @@ -19,12 +20,11 @@ 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 +import org.jetbrains.kotlin.types.KotlinType class JavaCollectionsStaticMethodInspection : AbstractKotlinInspection() { override fun buildVisitor(holder: ProblemsHolder, isOnTheFly: Boolean): PsiElementVisitor { @@ -33,7 +33,7 @@ class JavaCollectionsStaticMethodInspection : AbstractKotlinInspection() { val args = callExpression.valueArguments val firstArg = args.firstOrNull() ?: return 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 fqName = descriptor.importableFqName?.asString() ?: return @@ -70,14 +70,11 @@ 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 fun KotlinType.isMutableList() = + constructor.declarationDescriptor?.fqNameSafe == KotlinBuiltIns.FQ_NAMES.mutableList + +private fun KotlinType.isMutableListOrSubtype(): Boolean { + return isMutableList() || constructor.supertypes.reversed().any { it.isMutableList() } } private class ReplaceWithStdLibFix(private val methodName: String, private val receiver: String) : LocalQuickFix {