Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

#43 Improve Array handling

  • Loading branch information...
commit d1bdf688b8a99b271d7573821a5c83e8edfd9f44 1 parent d8b8631
@timowest timowest authored
View
40 scalagen/src/main/scala/com/mysema/scalagen/ScalaDumpVisitor.scala
@@ -446,7 +446,7 @@ class ScalaDumpVisitor extends VoidVisitor[ScalaDumpVisitor.Context] with Helper
printer.print("]")
}
}
-
+
def visit(n: WildcardType, arg: Context) {
printer.print("_")
if (n.getExtends != null) {
@@ -536,23 +536,24 @@ class ScalaDumpVisitor extends VoidVisitor[ScalaDumpVisitor.Context] with Helper
def visit(n: ArrayCreationExpr, arg: Context) {
if (n.getDimensions != null) {
- printer.print("new ")
if (arg.assignType != null) {
+ printer.print("new ")
arg.assignType.accept(this, arg)
} else {
- val max = if (n.getDimensions != null) n.getArrayCount + 1 else n.getArrayCount
+ val max = n.getArrayCount + 1
+ printer.print("Array.ofDim[")
for (i <- 0 until max) {
- printer.print("Array[")
- }
- val typeArg = arg.typeArg
- arg.typeArg = true
- n.getType.accept(this, arg)
- arg.typeArg = typeArg
- for (i <- 0 until max) {
- printer.print("]")
+ val typeArg = arg.typeArg
+ arg.typeArg = true
+ n.getType.accept(this, arg)
+ arg.typeArg = typeArg
+ if (i > 0) {
+ printer.print(",")
+ }
}
- }
+ printer.print("]")
+ }
printer.print(n.getDimensions.map(print(_,arg)).mkString("(",", ",")"))
} else {
@@ -939,11 +940,22 @@ class ScalaDumpVisitor extends VoidVisitor[ScalaDumpVisitor.Context] with Helper
}
if (v.getInit == null || v.getInit.isInstanceOf[NullLiteralExpr]){
v.getId.accept(this, arg)
- printer.print(": ")
+ printer.print(": ")
+ for (i <- 0 until v.getId.getArrayCount) {
+ printer.print("Array[")
+ }
n.getType.accept(this, arg)
+ for (i <- 0 until v.getId.getArrayCount) {
+ printer.print("]")
+ }
if (!asParameter) {
printer.print(" = ")
- printer.print(if (v.getInit() == null) "_" else "null")
+ if (n.getType.isInstanceOf[PrimitiveType]) {
+ printer.print("0")
+ } else {
+ printer.print("null")
+ }
+ //printer.print(if (v.getInit() == null) "_" else "null")
}
} else {
v.accept(this, arg)
View
12 scalagen/src/test/scala/com/mysema/examples/ArrayTests.java
@@ -17,4 +17,16 @@ void bar2() {
String el1s[] = new String[]{"a","b"};
String el2s[] = new String[]{"a","b","c"};
}
+
+ void bar3() {
+ final int SIZE = 3;
+ String strings1[];
+ strings1 = new String[SIZE];
+ }
+
+ void bar4() {
+ final int SIZE = 3;
+ String[] strings2;
+ strings2 = new String[SIZE];
+ }
}
View
2  scalagen/src/test/scala/com/mysema/scalagen/SerializationTest.scala
@@ -60,7 +60,7 @@ class SerializationTest extends AbstractParserTest {
@Test
def ArrayTests {
val sources = toScala[ArrayTests]
- assertContains(sources, "def foo(): Array[Int] = new Array[Int](2)")
+ assertContains(sources, "def foo(): Array[Int] = Array.ofDim[Int](2)")
}
@Test
Please sign in to comment.
Something went wrong with that request. Please try again.