Merge ReplaceArraysCopyOfWithCopyOfInspection and ReplaceJavaStaticMethodWithTopLevelFunctionInspection

Relates to #KT-23023
This commit is contained in:
Dmitry Gridin
2019-04-11 11:07:53 +07:00
parent 50b89dd587
commit 6b7f4dda31
11 changed files with 30 additions and 100 deletions
@@ -3189,15 +3189,6 @@
language="kotlin"
/>
<localInspection implementationClass="org.jetbrains.kotlin.idea.inspections.ReplaceArraysCopyOfWithCopyOfInspection"
displayName="Replace 'Arrays.copyOf' with 'copyOf'"
groupPath="Kotlin"
groupName="Style issues"
enabledByDefault="true"
level="WEAK WARNING"
language="kotlin"
/>
<localInspection implementationClass="org.jetbrains.kotlin.idea.inspections.SetterBackingFieldAssignmentInspection"
displayName="Existing backing field is not assigned by the setter"
groupPath="Kotlin"
@@ -1,5 +0,0 @@
<html>
<body>
This inspection reports <code>Arrays.copyOf</code> function calls replaceable with <code>copyOf</code>.
</body>
</html>
@@ -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"))
}
@@ -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 }
}
}
@@ -1 +0,0 @@
org.jetbrains.kotlin.idea.inspections.ReplaceArraysCopyOfWithCopyOfInspection
@@ -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)