I fixed ArrayWithoutInitializationExpression class for object array and multi

dimension array. And I added 2 tests to check object Initialization.
This commit is contained in:
hozumi
2013-02-12 03:43:21 +09:00
committed by Pavel V. Talanov
parent 2fb770ab98
commit d03e1f5fcb
9 changed files with 28 additions and 8 deletions
@@ -2,6 +2,7 @@ package org.jetbrains.jet.j2k.ast
import org.jetbrains.jet.j2k.ast.types.ArrayType import org.jetbrains.jet.j2k.ast.types.ArrayType
import org.jetbrains.jet.j2k.ast.types.Type import org.jetbrains.jet.j2k.ast.types.Type
import org.jetbrains.jet.j2k.ast.types.PrimitiveType
public open class ArrayWithoutInitializationExpression(val `type` : Type, val expressions : List<Expression>) : Expression() { public open class ArrayWithoutInitializationExpression(val `type` : Type, val expressions : List<Expression>) : Expression() {
public override fun toKotlin() : String { public override fun toKotlin() : String {
@@ -9,7 +10,7 @@ public open class ArrayWithoutInitializationExpression(val `type` : Type, val ex
return constructInnerType(`type`, expressions) return constructInnerType(`type`, expressions)
} }
return getConstructorName(`type`) return getConstructorName(`type`, expressions.size() != 0)
} }
private fun constructInnerType(hostType : ArrayType, expressions: List<Expression>) : String { private fun constructInnerType(hostType : ArrayType, expressions: List<Expression>) : String {
@@ -22,7 +23,7 @@ public open class ArrayWithoutInitializationExpression(val `type` : Type, val ex
return oneDim(hostType, expressions[0], "{" + constructInnerType(innerType, expressions.subList(1, expressions.size())) + "}") return oneDim(hostType, expressions[0], "{" + constructInnerType(innerType, expressions.subList(1, expressions.size())) + "}")
} }
return getConstructorName(hostType) return getConstructorName(hostType, expressions.size() != 0)
} }
class object { class object {
@@ -31,9 +32,24 @@ public open class ArrayWithoutInitializationExpression(val `type` : Type, val ex
} }
private open fun oneDim(`type` : Type, size : Expression, init : String) : String { private open fun oneDim(`type` : Type, size : Expression, init : String) : String {
return getConstructorName(`type`) + "(" + size.toKotlin() + init.withPrefix(", ") + ")" return getConstructorName(`type`, !init.isEmpty()) + "(" + size.toKotlin() + init.withPrefix(", ") + ")"
} }
private open fun getConstructorName(`type` : Type) : String = `type`.convertedToNotNull().toKotlin() private open fun getConstructorName(`type` : Type, hasInit : Boolean) : String {
return if (`type` is ArrayType)
when (`type`.elementType) {
is PrimitiveType ->
`type`.convertedToNotNull().toKotlin()
is ArrayType ->
if (hasInit)
`type`.convertedToNotNull().toKotlin()
else
"arrayOfNulls<" + `type`.elementType.toKotlin() + ">"
else ->
"arrayOfNulls<" + `type`.elementType.toKotlin() + ">"
}
else
`type`.convertedToNotNull().toKotlin()
}
} }
} }
@@ -0,0 +1 @@
int[] a = new int[10]
@@ -0,0 +1 @@
var a : IntArray? = IntArray(10)
@@ -0,0 +1 @@
Object[] a = new Object[10]
@@ -0,0 +1 @@
var a : Array<Any?>? = arrayOfNulls<Any?>(10)
@@ -1 +1 @@
var d2 : Array<IntArray?>? = Array<IntArray?>(5) var d2 : Array<IntArray?>? = arrayOfNulls<IntArray?>(5)
@@ -1 +1 @@
var d3 : Array<Array<IntArray?>?>? = Array<Array<IntArray?>?>(5, {Array<IntArray?>(5)}) var d3 : Array<Array<IntArray?>?>? = Array<Array<IntArray?>?>(5, {arrayOfNulls<IntArray?>(5)})
@@ -1 +1 @@
var ss : Array<Array<String?>?>? = Array<Array<String?>?>(5, {Array<String?>(5)}) var ss : Array<Array<String?>?>? = Array<Array<String?>?>(5, {arrayOfNulls<String?>(5)})
@@ -1 +1 @@
var sss : Array<Array<Array<String?>?>?>? = Array<Array<Array<String?>?>?>(5, {Array<Array<String?>?>(5, {Array<String?>(5)})}) var sss : Array<Array<Array<String?>?>?>? = Array<Array<Array<String?>?>?>(5, {Array<Array<String?>?>(5, {arrayOfNulls<String?>(5)})})