KT-7695 Java to Kotlin converter incorrectly generates a cast to non-nullable type
#KT-7695 Fixed
This commit is contained in:
@@ -428,11 +428,14 @@ class DefaultExpressionConverter : JavaElementVisitor(), ExpressionConverter {
|
||||
val operandType = operand?.getType()
|
||||
val typeText = castType.getType().getCanonicalText()
|
||||
val typeConversion = PRIMITIVE_TYPE_CONVERSIONS[typeText]
|
||||
val operandConverted = codeConverter.convertExpression(operand)
|
||||
if (operandType is PsiPrimitiveType && typeConversion != null) {
|
||||
result = MethodCallExpression.buildNotNull(codeConverter.convertExpression(operand), typeConversion)
|
||||
result = MethodCallExpression.buildNotNull(operandConverted, typeConversion)
|
||||
}
|
||||
else {
|
||||
result = TypeCastExpression(typeConverter.convertType(castType.getType()), codeConverter.convertExpression(operand))
|
||||
val typeConverted = typeConverter.convertType(castType.getType(),
|
||||
if (operandConverted.isNullable) Nullability.Nullable else Nullability.NotNull)
|
||||
result = TypeCastExpression(typeConverted, operandConverted)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
class A {
|
||||
private String foo(Object o, boolean b) {
|
||||
if (b) return (String) o;
|
||||
return "";
|
||||
}
|
||||
|
||||
void bar() {
|
||||
if (foo(null, true) == null) {
|
||||
System.out.println("null");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
class A {
|
||||
private fun foo(o: Any?, b: Boolean): String? {
|
||||
if (b) return o as String?
|
||||
return ""
|
||||
}
|
||||
|
||||
fun bar() {
|
||||
if (foo(null, true) == null) {
|
||||
println("null")
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -4180,6 +4180,12 @@ public class JavaToKotlinConverterForWebDemoTestGenerated extends AbstractJavaTo
|
||||
JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("j2k/testData/fileOrElement/typeCastExpression"), Pattern.compile("^(.+)\\.java$"), true);
|
||||
}
|
||||
|
||||
@TestMetadata("castNullable.java")
|
||||
public void testCastNullable() throws Exception {
|
||||
String fileName = JetTestUtils.navigationMetadata("j2k/testData/fileOrElement/typeCastExpression/castNullable.java");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("extendsWildcardCast.java")
|
||||
public void testExtendsWildcardCast() throws Exception {
|
||||
String fileName = JetTestUtils.navigationMetadata("j2k/testData/fileOrElement/typeCastExpression/extendsWildcardCast.java");
|
||||
|
||||
@@ -4180,6 +4180,12 @@ public class JavaToKotlinConverterSingleFileTestGenerated extends AbstractJavaTo
|
||||
JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("j2k/testData/fileOrElement/typeCastExpression"), Pattern.compile("^(.+)\\.java$"), true);
|
||||
}
|
||||
|
||||
@TestMetadata("castNullable.java")
|
||||
public void testCastNullable() throws Exception {
|
||||
String fileName = JetTestUtils.navigationMetadata("j2k/testData/fileOrElement/typeCastExpression/castNullable.java");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("extendsWildcardCast.java")
|
||||
public void testExtendsWildcardCast() throws Exception {
|
||||
String fileName = JetTestUtils.navigationMetadata("j2k/testData/fileOrElement/typeCastExpression/extendsWildcardCast.java");
|
||||
|
||||
Reference in New Issue
Block a user