ReplaceJavaStaticMethodWithKotlinAnalogInspection: introduce Transformation for extension with non-null arguments

This commit is contained in:
Dmitry Gridin
2019-07-09 17:35:04 +03:00
parent 8b8858b223
commit 8314dbffd5
5 changed files with 45 additions and 2 deletions
@@ -139,8 +139,8 @@ class ReplaceJavaStaticMethodWithKotlinAnalogInspection : AbstractKotlinInspecti
},
Replacement("java.util.Arrays.copyOfRange", "kotlin.collections.copyOfRange", ToExtensionFunctionWithNonNullableReceiver),
// Replacement("java.util.Arrays.binarySearch", "kotlin.collections.binarySearch", ToExtensionFunction),
Replacement("java.util.Arrays.equals", "kotlin.collections.contentEquals", ToExtensionFunctionWithNonNullableReceiver),
Replacement("java.util.Arrays.deepEquals", "kotlin.collections.contentDeepEquals", ToExtensionFunctionWithNonNullableReceiver),
Replacement("java.util.Arrays.equals", "kotlin.collections.contentEquals", ToExtensionFunctionWithNonNullableArguments),
Replacement("java.util.Arrays.deepEquals", "kotlin.collections.contentDeepEquals", ToExtensionFunctionWithNonNullableArguments),
// Replacement("java.util.Arrays.fill", "kotlin.collections.fill", ToExtensionFunction),
// Replacement("java.util.Arrays.sort", "kotlin.collections.sort", ToExtensionFunction) {
// it.valueArguments.size == 3
@@ -62,6 +62,21 @@ object ToExtensionFunctionWithNonNullableReceiver : Transformation {
?.isNullable() == false
}
object ToExtensionFunctionWithNonNullableArguments : Transformation {
override fun invoke(callExpression: KtCallExpression, replacement: Replacement) = ToExtensionFunctionWithNonNullableReceiver(
callExpression,
replacement
)
override fun isApplicable(callExpression: KtCallExpression): Boolean = ToExtensionFunctionWithNonNullableReceiver.isApplicable(
callExpression
)
override fun isApplicable(callExpression: KtCallExpression, context: BindingContext): Boolean = callExpression.valueArguments.all {
it.getArgumentExpression()?.getType(context)?.isNullable() == false
}
}
object ToExtensionFunctionWithNullableReceiver : Transformation {
override fun invoke(callExpression: KtCallExpression, replacement: Replacement) = ToExtensionFunctionWithNonNullableReceiver(
callExpression,
@@ -0,0 +1,9 @@
// WITH_RUNTIME
// PROBLEM: none
import java.util.Arrays
fun test() {
val a = arrayOf(1, 2, 3)
val b: Array<*>? = null
val result = Arrays.<caret>deepEquals(a, b)
}
@@ -0,0 +1,9 @@
// WITH_RUNTIME
// PROBLEM: none
import java.util.Arrays
fun test() {
val a = arrayOf(1, 2, 3)
val b: Array<*>? = null
val result = Arrays.<caret>equals(a, b)
}
@@ -9076,6 +9076,11 @@ public class LocalInspectionTestGenerated extends AbstractLocalInspectionTest {
runTest("idea/testData/inspectionsLocal/replaceJavaStaticMethodWithKotlinAnalog/collections/deepEquals.kt");
}
@TestMetadata("deepEqualsWithNullOther.kt")
public void testDeepEqualsWithNullOther() throws Exception {
runTest("idea/testData/inspectionsLocal/replaceJavaStaticMethodWithKotlinAnalog/collections/deepEqualsWithNullOther.kt");
}
@TestMetadata("deepHashCode.kt")
public void testDeepHashCode() throws Exception {
runTest("idea/testData/inspectionsLocal/replaceJavaStaticMethodWithKotlinAnalog/collections/deepHashCode.kt");
@@ -9091,6 +9096,11 @@ public class LocalInspectionTestGenerated extends AbstractLocalInspectionTest {
runTest("idea/testData/inspectionsLocal/replaceJavaStaticMethodWithKotlinAnalog/collections/equals.kt");
}
@TestMetadata("equalsWithNullOther.kt")
public void testEqualsWithNullOther() throws Exception {
runTest("idea/testData/inspectionsLocal/replaceJavaStaticMethodWithKotlinAnalog/collections/equalsWithNullOther.kt");
}
@TestMetadata("hashCode.kt")
public void testHashCode() throws Exception {
runTest("idea/testData/inspectionsLocal/replaceJavaStaticMethodWithKotlinAnalog/collections/hashCode.kt");