Java to Kotlin converter: fixed bug
This commit is contained in:
@@ -69,6 +69,7 @@ class TypeConverter(val settings: ConverterSettings, val conversionScope: Conver
|
|||||||
|
|
||||||
private fun variableNullabilityNoCache(variable: PsiVariable): Nullability {
|
private fun variableNullabilityNoCache(variable: PsiVariable): Nullability {
|
||||||
if (variable is PsiEnumConstant) return Nullability.NotNull
|
if (variable is PsiEnumConstant) return Nullability.NotNull
|
||||||
|
if (variable.getType() is PsiPrimitiveType) return Nullability.NotNull
|
||||||
|
|
||||||
var nullability = variable.nullabilityFromAnnotations()
|
var nullability = variable.nullabilityFromAnnotations()
|
||||||
|
|
||||||
@@ -154,6 +155,8 @@ class TypeConverter(val settings: ConverterSettings, val conversionScope: Conver
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun methodNullabilityNoCache(method: PsiMethod): Nullability {
|
private fun methodNullabilityNoCache(method: PsiMethod): Nullability {
|
||||||
|
if (method.getReturnType() is PsiPrimitiveType) return Nullability.NotNull
|
||||||
|
|
||||||
var nullability = method.nullabilityFromAnnotations()
|
var nullability = method.nullabilityFromAnnotations()
|
||||||
|
|
||||||
if (nullability == Nullability.Default) {
|
if (nullability == Nullability.Default) {
|
||||||
|
|||||||
@@ -47,19 +47,9 @@ abstract class NotNullType() : Type() {
|
|||||||
abstract class Type() : Element() {
|
abstract class Type() : Element() {
|
||||||
abstract val isNullable: Boolean
|
abstract val isNullable: Boolean
|
||||||
|
|
||||||
open fun toNotNullType(): Type {
|
open fun toNotNullType(): Type = this
|
||||||
if (isNullable) {
|
|
||||||
throw UnsupportedOperationException("toNotNullType must be defined")
|
|
||||||
}
|
|
||||||
return this
|
|
||||||
}
|
|
||||||
|
|
||||||
open fun toNullableType(): Type {
|
open fun toNullableType(): Type = this
|
||||||
if (!isNullable) {
|
|
||||||
throw UnsupportedOperationException("toNullableType must be defined")
|
|
||||||
}
|
|
||||||
return this
|
|
||||||
}
|
|
||||||
|
|
||||||
object Empty : NotNullType() {
|
object Empty : NotNullType() {
|
||||||
override fun generateCode(builder: CodeBuilder) {
|
override fun generateCode(builder: CodeBuilder) {
|
||||||
|
|||||||
@@ -2084,6 +2084,11 @@ public class JavaToKotlinConverterTestGenerated extends AbstractJavaToKotlinConv
|
|||||||
doTest("j2k/tests/testData/ast/nullability/MethodReturnsTernaryNull.java");
|
doTest("j2k/tests/testData/ast/nullability/MethodReturnsTernaryNull.java");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@TestMetadata("NullableIntNoCrash.java")
|
||||||
|
public void testNullableIntNoCrash() throws Exception {
|
||||||
|
doTest("j2k/tests/testData/ast/nullability/NullableIntNoCrash.java");
|
||||||
|
}
|
||||||
|
|
||||||
@TestMetadata("NullableMethodDotAccess.java")
|
@TestMetadata("NullableMethodDotAccess.java")
|
||||||
public void testNullableMethodDotAccess() throws Exception {
|
public void testNullableMethodDotAccess() throws Exception {
|
||||||
doTest("j2k/tests/testData/ast/nullability/NullableMethodDotAccess.java");
|
doTest("j2k/tests/testData/ast/nullability/NullableMethodDotAccess.java");
|
||||||
|
|||||||
@@ -0,0 +1,8 @@
|
|||||||
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
|
||||||
|
//file
|
||||||
|
class A {
|
||||||
|
int field = foo();
|
||||||
|
|
||||||
|
@Nullable int foo() { return 1; }
|
||||||
|
}
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
class A() {
|
||||||
|
var field = foo()
|
||||||
|
|
||||||
|
fun foo(): Int {
|
||||||
|
return 1
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user