[kotlin] J2K: don't convert some Java methods to Kotlin equivalents

Avoid conversions that can change program semantics
(for example, by throwing exceptions for some kinds of arguments).

^KTIJ-6060
^KTIJ-25309

GitOrigin-RevId: 6a6f881b5bc590fac84a4decac2a42b80df7617b
This commit is contained in:
Alexey Belkov
2023-05-25 15:58:57 +00:00
committed by intellij-monorepo-bot
parent 56b512ff70
commit b67f1571a5
5 changed files with 9 additions and 20 deletions
@@ -611,8 +611,6 @@ private class ConversionsHolder(private val symbolProvider: JKSymbolProvider, pr
private val arrayConversions: List<Conversion> = listOf(
Method("java.util.Arrays.copyOf") convertTo ExtensionMethod("kotlin.collections.copyOf") withReplaceType REPLACE_WITH_QUALIFIER
withByArgumentsFilter { it.size == 2 },
Method("java.util.Arrays.copyOfRange") convertTo ExtensionMethod("kotlin.collections.copyOfRange") withReplaceType REPLACE_WITH_QUALIFIER
withByArgumentsFilter { it.size == 3 },
Method("java.util.Arrays.equals") convertTo ExtensionMethod("kotlin.collections.contentEquals") withReplaceType REPLACE_WITH_QUALIFIER
withByArgumentsFilter { it.size == 2 },
Method("java.util.Arrays.deepEquals") convertTo ExtensionMethod("kotlin.collections.contentDeepEquals") withReplaceType REPLACE_WITH_QUALIFIER
@@ -659,15 +657,6 @@ private class ConversionsHolder(private val symbolProvider: JKSymbolProvider, pr
Method("java.lang.Math.nextUp") convertTo ExtensionMethod("kotlin.math.nextUp") withReplaceType REPLACE_WITH_QUALIFIER,
Method("java.lang.Math.pow") convertTo ExtensionMethod("kotlin.math.pow") withReplaceType REPLACE_WITH_QUALIFIER,
Method("java.lang.Math.rint") convertTo Method("kotlin.math.round") withReplaceType REPLACE_WITH_QUALIFIER,
Method("java.lang.Math.round") convertTo CustomExpression { expression: JKExpression ->
val arguments = (expression as JKCallExpression).arguments
if (arguments.arguments.isEmpty()) return@CustomExpression expression
val firstArgument = arguments.arguments[0]::value.detached()
val method = if (firstArgument.isDoubleType()) "roundToLong" else "roundToInt"
firstArgument.callOn(symbolProvider.provideMethodSymbol("kotlin.math.$method")).withFormattingFrom(expression)
} withReplaceType REPLACE_WITH_QUALIFIER,
Method("java.lang.Math.signum") convertTo Method("kotlin.math.sign") withReplaceType REPLACE_WITH_QUALIFIER,
Method("java.lang.Math.sin") convertTo Method("kotlin.math.sin") withReplaceType REPLACE_WITH_QUALIFIER,
Method("java.lang.Math.sinh") convertTo Method("kotlin.math.sinh") withReplaceType REPLACE_WITH_QUALIFIER,
@@ -4,9 +4,9 @@ public class J {
void foo(Object[] a) {
Arrays.copyOf(a, 5);
Arrays.copyOf(a, 5, Object[].class); // not applicable
//
// Don't convert copyOfRange, because Kotlin's copyOfRange can throw IndexOutOfBoundsException
Arrays.copyOfRange(a, 5, 6);
Arrays.copyOfRange(a, 5, 6, Object[].class); // not applicable
Arrays.copyOfRange(a, 5, 6, Object[].class);
//
Arrays.equals(a, a);
Arrays.deepEquals(a, a);
@@ -4,9 +4,9 @@ class J {
fun foo(a: Array<Any>) {
a.copyOf(5)
Arrays.copyOf(a, 5, Array<Any>::class.java) // not applicable
//
a.copyOfRange(5, 6)
Arrays.copyOfRange(a, 5, 6, Array<Any>::class.java) // not applicable
// Don't convert copyOfRange, because Kotlin's copyOfRange can throw IndexOutOfBoundsException
Arrays.copyOfRange(a, 5, 6)
Arrays.copyOfRange(a, 5, 6, Array<Any>::class.java)
//
a.contentEquals(a)
a.contentDeepEquals(a)
@@ -26,6 +26,7 @@ public class J {
Math.nextUp(x);
Math.pow(x, y);
Math.rint(x);
// Don't convert `Math.round` calls, because Kotlin's `round` throws an exception for a NaN argument
Math.round(x);
Math.round(f);
Math.signum(x);
@@ -4,8 +4,6 @@
// ERROR: Unresolved reference: nextDown
// ERROR: Unresolved reference: nextUp
// ERROR: Unresolved reference: pow
// ERROR: Unresolved reference: roundToLong
// ERROR: Unresolved reference: roundToInt
import kotlin.math.abs
import kotlin.math.acos
import kotlin.math.asin
@@ -60,8 +58,9 @@ class J {
x.nextUp()
x.pow(y)
round(x)
x.roundToLong()
f.roundToInt()
// Don't convert `Math.round` calls, because Kotlin's `round` throws an exception for a NaN argument
Math.round(x)
Math.round(f)
sign(x)
sin(x)
sinh(x)