Browse files

#14 added var to val conversion

  • Loading branch information...
1 parent a8a0152 commit c89db24feb8485a7a80e8c00a3d25c6000287071 @timowest committed Jan 22, 2012
View
3 scalagen/src/main/scala/com/mysema/scalagen/Converter.scala
@@ -25,7 +25,8 @@ object Converter {
* default instance for Converter type
*/
lazy val instance = new Converter("UTF-8",List[UnitTransformer](
- Rethrows,
+ Rethrows,
+ VarToVal,
RemoveAsserts,
Annotations,
Enums,
View
7 scalagen/src/main/scala/com/mysema/scalagen/Enums.scala
@@ -62,13 +62,6 @@ class Enums extends UnitTransformerBase {
conversion.setBody(new Return(new Cast(ty, "v")))
conversion.setParameters(new Parameter(valueType, "v") :: Nil)
-// val members = new ArrayList[BodyDecl]()
-// members.addAll(entries)
-// members.add(typeDecl)
-// members.addAll(n.getMembers.filter(isStatic))
-// members.add(conversion)
-// members
-
entries ::: typeDecl :: n.getMembers.filter(isStatic) ::: conversion :: Nil
}
View
73 scalagen/src/main/scala/com/mysema/scalagen/VarToVal.scala
@@ -0,0 +1,73 @@
+/*
+ * Copyright (C) 2011, Mysema Ltd
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ */
+package com.mysema.scalagen
+
+import java.util.ArrayList
+import japa.parser.ast.CompilationUnit
+import UnitTransformer._
+import defs._
+import japa.parser.ast.body.ModifierSet
+
+object VarToVal extends VarToVal
+
+object defs {
+ type Vars = List[Map[String,VariableDeclaration]]
+}
+
+/**
+ * VarToVal changes var to val if no reassignments are done
+ */
+class VarToVal extends ModifierVisitor[Vars] with UnitTransformer {
+
+ def transform(cu: CompilationUnit): CompilationUnit = {
+ cu.accept(this, Nil).asInstanceOf[CompilationUnit]
+ }
+
+ override def visit(n: Block, arg: Vars): Node = {
+ if (n.getStmts == null) {
+ return n
+ }
+ val vars = n.getStmts.collect { case Stmt(v: VariableDeclaration) => v }
+ .flatMap(v => v.getVars.map(va => (va.getId.getName,v)))
+ .toMap
+
+ // set vars to final
+ vars.values
+ .foreach { v => v.setModifiers(v.getModifiers.addModifier(ModifierSet.FINAL)) }
+
+ super.visit(n, vars :: arg)
+ }
+
+ override def visit(n: Assign, arg: Vars): Node = {
+ removeFinal(n.getTarget.toString, arg)
+ n
+ }
+
+ override def visit(n: Unary, arg: Vars): Node = {
+ if (n.getOperator.toString.endsWith("crement")) {
+ removeFinal(n.getExpr.toString, arg)
+ }
+ n
+ }
+
+ // leave VariableDeclarations unchanged
+ override def visit(n: VariableDeclaration, arg: Vars): Node = n
+
+ private def removeFinal(key: String, arg: Vars) {
+ arg.find(_.contains(key))
+ .flatMap(_.get(key))
+ .foreach { v => v.setModifiers(v.getModifiers().removeModifier(ModifierSet.FINAL))}
+ }
+
+}
View
4 scalagen/src/test/scala/com/mysema/scalagen/ConvertProjects.scala
@@ -29,10 +29,10 @@ object ConvertProjects extends AbstractParserTest {
def main(args: Array[String]) {
val start = System.currentTimeMillis()
//Converter.instance.convert(new File("../../quercus"), new File("target/quercus"))
- Converter.instance.convert(new File("../../../querydsl"), new File("target/querydsl"))
- Converter.instance.convert(new File("../../../rdfbean"), new File("target/rdfbean"))
+ Converter.instance.convert(new File("../../../querydsl"), new File("target/querydsl"))
Converter.instance.convert(new File("../../codegen"), new File("target/codegen"))
Converter.instance.convert(new File("../../webmin"), new File("target/webmin"))
+ Converter.instance.convert(new File("../../../rdfbean"), new File("target/rdfbean"))
val duration = System.currentTimeMillis() - start
println("took " + (duration/1000) + "s")
}
View
10 scalagen/src/test/scala/com/mysema/scalagen/Enums.scala
@@ -0,0 +1,10 @@
+package com.mysema.scalagen
+
+object Gender extends Enumeration {
+ val MALE = new Gender(0)
+ val FEMALE = new Gender(1)
+}
+
+class Gender(val id: Int) extends Gender.Value {
+
+}
View
2 scalagen/src/test/scala/com/mysema/scalagen/HelpersTest.scala
@@ -8,7 +8,7 @@ import java.util.Collections
class HelpersTest {
- object helpers extends Helpers
+ val helpers = new AnyRef with Helpers
@Test
def IsHashCode {

0 comments on commit c89db24

Please sign in to comment.