Java to Kotlin converter: do not generate default field initializer when it's incorrect anyway

This commit is contained in:
Valentin Kipyatkov
2014-06-26 15:19:59 +04:00
parent 44c706eb3e
commit f8a412aea0
16 changed files with 21 additions and 22 deletions
@@ -169,7 +169,7 @@ class ConstructorConverter(private val converter: Converter) {
assert(classBody.primaryConstructorSignature == null)
val finalOrWithEmptyInitializerFields = classBody.members.filterIsInstance(javaClass<Field>()).filter { it.isVal || it.initializer.isEmpty }
val initializers = HashMap<Field, Expression>()
val initializers = HashMap<Field, Expression?>()
for (factoryFunction in classBody.factoryFunctions()) {
for (field in finalOrWithEmptyInitializerFields) {
initializers.put(field, getDefaultInitializer(field))
@@ -198,9 +198,8 @@ class ConstructorConverter(private val converter: Converter) {
}
}
val initializer = MethodCallExpression.buildNotNull(null,
className.name,
finalOrWithEmptyInitializerFields.map { initializers[it]!! }).assignNoPrototype()
val arguments = finalOrWithEmptyInitializerFields.map { initializers[it] ?: LiteralExpression("null").assignNoPrototype() }
val initializer = MethodCallExpression.buildNotNull(null, className.name, arguments).assignNoPrototype()
if (statements.isNotEmpty()) {
val localVar = LocalVariable(tempValIdentifier(),
Annotations.Empty,
+3 -3
View File
@@ -49,7 +49,7 @@ fun PsiVariable.countWriteAccesses(scope: PsiElement?): Int
fun PsiVariable.hasWriteAccesses(scope: PsiElement?): Boolean
= if (scope != null) findVariableUsages(this, scope).any { PsiUtil.isAccessedForWriting(it) } else false
fun getDefaultInitializer(field: Field): Expression {
fun getDefaultInitializer(field: Field): Expression? {
val t = field.`type`
val result = if (t.isNullable) {
LiteralExpression("null")
@@ -64,9 +64,9 @@ fun getDefaultInitializer(field: Field): Expression {
}
}
else {
LiteralExpression("0")
null
}
return result.assignNoPrototype()
return result?.assignNoPrototype()
}
fun isVal(field: PsiField): Boolean {
+1 -1
View File
@@ -41,7 +41,7 @@ class Field(
var initializerToUse = initializer
if (initializerToUse.isEmpty && defaultInitializer) {
initializerToUse = getDefaultInitializer(this)
initializerToUse = getDefaultInitializer(this) ?: Expression.Empty
}
if (!initializerToUse.isEmpty) {
builder append " = " append initializerToUse
@@ -1,5 +1,5 @@
class Test {
var str: String = 0
var str: String
{
str = "Ola"
}
@@ -1,5 +1,5 @@
class Test {
var str: String = 0
var str: String
class object {
{
@@ -1,6 +1,6 @@
class Library {
class object {
val ourOut: java.io.PrintStream = 0
val ourOut: java.io.PrintStream
}
}
@@ -1,5 +1,5 @@
class Library {
public val myString: String = 0
public val myString: String
}
class User {
@@ -1,5 +1,5 @@
abstract class Shape {
public var color: String = 0
public var color: String
public fun setColor(c: String) {
color = c
}
@@ -1,5 +1,5 @@
class T {
var a: String = 0
var b: String = 0
var a: String
var b: String
var c = "abc"
}
@@ -1,5 +1,5 @@
class C(x: String) {
public var x: Any = 0
public var x: Any
{
this.x = x
@@ -1,5 +1,5 @@
class C(x: Any, b: Boolean) {
public var x: Any = 0
public var x: Any
{
if (b) {
@@ -2,7 +2,7 @@ public class Test private(private val myName: String, var a: Boolean, var b: Dou
class object {
public fun create(): Test {
return Test(0, false, 0.toDouble(), 0.toFloat(), 0, 0, 0, ' ')
return Test(null, false, 0.toDouble(), 0.toFloat(), 0, 0, 0, ' ')
}
public fun create(name: String): Test {
@@ -1,3 +1,3 @@
class C {
var f: Foo = 0
var f: Foo
}
@@ -1,3 +1,3 @@
class C {
protected var f: Foo = 0
protected var f: Foo
}
+1 -1
View File
@@ -1,3 +1,3 @@
class C {
public var f: Foo = 0
public var f: Foo
}
@@ -1,3 +1,3 @@
class C {
var f: Foo = 0
var f: Foo
}