From 8eb2e28bff315eaf728f190aede5609114f2a521 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timo=20Westk=C3=A4mper?= Date: Sun, 19 Sep 2010 15:48:55 +0000 Subject: [PATCH] added custom class header option --- .../java/com/mysema/codegen/ScalaWriter.java | 28 +++++++++++++++---- .../com/mysema/codegen/ScalaWriterTest.java | 28 +++++++++++++++---- 2 files changed, 45 insertions(+), 11 deletions(-) diff --git a/src/main/java/com/mysema/codegen/ScalaWriter.java b/src/main/java/com/mysema/codegen/ScalaWriter.java index d72f1e985..b09a66f27 100644 --- a/src/main/java/com/mysema/codegen/ScalaWriter.java +++ b/src/main/java/com/mysema/codegen/ScalaWriter.java @@ -63,11 +63,18 @@ public class ScalaWriter extends AbstractCodeWriter{ private Type type; + private final boolean compact; + public ScalaWriter(Appendable appendable){ - super(appendable); - this.packages.add("java.lang"); + this(appendable, false); } + public ScalaWriter(Appendable appendable, boolean compact){ + super(appendable); + this.packages.add("java.lang"); + this.compact = compact; + } + @Override public ScalaWriter annotation(Annotation annotation) throws IOException { beginLine().append("@").appendType(annotation.annotationType()); @@ -144,6 +151,12 @@ public class ScalaWriter extends AbstractCodeWriter{ } return this; } + + public ScalaWriter beginClass(String header) throws IOException{ + line(PUBLIC_CLASS, header); + goIn(); + return this; + } @Override public ScalaWriter beginClass(Type type) throws IOException { @@ -153,7 +166,7 @@ public class ScalaWriter extends AbstractCodeWriter{ @Override public ScalaWriter beginClass(Type type, Type superClass, Type... interfaces) throws IOException { packages.add(type.getPackageName()); - beginLine(PUBLIC_CLASS + type.getSimpleName()); + beginLine(PUBLIC_CLASS, type.getSimpleName()); if (superClass != null){ append(EXTENDS + getGenericName(false, superClass)); } @@ -241,15 +254,18 @@ public class ScalaWriter extends AbstractCodeWriter{ } public ScalaWriter field(Type type, String name) throws IOException { - return line(VAR + name + ": " + getGenericName(true, type) + SEMICOLON).nl(); + line(VAR + name + ": " + getGenericName(true, type) + SEMICOLON); + return compact ? this : nl(); } private ScalaWriter field(String modifier, Type type, String name) throws IOException{ - return line(modifier + name + ": " + getGenericName(true, type) + SEMICOLON).nl(); + line(modifier + name + ": " + getGenericName(true, type) + SEMICOLON); + return compact ? this : nl(); } private ScalaWriter field(String modifier, Type type, String name, String value) throws IOException{ - return line(modifier + name + ": " + getGenericName(true, type) + ASSIGN + value + SEMICOLON).nl(); + line(modifier + name + ": " + getGenericName(true, type) + ASSIGN + value + SEMICOLON); + return compact ? this : nl(); } @Override diff --git a/src/test/java/com/mysema/codegen/ScalaWriterTest.java b/src/test/java/com/mysema/codegen/ScalaWriterTest.java index 9625a0d0a..49b1097cc 100644 --- a/src/test/java/com/mysema/codegen/ScalaWriterTest.java +++ b/src/test/java/com/mysema/codegen/ScalaWriterTest.java @@ -30,7 +30,7 @@ public class ScalaWriterTest { private Writer w = new StringWriter(); - private ScalaWriter writer = new ScalaWriter(w); + private ScalaWriter writer = new ScalaWriter(w, true); private Type testType, testType2, testSuperType, testInterface1, testInterface2; @@ -44,7 +44,25 @@ public class ScalaWriterTest { } @Test - public void beanAccessors() throws IOException{ + public void CustomHeader() throws IOException{ +// class QDepartment(path: String) extends RelationalPathBase[QDepartment](classOf[QDepartment], path){ +// val id = createNumber("ID", classOf[Integer]); +// val company = createNumber("COMPANY", classOf[Integer]); +// val idKey = createPrimaryKey(id); +// val companyKey: ForeignKey[QCompany] = createForeignKey(company, "ID"); +// } + writer.beginClass("QDepartment(path: String) extends RelationalPathBase[QDepartment](classOf[QDepartment], path)"); + writer.publicFinal(Types.OBJECT, "id", "createNumber(\"ID\", classOf[Integer])"); + writer.publicFinal(Types.OBJECT, "company", "createNumber(\"COMPANY\", classOf[Integer])"); + writer.publicFinal(Types.OBJECT, "idKey", "createPrimaryKey(id)"); + writer.publicFinal(Types.OBJECT, "companyKey", "createForeignKey(company,\"ID\")"); + writer.end(); + + System.out.println(w); + } + + @Test + public void BeanAccessors() throws IOException{ writer.beginClass(new SimpleType("Person")); writer.beginPublicMethod(Types.STRING, "getName"); writer.line("\"Daniel Spiewak\""); @@ -58,7 +76,7 @@ public class ScalaWriterTest { } @Test - public void arrays() throws IOException{ + public void Arrays() throws IOException{ // def main(args: Array[String]) { writer.beginClass(new SimpleType("Main")); writer.field(Types.STRING.asArrayType(), "stringArray"); @@ -73,7 +91,7 @@ public class ScalaWriterTest { } @Test - public void trait() throws IOException{ + public void Trait() throws IOException{ // trait MyTrait writer.beginInterface(new SimpleType("MyTrait")); writer.line("//"); @@ -84,7 +102,7 @@ public class ScalaWriterTest { } @Test - public void fields() throws IOException{ + public void Field() throws IOException{ // private val people: List[Person] writer.imports(List.class); writer.beginClass(new SimpleType("Main"));