From 6b7f4dda318418a7b91fc21a5b5b1829fae63f49 Mon Sep 17 00:00:00 2001 From: Dmitry Gridin Date: Thu, 11 Apr 2019 11:07:53 +0700 Subject: [PATCH] Merge ReplaceArraysCopyOfWithCopyOfInspection and ReplaceJavaStaticMethodWithTopLevelFunctionInspection Relates to #KT-23023 --- idea/resources/META-INF/plugin-common.xml | 9 --- .../ReplaceArraysCopyOfWithCopyOf.html | 5 -- ...ReplaceArraysCopyOfWithCopyOfInspection.kt | 56 ------------------- ...ticMethodWithTopLevelFunctionInspection.kt | 3 +- .../replaceArraysCopyOfWithCopyOf/.inspection | 1 - .../arrays}/nonArraysCopyOf.kt | 0 .../arrays}/qualified.kt | 0 .../arrays}/qualified.kt.after | 0 .../arrays}/simple.kt | 0 .../arrays}/simple.kt.after | 0 .../LocalInspectionTestGenerated.java | 56 +++++++++---------- 11 files changed, 30 insertions(+), 100 deletions(-) delete mode 100644 idea/resources/inspectionDescriptions/ReplaceArraysCopyOfWithCopyOf.html delete mode 100644 idea/src/org/jetbrains/kotlin/idea/inspections/ReplaceArraysCopyOfWithCopyOfInspection.kt delete mode 100644 idea/testData/inspectionsLocal/replaceArraysCopyOfWithCopyOf/.inspection rename idea/testData/inspectionsLocal/{replaceArraysCopyOfWithCopyOf => replaceJavaStaticMethodWithTopLevelFunction/arrays}/nonArraysCopyOf.kt (100%) rename idea/testData/inspectionsLocal/{replaceArraysCopyOfWithCopyOf => replaceJavaStaticMethodWithTopLevelFunction/arrays}/qualified.kt (100%) rename idea/testData/inspectionsLocal/{replaceArraysCopyOfWithCopyOf => replaceJavaStaticMethodWithTopLevelFunction/arrays}/qualified.kt.after (100%) rename idea/testData/inspectionsLocal/{replaceArraysCopyOfWithCopyOf => replaceJavaStaticMethodWithTopLevelFunction/arrays}/simple.kt (100%) rename idea/testData/inspectionsLocal/{replaceArraysCopyOfWithCopyOf => replaceJavaStaticMethodWithTopLevelFunction/arrays}/simple.kt.after (100%) diff --git a/idea/resources/META-INF/plugin-common.xml b/idea/resources/META-INF/plugin-common.xml index 9675e6bc0cf..d7acb3e904e 100644 --- a/idea/resources/META-INF/plugin-common.xml +++ b/idea/resources/META-INF/plugin-common.xml @@ -3189,15 +3189,6 @@ language="kotlin" /> - - - -This inspection reports Arrays.copyOf function calls replaceable with copyOf. - - \ No newline at end of file diff --git a/idea/src/org/jetbrains/kotlin/idea/inspections/ReplaceArraysCopyOfWithCopyOfInspection.kt b/idea/src/org/jetbrains/kotlin/idea/inspections/ReplaceArraysCopyOfWithCopyOfInspection.kt deleted file mode 100644 index b5d42b738d4..00000000000 --- a/idea/src/org/jetbrains/kotlin/idea/inspections/ReplaceArraysCopyOfWithCopyOfInspection.kt +++ /dev/null @@ -1,56 +0,0 @@ -/* - * Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license - * that can be found in the license/LICENSE.txt file. - */ - -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.project.Project -import com.intellij.psi.PsiElementVisitor -import org.jetbrains.kotlin.idea.inspections.collections.isCalling -import org.jetbrains.kotlin.name.FqName -import org.jetbrains.kotlin.psi.* - -class ReplaceArraysCopyOfWithCopyOfInspection : AbstractKotlinInspection() { - override fun buildVisitor(holder: ProblemsHolder, isOnTheFly: Boolean): PsiElementVisitor { - return simpleNameExpressionVisitor { simpleNameExpression -> - if (simpleNameExpression.isArraysCopyOf()) { - holder.registerProblem( - simpleNameExpression, - "Replace 'Arrays.copyOf' with 'copyOf'", - ProblemHighlightType.GENERIC_ERROR_OR_WARNING, - ReplaceArraysCopyOfWithCopyOfQuickfix() - ) - } - } - } -} - -class ReplaceArraysCopyOfWithCopyOfQuickfix : LocalQuickFix { - override fun getName() = "Replace 'Arrays.copyOf' with 'copyOf'" - - override fun getFamilyName() = name - - override fun applyFix(project: Project, descriptor: ProblemDescriptor) { - 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 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/src/org/jetbrains/kotlin/idea/inspections/ReplaceJavaStaticMethodWithTopLevelFunctionInspection.kt b/idea/src/org/jetbrains/kotlin/idea/inspections/ReplaceJavaStaticMethodWithTopLevelFunctionInspection.kt index 9f948922582..809e7eceb5e 100644 --- a/idea/src/org/jetbrains/kotlin/idea/inspections/ReplaceJavaStaticMethodWithTopLevelFunctionInspection.kt +++ b/idea/src/org/jetbrains/kotlin/idea/inspections/ReplaceJavaStaticMethodWithTopLevelFunctionInspection.kt @@ -152,7 +152,8 @@ class ReplaceJavaStaticMethodWithTopLevelFunctionInspection : AbstractKotlinInsp Replacement("java.lang.Math.sqrt", "kotlin.math.sqrt"), Replacement("java.lang.Math.tan", "kotlin.math.tan"), Replacement("java.lang.Math.tanh", "kotlin.math.tanh"), - Replacement("java.lang.Math.copySign", "kotlin.math.withSign", toExtensionFunction = true) + Replacement("java.lang.Math.copySign", "kotlin.math.withSign", toExtensionFunction = true), + Replacement("java.util.Arrays.copyOf", "kotlin.collections.copyOf", toExtensionFunction = true) ).groupBy { it.javaMethodShortName } } } diff --git a/idea/testData/inspectionsLocal/replaceArraysCopyOfWithCopyOf/.inspection b/idea/testData/inspectionsLocal/replaceArraysCopyOfWithCopyOf/.inspection deleted file mode 100644 index 8c5e1815c2f..00000000000 --- a/idea/testData/inspectionsLocal/replaceArraysCopyOfWithCopyOf/.inspection +++ /dev/null @@ -1 +0,0 @@ -org.jetbrains.kotlin.idea.inspections.ReplaceArraysCopyOfWithCopyOfInspection \ No newline at end of file diff --git a/idea/testData/inspectionsLocal/replaceArraysCopyOfWithCopyOf/nonArraysCopyOf.kt b/idea/testData/inspectionsLocal/replaceJavaStaticMethodWithTopLevelFunction/arrays/nonArraysCopyOf.kt similarity index 100% rename from idea/testData/inspectionsLocal/replaceArraysCopyOfWithCopyOf/nonArraysCopyOf.kt rename to idea/testData/inspectionsLocal/replaceJavaStaticMethodWithTopLevelFunction/arrays/nonArraysCopyOf.kt diff --git a/idea/testData/inspectionsLocal/replaceArraysCopyOfWithCopyOf/qualified.kt b/idea/testData/inspectionsLocal/replaceJavaStaticMethodWithTopLevelFunction/arrays/qualified.kt similarity index 100% rename from idea/testData/inspectionsLocal/replaceArraysCopyOfWithCopyOf/qualified.kt rename to idea/testData/inspectionsLocal/replaceJavaStaticMethodWithTopLevelFunction/arrays/qualified.kt diff --git a/idea/testData/inspectionsLocal/replaceArraysCopyOfWithCopyOf/qualified.kt.after b/idea/testData/inspectionsLocal/replaceJavaStaticMethodWithTopLevelFunction/arrays/qualified.kt.after similarity index 100% rename from idea/testData/inspectionsLocal/replaceArraysCopyOfWithCopyOf/qualified.kt.after rename to idea/testData/inspectionsLocal/replaceJavaStaticMethodWithTopLevelFunction/arrays/qualified.kt.after diff --git a/idea/testData/inspectionsLocal/replaceArraysCopyOfWithCopyOf/simple.kt b/idea/testData/inspectionsLocal/replaceJavaStaticMethodWithTopLevelFunction/arrays/simple.kt similarity index 100% rename from idea/testData/inspectionsLocal/replaceArraysCopyOfWithCopyOf/simple.kt rename to idea/testData/inspectionsLocal/replaceJavaStaticMethodWithTopLevelFunction/arrays/simple.kt diff --git a/idea/testData/inspectionsLocal/replaceArraysCopyOfWithCopyOf/simple.kt.after b/idea/testData/inspectionsLocal/replaceJavaStaticMethodWithTopLevelFunction/arrays/simple.kt.after similarity index 100% rename from idea/testData/inspectionsLocal/replaceArraysCopyOfWithCopyOf/simple.kt.after rename to idea/testData/inspectionsLocal/replaceJavaStaticMethodWithTopLevelFunction/arrays/simple.kt.after diff --git a/idea/tests/org/jetbrains/kotlin/idea/inspections/LocalInspectionTestGenerated.java b/idea/tests/org/jetbrains/kotlin/idea/inspections/LocalInspectionTestGenerated.java index b74f206c114..6df2e2a7a7a 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/inspections/LocalInspectionTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/inspections/LocalInspectionTestGenerated.java @@ -6766,34 +6766,6 @@ public class LocalInspectionTestGenerated extends AbstractLocalInspectionTest { } } - @TestMetadata("idea/testData/inspectionsLocal/replaceArraysCopyOfWithCopyOf") - @TestDataPath("$PROJECT_ROOT") - @RunWith(JUnit3RunnerWithInners.class) - public static class ReplaceArraysCopyOfWithCopyOf extends AbstractLocalInspectionTest { - private void runTest(String testDataFilePath) throws Exception { - KotlinTestUtils.runTest(this::doTest, TargetBackend.ANY, testDataFilePath); - } - - public void testAllFilesPresentInReplaceArraysCopyOfWithCopyOf() throws Exception { - KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/inspectionsLocal/replaceArraysCopyOfWithCopyOf"), Pattern.compile("^([\\w\\-_]+)\\.(kt|kts)$"), TargetBackend.ANY, true); - } - - @TestMetadata("nonArraysCopyOf.kt") - public void testNonArraysCopyOf() throws Exception { - runTest("idea/testData/inspectionsLocal/replaceArraysCopyOfWithCopyOf/nonArraysCopyOf.kt"); - } - - @TestMetadata("qualified.kt") - public void testQualified() throws Exception { - runTest("idea/testData/inspectionsLocal/replaceArraysCopyOfWithCopyOf/qualified.kt"); - } - - @TestMetadata("simple.kt") - public void testSimple() throws Exception { - runTest("idea/testData/inspectionsLocal/replaceArraysCopyOfWithCopyOf/simple.kt"); - } - } - @TestMetadata("idea/testData/inspectionsLocal/replaceAssertBooleanWithAssertEquality") @TestDataPath("$PROJECT_ROOT") @RunWith(JUnit3RunnerWithInners.class) @@ -7216,6 +7188,34 @@ public class LocalInspectionTestGenerated extends AbstractLocalInspectionTest { KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/inspectionsLocal/replaceJavaStaticMethodWithTopLevelFunction"), Pattern.compile("^([\\w\\-_]+)\\.(kt|kts)$"), TargetBackend.ANY, true); } + @TestMetadata("idea/testData/inspectionsLocal/replaceJavaStaticMethodWithTopLevelFunction/arrays") + @TestDataPath("$PROJECT_ROOT") + @RunWith(JUnit3RunnerWithInners.class) + public static class Arrays extends AbstractLocalInspectionTest { + private void runTest(String testDataFilePath) throws Exception { + KotlinTestUtils.runTest(this::doTest, TargetBackend.ANY, testDataFilePath); + } + + public void testAllFilesPresentInArrays() throws Exception { + KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/inspectionsLocal/replaceJavaStaticMethodWithTopLevelFunction/arrays"), Pattern.compile("^([\\w\\-_]+)\\.(kt|kts)$"), TargetBackend.ANY, true); + } + + @TestMetadata("nonArraysCopyOf.kt") + public void testNonArraysCopyOf() throws Exception { + runTest("idea/testData/inspectionsLocal/replaceJavaStaticMethodWithTopLevelFunction/arrays/nonArraysCopyOf.kt"); + } + + @TestMetadata("qualified.kt") + public void testQualified() throws Exception { + runTest("idea/testData/inspectionsLocal/replaceJavaStaticMethodWithTopLevelFunction/arrays/qualified.kt"); + } + + @TestMetadata("simple.kt") + public void testSimple() throws Exception { + runTest("idea/testData/inspectionsLocal/replaceJavaStaticMethodWithTopLevelFunction/arrays/simple.kt"); + } + } + @TestMetadata("idea/testData/inspectionsLocal/replaceJavaStaticMethodWithTopLevelFunction/math") @TestDataPath("$PROJECT_ROOT") @RunWith(JUnit3RunnerWithInners.class)