Inspection to convert Arrays.copyOf(a, size) to a.copyOf(size)

This commit is contained in:
kenji tomita
2018-03-10 05:20:25 +03:00
committed by Nikolay Krasko
parent fc4f7303d3
commit e6de8e9cd3
14 changed files with 171 additions and 0 deletions
@@ -0,0 +1,5 @@
<html>
<body>
This inspection reports <code>Arrays.copyOf</code> function calls replaceable with <code>copyOf</code>.
</body>
</html>
+9
View File
@@ -3085,6 +3085,15 @@ The Kotlin plugin provides language support in IntelliJ IDEA and Android Studio.
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"
/>
<referenceImporter implementation="org.jetbrains.kotlin.idea.quickfix.KotlinReferenceImporter"/>
<fileType.fileViewProviderFactory filetype="KJSM" implementationClass="com.intellij.psi.ClassFileViewProviderFactory"/>
+9
View File
@@ -3083,6 +3083,15 @@ The Kotlin plugin provides language support in IntelliJ IDEA and Android Studio.
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"
/>
<referenceImporter implementation="org.jetbrains.kotlin.idea.quickfix.KotlinReferenceImporter"/>
<fileType.fileViewProviderFactory filetype="KJSM" implementationClass="com.intellij.psi.ClassFileViewProviderFactory"/>
+9
View File
@@ -3084,6 +3084,15 @@ The Kotlin plugin provides language support in IntelliJ IDEA and Android Studio.
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"
/>
<referenceImporter implementation="org.jetbrains.kotlin.idea.quickfix.KotlinReferenceImporter"/>
<fileType.fileViewProviderFactory filetype="KJSM" implementationClass="com.intellij.psi.ClassFileViewProviderFactory"/>
+9
View File
@@ -3085,6 +3085,15 @@ The Kotlin plugin provides language support in IntelliJ IDEA and Android Studio.
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"
/>
<referenceImporter implementation="org.jetbrains.kotlin.idea.quickfix.KotlinReferenceImporter"/>
<fileType.fileViewProviderFactory filetype="KJSM" implementationClass="com.intellij.psi.ClassFileViewProviderFactory"/>
+9
View File
@@ -3083,6 +3083,15 @@ The Kotlin plugin provides language support in IntelliJ IDEA and Android Studio.
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"
/>
<referenceImporter implementation="org.jetbrains.kotlin.idea.quickfix.KotlinReferenceImporter"/>
<fileType.fileViewProviderFactory filetype="KJSM" implementationClass="com.intellij.psi.ClassFileViewProviderFactory"/>
+9
View File
@@ -3083,6 +3083,15 @@ The Kotlin plugin provides language support in IntelliJ IDEA and Android Studio.
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"
/>
<referenceImporter implementation="org.jetbrains.kotlin.idea.quickfix.KotlinReferenceImporter"/>
<fileType.fileViewProviderFactory filetype="KJSM" implementationClass="com.intellij.psi.ClassFileViewProviderFactory"/>
+9
View File
@@ -3085,6 +3085,15 @@ The Kotlin plugin provides language support in IntelliJ IDEA and Android Studio.
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"
/>
<referenceImporter implementation="org.jetbrains.kotlin.idea.quickfix.KotlinReferenceImporter"/>
<fileType.fileViewProviderFactory filetype="KJSM" implementationClass="com.intellij.psi.ClassFileViewProviderFactory"/>
@@ -0,0 +1,57 @@
/*
* 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.intentions.callExpression
import org.jetbrains.kotlin.idea.intentions.calleeName
import org.jetbrains.kotlin.idea.intentions.getCallableDescriptor
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()
)
}
}
}
}
}
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 KtDotQualifiedExpression
val args = element.callExpression?.valueArguments?.mapNotNull { it.getArgumentExpression() }?.toTypedArray() ?: return
element.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")
}
@@ -0,0 +1 @@
org.jetbrains.kotlin.idea.inspections.ReplaceArraysCopyOfWithCopyOfInspection
@@ -0,0 +1,10 @@
// PROBLEM: none
class A {
fun copyOf(x: Int, y: Int) {
}
}
fun test() {
A().<caret>copyOf(1, 2)
}
@@ -0,0 +1,7 @@
// WITH_RUNTIME
import java.util.Arrays
fun test() {
val array = intArrayOf(1, 2, 3)
val result = <caret>Arrays.copyOf(array, 3)
}
@@ -0,0 +1,7 @@
// WITH_RUNTIME
import java.util.Arrays
fun test() {
val array = intArrayOf(1, 2, 3)
val result = array.copyOf(3)
}
@@ -5117,6 +5117,27 @@ public class LocalInspectionTestGenerated extends AbstractLocalInspectionTest {
}
}
@TestMetadata("idea/testData/inspectionsLocal/replaceArraysCopyOfWithCopyOf")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class ReplaceArraysCopyOfWithCopyOf extends AbstractLocalInspectionTest {
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 {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspectionsLocal/replaceArraysCopyOfWithCopyOf/nonArraysCopyOf.kt");
doTest(fileName);
}
@TestMetadata("simple.kt")
public void testSimple() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspectionsLocal/replaceArraysCopyOfWithCopyOf/simple.kt");
doTest(fileName);
}
}
@TestMetadata("idea/testData/inspectionsLocal/replacePutWithAssignment")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)