KT-40359 Catch NumberFormatException during literal conversion

- Instead of failing J2K conversion completely, insert TODO expression
to the resulting code (it is better to have a converted code with TODO
than to have nothing at all)
- See EA-210233 for similar exceptions
- ^KT-40359 Fixed
This commit is contained in:
Roman Golyshev
2020-07-20 14:46:14 +03:00
committed by Roman Golyshev
parent cc0a787735
commit cba671a3ef
3 changed files with 29 additions and 4 deletions
@@ -6,16 +6,35 @@
package org.jetbrains.kotlin.nj2k.conversions
import org.jetbrains.annotations.NonNls
import org.jetbrains.kotlin.nj2k.NewJ2kConverterContext
import org.jetbrains.kotlin.nj2k.tree.JKLiteralExpression
import org.jetbrains.kotlin.nj2k.tree.JKTreeElement
import org.jetbrains.kotlin.nj2k.tree.*
import java.math.BigInteger
class LiteralConversion(context: NewJ2kConverterContext) : RecursiveApplicableConversionBase(context) {
override fun applyToElement(element: JKTreeElement): JKTreeElement {
if (element !is JKLiteralExpression) return recurse(element)
element.convertLiteral()
return recurse(element)
val convertedElement = try {
element.apply { convertLiteral() }
} catch (_: NumberFormatException) {
createTodoCall(cannotConvertLiteralMessage(element))
}
return recurse(convertedElement)
}
private fun createTodoCall(@NonNls message: String): JKCallExpressionImpl {
val todoMethodSymbol = symbolProvider.provideMethodSymbol("kotlin.TODO")
val todoMessageArgument = JKArgumentImpl(JKLiteralExpression("\"$message\"", JKLiteralExpression.LiteralType.STRING))
return JKCallExpressionImpl(todoMethodSymbol, JKArgumentList(todoMessageArgument))
}
private fun cannotConvertLiteralMessage(element: JKLiteralExpression): String {
val literalType = element.type.toString().toLowerCase()
val literalValue = element.literal
return "Could not convert $literalType literal '$literalValue' to Kotlin"
}
private fun JKLiteralExpression.convertLiteral() {
+4
View File
@@ -1,8 +1,12 @@
class Test {
int negativeOctalInt = 023432423432;
int octalInt = 07432423432;
int invalidOctalInt = 08;
long negativeOctalLong = 01777777777777777777777L;
long octalLong = 0777777777777777777777L;
long invalidOctalLong = 08L;
short octalShort = 047777;
byte octalByte = 077;
}
+2
View File
@@ -1,8 +1,10 @@
internal class Test {
var negativeOctalInt = -1670764774
var octalInt = 1013589786
var invalidOctalInt: Int = TODO("Could not convert int literal '08' to Kotlin")
var negativeOctalLong = -1L
var octalLong = 9223372036854775807L
var invalidOctalLong: Long = TODO("Could not convert long literal '08L' to Kotlin")
var octalShort: Short = 20479
var octalByte: Byte = 63
}