Skip to content

Commit

Permalink
Fix primitive init handling #56
Browse files Browse the repository at this point in the history
  • Loading branch information
timowest committed Jan 26, 2014
1 parent 1995173 commit 96c841d
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 15 deletions.
34 changes: 31 additions & 3 deletions scalagen/src/main/scala/com/mysema/scalagen/ScalaDumpVisitor.scala
Expand Up @@ -1031,6 +1031,30 @@ class ScalaDumpVisitor(settings: ConversionSettings) extends VoidVisitor[ScalaDu
}
printArguments(n.getArgs, arg)
}

def isTypeInitMatch(n: VariableDeclarationExpr, v: VariableDeclarator) = {
import PrimitiveType.Primitive
val init = v.getInit
if (init.isInstanceOf[LiteralExpr]) {
n.getType match {
case ptype: PrimitiveType => {
ptype.getType match {
case Primitive.Boolean => init.isInstanceOf[BooleanLiteralExpr]
case Primitive.Byte => false
case Primitive.Char => init.isInstanceOf[CharLiteralExpr]
case Primitive.Double => init.isInstanceOf[DoubleLiteralExpr]
case Primitive.Float => false
case Primitive.Int => init.isInstanceOf[IntegerLiteralExpr]
case Primitive.Long => init.isInstanceOf[LongLiteralExpr]
case Primitive.Short => false
}
}
case _ => true
}
} else {
true
}
}

def visit(n: VariableDeclarationExpr, arg: Context) {
val asParameter = n.getModifiers == -1
Expand All @@ -1042,7 +1066,7 @@ class ScalaDumpVisitor(settings: ConversionSettings) extends VoidVisitor[ScalaDu
if (!asParameter) {
printer.print(modifier)
}
if (v.getInit == null || v.getInit.isInstanceOf[NullLiteralExpr]){
if (v.getInit == null || v.getInit.isInstanceOf[NullLiteralExpr] || !isTypeInitMatch(n, v)){
v.getId.accept(this, arg)
printer.print(": ")
for (i <- 0 until v.getId.getArrayCount) {
Expand All @@ -1055,8 +1079,12 @@ class ScalaDumpVisitor(settings: ConversionSettings) extends VoidVisitor[ScalaDu
if (!asParameter) {
printer.print(" = ")
if (n.getType.isInstanceOf[PrimitiveType]) {
val ptype = n.getType.asInstanceOf[PrimitiveType]
printer.print(DEFAULTS(ptype.getType))
if (v.getInit != null) {
v.getInit.accept(this, arg)
} else {
val ptype = n.getType.asInstanceOf[PrimitiveType]
printer.print(DEFAULTS(ptype.getType))
}
} else {
printer.print("null")
}
Expand Down
12 changes: 0 additions & 12 deletions scalagen/src/test/scala/com/mysema/examples/DoublePrimitive.java

This file was deleted.

27 changes: 27 additions & 0 deletions scalagen/src/test/scala/com/mysema/examples/PrimitiveInits.java
@@ -0,0 +1,27 @@
package com.mysema.examples;

public class PrimitiveInits {

double a = 3;

void doSomething() {
// byte
byte b = 0;
// char
char c = 0;
// int
int i = 2;
// long
long l1 = 1;
long l2 = 2l;
// float
float f1 = 3f;
float f2 = 3;
// double
double d1 = 2d;
double d2 = 2.0;
double d3 = 2;
System.out.println(a + d1);
}

}

0 comments on commit 96c841d

Please sign in to comment.