added custom class header option

This commit is contained in:
Timo Westkämper 2010-09-19 15:48:55 +00:00
parent 3a9cd91641
commit 8eb2e28bff
2 changed files with 45 additions and 11 deletions

View File

@ -63,11 +63,18 @@ public class ScalaWriter extends AbstractCodeWriter<ScalaWriter>{
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<ScalaWriter>{
}
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<ScalaWriter>{
@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<ScalaWriter>{
}
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

View File

@ -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"));