Java to Kotlin converter: generate "!!." instead of "?." for nullable values

#KT-3943 Fixed
This commit is contained in:
Valentin Kipyatkov
2014-06-10 18:30:58 +04:00
parent a07dae9734
commit b71061eb5c
12 changed files with 24 additions and 28 deletions
@@ -73,14 +73,11 @@ class SuperExpression(val identifier: Identifier) : Expression() {
class QualifiedExpression(val expression: Expression, val identifier: Expression) : Expression() { class QualifiedExpression(val expression: Expression, val identifier: Expression) : Expression() {
override val isNullable: Boolean override val isNullable: Boolean
get() { get() = identifier.isNullable
if (!expression.isEmpty && expression.isNullable) return true
return identifier.isNullable
}
override fun toKotlin(): String { override fun toKotlin(): String {
if (!expression.isEmpty) { if (!expression.isEmpty) {
return operandToKotlin(expression) + (if (expression.isNullable) "?." else ".") + identifier.toKotlin() return operandToKotlin(expression) + (if (expression.isNullable) "!!." else ".") + identifier.toKotlin()
} }
return identifier.toKotlin() return identifier.toKotlin()
@@ -18,20 +18,19 @@ package org.jetbrains.jet.j2k.ast
import java.util.ArrayList import java.util.ArrayList
open class MethodCallExpression( class MethodCallExpression(
val methodExpression: Expression, val methodExpression: Expression,
val arguments: List<Expression>, val arguments: List<Expression>,
val typeParameters: List<Type>, val typeParameters: List<Type>,
val resultIsNullable: Boolean = false override val isNullable: Boolean = false
) : Expression() { ) : Expression() {
override val isNullable: Boolean
get() = methodExpression.isNullable || resultIsNullable
override fun toKotlin(): String { override fun toKotlin(): String {
val typeParamsToKotlin: String = typeParameters.toKotlin(", ", "<", ">") return operandToKotlin(methodExpression) +
val argumentsMapped = arguments.map { it.toKotlin() } typeParameters.toKotlin(", ", "<", ">") +
return operandToKotlin(methodExpression) + typeParamsToKotlin + "(" + argumentsMapped.makeString(", ") + ")" "(" +
arguments.map { it.toKotlin() }.makeString(", ") +
")"
} }
class object { class object {
@@ -6,7 +6,7 @@ public class Test(str: String) {
var myStr: String = "String2" var myStr: String = "String2"
public fun sout(str: String) { public fun sout(str: String) {
System.out?.println(str) System.out!!.println(str)
} }
public fun dummy(str: String): String { public fun dummy(str: String): String {
@@ -15,8 +15,8 @@ class Bar() {
class Test() { class Test() {
public fun test(barNotNull: Bar, barNullable: Bar?) { public fun test(barNotNull: Bar, barNullable: Bar?) {
barNotNull.fooNotNull.execute() barNotNull.fooNotNull.execute()
barNotNull.fooNullable?.execute() barNotNull.fooNullable!!.execute()
barNullable?.fooNotNull?.execute() barNullable!!.fooNotNull.execute()
barNullable?.fooNullable?.execute() barNullable!!.fooNullable!!.execute()
} }
} }
@@ -6,6 +6,6 @@ class Foo() {
fun foo(o: BitSet?) { fun foo(o: BitSet?) {
val o2: BitSet? = o val o2: BitSet? = o
val foo: Int = 0 val foo: Int = 0
foo = o2?.size()!! foo = o2!!.size()
} }
} }
@@ -13,9 +13,9 @@ class User() {
fun main() { fun main() {
val lib: Library = Library() val lib: Library = Library()
lib.call() lib.call()
lib.getString()?.isEmpty() lib.getString()!!.isEmpty()
Library().call() Library().call()
Library().getString()?.isEmpty() Library().getString()!!.isEmpty()
} }
} }
@@ -8,11 +8,11 @@ import java.io.File
public class Test() { public class Test() {
class object { class object {
public fun isDir(parent: File?): Boolean { public fun isDir(parent: File?): Boolean {
if (parent == null || !parent?.exists()!!) { if (parent == null || !parent!!.exists()) {
return false return false
} }
val result = true val result = true
if (parent?.isDirectory()!!) { if (parent!!.isDirectory()) {
return true return true
} else } else
return false return false
@@ -2,6 +2,6 @@ import kotlinApi.*
class A() { class A() {
fun foo(t: KotlinTrait): Int { fun foo(t: KotlinTrait): Int {
return t.nullableFun()?.length()!! + t.notNullableFun().length() return t.nullableFun()!!.length() + t.notNullableFun().length()
} }
} }
@@ -2,6 +2,6 @@ import kotlinApi.*
class A() { class A() {
fun foo(c: KotlinClass): Int { fun foo(c: KotlinClass): Int {
return c.nullableProperty?.length()!! + c.property.length() + KotlinClass.nullableStaticVar!! + KotlinClass.staticVar + KotlinClass.nullableStaticFun(1)!! + KotlinClass.staticFun(1) + nullableGlobalFunction("")?.length()!! + globalFunction("").length() return c.nullableProperty!!.length() + c.property.length() + KotlinClass.nullableStaticVar!! + KotlinClass.staticVar + KotlinClass.nullableStaticFun(1)!! + KotlinClass.staticFun(1) + nullableGlobalFunction("")!!.length() + globalFunction("").length()
} }
} }
@@ -4,6 +4,6 @@ class C() {
} }
fun foo(): Int { fun foo(): Int {
return getString(true)?.length()!! return getString(true)!!.length()
} }
} }
@@ -1,5 +1,5 @@
fun foo(s: String?, b: Boolean): Int { fun foo(s: String?, b: Boolean): Int {
if (s == null) System.out.println("null") if (s == null) System.out.println("null")
if (b) return s?.length()!! if (b) return s!!.length()
return 10 return 10
} }
@@ -1,3 +1,3 @@
val s = null val s = null
if (!s?.isEmpty()!!) { if (!s!!.isEmpty()) {
} }