Java to Kotlin converter: generate "!!." instead of "?." for nullable values
#KT-3943 Fixed
This commit is contained in:
@@ -73,14 +73,11 @@ class SuperExpression(val identifier: Identifier) : Expression() {
|
||||
|
||||
class QualifiedExpression(val expression: Expression, val identifier: Expression) : Expression() {
|
||||
override val isNullable: Boolean
|
||||
get() {
|
||||
if (!expression.isEmpty && expression.isNullable) return true
|
||||
return identifier.isNullable
|
||||
}
|
||||
get() = identifier.isNullable
|
||||
|
||||
override fun toKotlin(): String {
|
||||
if (!expression.isEmpty) {
|
||||
return operandToKotlin(expression) + (if (expression.isNullable) "?." else ".") + identifier.toKotlin()
|
||||
return operandToKotlin(expression) + (if (expression.isNullable) "!!." else ".") + identifier.toKotlin()
|
||||
}
|
||||
|
||||
return identifier.toKotlin()
|
||||
|
||||
@@ -18,20 +18,19 @@ package org.jetbrains.jet.j2k.ast
|
||||
|
||||
import java.util.ArrayList
|
||||
|
||||
open class MethodCallExpression(
|
||||
class MethodCallExpression(
|
||||
val methodExpression: Expression,
|
||||
val arguments: List<Expression>,
|
||||
val typeParameters: List<Type>,
|
||||
val resultIsNullable: Boolean = false
|
||||
override val isNullable: Boolean = false
|
||||
) : Expression() {
|
||||
|
||||
override val isNullable: Boolean
|
||||
get() = methodExpression.isNullable || resultIsNullable
|
||||
|
||||
override fun toKotlin(): String {
|
||||
val typeParamsToKotlin: String = typeParameters.toKotlin(", ", "<", ">")
|
||||
val argumentsMapped = arguments.map { it.toKotlin() }
|
||||
return operandToKotlin(methodExpression) + typeParamsToKotlin + "(" + argumentsMapped.makeString(", ") + ")"
|
||||
return operandToKotlin(methodExpression) +
|
||||
typeParameters.toKotlin(", ", "<", ">") +
|
||||
"(" +
|
||||
arguments.map { it.toKotlin() }.makeString(", ") +
|
||||
")"
|
||||
}
|
||||
|
||||
class object {
|
||||
|
||||
@@ -6,7 +6,7 @@ public class Test(str: String) {
|
||||
var myStr: String = "String2"
|
||||
|
||||
public fun sout(str: String) {
|
||||
System.out?.println(str)
|
||||
System.out!!.println(str)
|
||||
}
|
||||
|
||||
public fun dummy(str: String): String {
|
||||
|
||||
@@ -15,8 +15,8 @@ class Bar() {
|
||||
class Test() {
|
||||
public fun test(barNotNull: Bar, barNullable: Bar?) {
|
||||
barNotNull.fooNotNull.execute()
|
||||
barNotNull.fooNullable?.execute()
|
||||
barNullable?.fooNotNull?.execute()
|
||||
barNullable?.fooNullable?.execute()
|
||||
barNotNull.fooNullable!!.execute()
|
||||
barNullable!!.fooNotNull.execute()
|
||||
barNullable!!.fooNullable!!.execute()
|
||||
}
|
||||
}
|
||||
@@ -6,6 +6,6 @@ class Foo() {
|
||||
fun foo(o: BitSet?) {
|
||||
val o2: BitSet? = o
|
||||
val foo: Int = 0
|
||||
foo = o2?.size()!!
|
||||
foo = o2!!.size()
|
||||
}
|
||||
}
|
||||
+2
-2
@@ -13,9 +13,9 @@ class User() {
|
||||
fun main() {
|
||||
val lib: Library = Library()
|
||||
lib.call()
|
||||
lib.getString()?.isEmpty()
|
||||
lib.getString()!!.isEmpty()
|
||||
|
||||
Library().call()
|
||||
Library().getString()?.isEmpty()
|
||||
Library().getString()!!.isEmpty()
|
||||
}
|
||||
}
|
||||
@@ -8,11 +8,11 @@ import java.io.File
|
||||
public class Test() {
|
||||
class object {
|
||||
public fun isDir(parent: File?): Boolean {
|
||||
if (parent == null || !parent?.exists()!!) {
|
||||
if (parent == null || !parent!!.exists()) {
|
||||
return false
|
||||
}
|
||||
val result = true
|
||||
if (parent?.isDirectory()!!) {
|
||||
if (parent!!.isDirectory()) {
|
||||
return true
|
||||
} else
|
||||
return false
|
||||
|
||||
@@ -2,6 +2,6 @@ import kotlinApi.*
|
||||
|
||||
class A() {
|
||||
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() {
|
||||
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 {
|
||||
return getString(true)?.length()!!
|
||||
return getString(true)!!.length()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
fun foo(s: String?, b: Boolean): Int {
|
||||
if (s == null) System.out.println("null")
|
||||
if (b) return s?.length()!!
|
||||
if (b) return s!!.length()
|
||||
return 10
|
||||
}
|
||||
@@ -1,3 +1,3 @@
|
||||
val s = null
|
||||
if (!s?.isEmpty()!!) {
|
||||
if (!s!!.isEmpty()) {
|
||||
}
|
||||
Reference in New Issue
Block a user