diff --git a/idea/src/org/jetbrains/kotlin/idea/inspections/ReplaceArraysCopyOfWithCopyOfInspection.kt b/idea/src/org/jetbrains/kotlin/idea/inspections/ReplaceArraysCopyOfWithCopyOfInspection.kt index 8513c19b556..b5d42b738d4 100644 --- a/idea/src/org/jetbrains/kotlin/idea/inspections/ReplaceArraysCopyOfWithCopyOfInspection.kt +++ b/idea/src/org/jetbrains/kotlin/idea/inspections/ReplaceArraysCopyOfWithCopyOfInspection.kt @@ -11,27 +11,20 @@ import com.intellij.codeInspection.ProblemHighlightType import com.intellij.codeInspection.ProblemsHolder import com.intellij.openapi.project.Project import com.intellij.psi.PsiElementVisitor -import org.jetbrains.kotlin.idea.intentions.callExpression -import org.jetbrains.kotlin.idea.intentions.calleeName -import org.jetbrains.kotlin.idea.intentions.getCallableDescriptor +import org.jetbrains.kotlin.idea.inspections.collections.isCalling import org.jetbrains.kotlin.name.FqName import org.jetbrains.kotlin.psi.* -import org.jetbrains.kotlin.resolve.descriptorUtil.fqNameSafe class ReplaceArraysCopyOfWithCopyOfInspection : AbstractKotlinInspection() { override fun buildVisitor(holder: ProblemsHolder, isOnTheFly: Boolean): PsiElementVisitor { - return object : KtVisitorVoid() { - override fun visitDotQualifiedExpression(expression: KtDotQualifiedExpression) { - super.visitDotQualifiedExpression(expression) - - if (expression.isArraysCopyOf()) { - holder.registerProblem( - expression, - "Replace 'Arrays.copyOf' with 'copyOf'", - ProblemHighlightType.WEAK_WARNING, - ReplaceArraysCopyOfWithCopyOfQuickfix() - ) - } + return simpleNameExpressionVisitor { simpleNameExpression -> + if (simpleNameExpression.isArraysCopyOf()) { + holder.registerProblem( + simpleNameExpression, + "Replace 'Arrays.copyOf' with 'copyOf'", + ProblemHighlightType.GENERIC_ERROR_OR_WARNING, + ReplaceArraysCopyOfWithCopyOfQuickfix() + ) } } } @@ -43,15 +36,21 @@ class ReplaceArraysCopyOfWithCopyOfQuickfix : LocalQuickFix { override fun getFamilyName() = name override fun applyFix(project: Project, descriptor: ProblemDescriptor) { - val element = descriptor.psiElement as KtDotQualifiedExpression - val args = element.callExpression?.valueArguments?.mapNotNull { it.getArgumentExpression() }?.toTypedArray() ?: return - element.replace(KtPsiFactory(element).createExpressionByPattern("$0.copyOf($1)", *args)) + val element = descriptor.psiElement as? KtSimpleNameExpression ?: return + val callExpression = (element.parent as? KtCallExpression) ?: return + val qualifiedExpression = (callExpression.parent as? KtDotQualifiedExpression) ?: return + + val args = callExpression.valueArguments.mapNotNull { it.getArgumentExpression() }.toTypedArray() ?: return + if (args.size != 2) return + + qualifiedExpression.replace(KtPsiFactory(element).createExpressionByPattern("$0.copyOf($1)", *args)) } } -private fun KtDotQualifiedExpression.isArraysCopyOf(): Boolean { - if (callExpression?.valueArguments?.size != 2) return false - if (callExpression?.valueArguments?.mapNotNull { it.getArgumentExpression() }?.size != 2) return false - if (calleeName != "copyOf") return false - return getCallableDescriptor()?.containingDeclaration?.fqNameSafe == FqName("java.util.Arrays") +private fun KtSimpleNameExpression.isArraysCopyOf(): Boolean { + val callExpression = (parent as? KtCallExpression) ?: return false + if (callExpression.valueArguments.size != 2) return false + if (callExpression.valueArguments.mapNotNull { it.getArgumentExpression() }.size != 2) return false + + return callExpression.isCalling(FqName("java.util.Arrays.copyOf")) } \ No newline at end of file diff --git a/idea/testData/inspectionsLocal/replaceArraysCopyOfWithCopyOf/qualified.kt b/idea/testData/inspectionsLocal/replaceArraysCopyOfWithCopyOf/qualified.kt new file mode 100644 index 00000000000..cd08af285a8 --- /dev/null +++ b/idea/testData/inspectionsLocal/replaceArraysCopyOfWithCopyOf/qualified.kt @@ -0,0 +1,5 @@ +// WITH_RUNTIME +fun test() { + val array = intArrayOf(1, 2, 3) + val result = java.util.Arrays.copyOf(array, 3) +} \ No newline at end of file diff --git a/idea/testData/inspectionsLocal/replaceArraysCopyOfWithCopyOf/qualified.kt.after b/idea/testData/inspectionsLocal/replaceArraysCopyOfWithCopyOf/qualified.kt.after new file mode 100644 index 00000000000..f76028beb80 --- /dev/null +++ b/idea/testData/inspectionsLocal/replaceArraysCopyOfWithCopyOf/qualified.kt.after @@ -0,0 +1,5 @@ +// WITH_RUNTIME +fun test() { + val array = intArrayOf(1, 2, 3) + val result = array.copyOf(3) +} \ No newline at end of file diff --git a/idea/testData/inspectionsLocal/replaceArraysCopyOfWithCopyOf/simple.kt b/idea/testData/inspectionsLocal/replaceArraysCopyOfWithCopyOf/simple.kt index acbc9f7efc7..35f1d154739 100644 --- a/idea/testData/inspectionsLocal/replaceArraysCopyOfWithCopyOf/simple.kt +++ b/idea/testData/inspectionsLocal/replaceArraysCopyOfWithCopyOf/simple.kt @@ -3,5 +3,5 @@ import java.util.Arrays fun test() { val array = intArrayOf(1, 2, 3) - val result = Arrays.copyOf(array, 3) + val result = Arrays.copyOf(array, 3) } \ No newline at end of file diff --git a/idea/tests/org/jetbrains/kotlin/idea/inspections/LocalInspectionTestGenerated.java b/idea/tests/org/jetbrains/kotlin/idea/inspections/LocalInspectionTestGenerated.java index 27eee1ee784..d9e55d37034 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/inspections/LocalInspectionTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/inspections/LocalInspectionTestGenerated.java @@ -5131,6 +5131,13 @@ public class LocalInspectionTestGenerated extends AbstractLocalInspectionTest { doTest(fileName); } + + @TestMetadata("qualified.kt") + public void testQualified() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspectionsLocal/replaceArraysCopyOfWithCopyOf/qualified.kt"); + doTest(fileName); + } + @TestMetadata("simple.kt") public void testSimple() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspectionsLocal/replaceArraysCopyOfWithCopyOf/simple.kt");