Fix for KT-9594 J2K: null cast incorrect conversion.
#KT-9594 fixed
This commit is contained in:
committed by
Simon Ogorodnik
parent
e5b8943d41
commit
5edb872224
@@ -109,7 +109,7 @@ class CodeConverter(
|
|||||||
|
|
||||||
val actualType = expression.type ?: return convertedExpression
|
val actualType = expression.type ?: return convertedExpression
|
||||||
|
|
||||||
if (actualType is PsiPrimitiveType || actualType is PsiClassType && expectedType is PsiPrimitiveType) {
|
if ((actualType is PsiPrimitiveType && actualType != PsiType.NULL) || actualType is PsiClassType && expectedType is PsiPrimitiveType) {
|
||||||
convertedExpression = BangBangExpression.surroundIfNullable(convertedExpression)
|
convertedExpression = BangBangExpression.surroundIfNullable(convertedExpression)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -270,6 +270,12 @@ class DefaultExpressionConverter : JavaElementVisitor(), ExpressionConverter {
|
|||||||
val value = expression.value
|
val value = expression.value
|
||||||
var text = expression.text!!
|
var text = expression.text!!
|
||||||
val type = expression.type
|
val type = expression.type
|
||||||
|
|
||||||
|
if(expression.isNullLiteral()) {
|
||||||
|
result = LiteralExpression.NullLiteral
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
if (type != null) {
|
if (type != null) {
|
||||||
val typeStr = type.canonicalText
|
val typeStr = type.canonicalText
|
||||||
if (typeStr == "double") {
|
if (typeStr == "double") {
|
||||||
@@ -314,6 +320,7 @@ class DefaultExpressionConverter : JavaElementVisitor(), ExpressionConverter {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
result = LiteralExpression(text)
|
result = LiteralExpression(text)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -84,10 +84,16 @@ class TypeCastExpression(val type: Type, val expression: Expression) : Expressio
|
|||||||
get() = type.isNullable
|
get() = type.isNullable
|
||||||
}
|
}
|
||||||
|
|
||||||
class LiteralExpression(val literalText: String) : Expression() {
|
open class LiteralExpression(val literalText: String) : Expression() {
|
||||||
|
|
||||||
override fun generateCode(builder: CodeBuilder) {
|
override fun generateCode(builder: CodeBuilder) {
|
||||||
builder.append(literalText)
|
builder.append(literalText)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
object NullLiteral : LiteralExpression("null"){
|
||||||
|
override val isNullable: Boolean
|
||||||
|
get() = true
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class ParenthesizedExpression(val expression: Expression) : Expression() {
|
class ParenthesizedExpression(val expression: Expression) : Expression() {
|
||||||
|
|||||||
@@ -0,0 +1,2 @@
|
|||||||
|
//expression
|
||||||
|
(Object) null
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
null as Any?
|
||||||
@@ -4823,6 +4823,12 @@ public class JavaToKotlinConverterForWebDemoTestGenerated extends AbstractJavaTo
|
|||||||
doTest(fileName);
|
doTest(fileName);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@TestMetadata("castNullObject.java")
|
||||||
|
public void testCastNullObject() throws Exception {
|
||||||
|
String fileName = KotlinTestUtils.navigationMetadata("j2k/testData/fileOrElement/typeCastExpression/castNullObject.java");
|
||||||
|
doTest(fileName);
|
||||||
|
}
|
||||||
|
|
||||||
@TestMetadata("castNullable.java")
|
@TestMetadata("castNullable.java")
|
||||||
public void testCastNullable() throws Exception {
|
public void testCastNullable() throws Exception {
|
||||||
String fileName = KotlinTestUtils.navigationMetadata("j2k/testData/fileOrElement/typeCastExpression/castNullable.java");
|
String fileName = KotlinTestUtils.navigationMetadata("j2k/testData/fileOrElement/typeCastExpression/castNullable.java");
|
||||||
|
|||||||
@@ -4823,6 +4823,12 @@ public class JavaToKotlinConverterSingleFileTestGenerated extends AbstractJavaTo
|
|||||||
doTest(fileName);
|
doTest(fileName);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@TestMetadata("castNullObject.java")
|
||||||
|
public void testCastNullObject() throws Exception {
|
||||||
|
String fileName = KotlinTestUtils.navigationMetadata("j2k/testData/fileOrElement/typeCastExpression/castNullObject.java");
|
||||||
|
doTest(fileName);
|
||||||
|
}
|
||||||
|
|
||||||
@TestMetadata("castNullable.java")
|
@TestMetadata("castNullable.java")
|
||||||
public void testCastNullable() throws Exception {
|
public void testCastNullable() throws Exception {
|
||||||
String fileName = KotlinTestUtils.navigationMetadata("j2k/testData/fileOrElement/typeCastExpression/castNullable.java");
|
String fileName = KotlinTestUtils.navigationMetadata("j2k/testData/fileOrElement/typeCastExpression/castNullable.java");
|
||||||
|
|||||||
Reference in New Issue
Block a user