Skip to content

Commit

Permalink
#55 Add test
Browse files Browse the repository at this point in the history
  • Loading branch information
timowest committed Jun 20, 2013
1 parent 026c679 commit 29d0617
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 11 deletions.
25 changes: 14 additions & 11 deletions scalagen/src/main/scala/com/mysema/scalagen/Primitives.scala
Original file line number Diff line number Diff line change
Expand Up @@ -15,33 +15,36 @@ package com.mysema.scalagen

import java.util.ArrayList
import UnitTransformer._
import japa.parser.ast.expr._

object Primitives extends Primitives

/**
* Primitives modifies primitive type related constants and method calls
*/
class Primitives extends UnitTransformerBase {

private val TRUE = new BooleanLiteral(true)

private val FALSE = new BooleanLiteral(false)

private val primitives = Set("Boolean","Byte","Char","Double","Float","Integer","Long","Short")

def transform(cu: CompilationUnit): CompilationUnit = {
cu.accept(this, cu).asInstanceOf[CompilationUnit]
}
cu.accept(this, cu).asInstanceOf[CompilationUnit]
}

override def visit(n: FieldAccess, arg: CompilationUnit): Node = n match {
case FieldAccess(str("Boolean"), "TRUE") => TRUE
case FieldAccess(str("Boolean"), "FALSE") => FALSE
case _ => super.visit(n, arg)
}


// XXX doesn't work properly for e.g. Double.valueOf(String)
override def visit(n: MethodCall, arg: CompilationUnit): Node = n match {
case MethodCall(str(scope), "valueOf", a :: Nil) if primitives.contains(scope) => a.accept(this, arg)
case MethodCall(str(scope), "valueOf", a :: Nil)
if primitives.contains(scope) && !a.isInstanceOf[LiteralExpr] => a.accept(this, arg)
case _ => super.visit(n, arg)
}
}

}
9 changes: 9 additions & 0 deletions scalagen/src/test/scala/com/mysema/examples/ValueOf.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.mysema.examples;

public class ValueOf {

public void foo() {
double a = Double.valueOf("1.0");
System.out.println(a - 3.0);
}
}

0 comments on commit 29d0617

Please sign in to comment.