added escaping of Scala reserved words

This commit is contained in:
Timo Westkämper 2011-05-10 08:20:28 +00:00
parent 0087082cd5
commit 19cf1b95b5
4 changed files with 103 additions and 8 deletions

View File

@ -4,7 +4,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.mysema.codegen</groupId>
<artifactId>codegen</artifactId>
<version>0.3.8-SNAPSHOT</version>
<version>0.3.9</version>
<name>Codegen</name>
<description>Code generation and compilation for Java</description>
<parent>

View File

@ -21,6 +21,7 @@ import org.apache.commons.lang.StringEscapeUtils;
import com.mysema.codegen.model.Parameter;
import com.mysema.codegen.model.Type;
import com.mysema.codegen.model.Types;
import com.mysema.codegen.support.ScalaSyntaxUtils;
/**
* @author tiwe
@ -113,7 +114,7 @@ public class ScalaWriter extends AbstractCodeWriter<ScalaWriter>{
}else{
append("(");
}
append(method.getName()+"=");
append(escape(method.getName())+"=");
annotationConstant(value);
} catch (IllegalArgumentException e) {
throw new CodegenException(e);
@ -269,9 +270,9 @@ public class ScalaWriter extends AbstractCodeWriter<ScalaWriter>{
private ScalaWriter beginMethod(String modifiers, Type returnType, String methodName, Parameter... args) throws IOException{
if (returnType.equals(Types.VOID)){
beginLine(modifiers + methodName).params(args).append(" {").nl();
beginLine(modifiers + escape(methodName)).params(args).append(" {").nl();
}else{
beginLine(modifiers + methodName).params(args).append(": " + getGenericName(true, returnType)).append(" {").nl();
beginLine(modifiers + escape(methodName)).params(args).append(": " + getGenericName(true, returnType)).append(" {").nl();
}
return goIn();
@ -304,17 +305,17 @@ public class ScalaWriter extends AbstractCodeWriter<ScalaWriter>{
}
public ScalaWriter field(Type type, String name) throws IOException {
line(VAR + name + ": " + getGenericName(true, type) + SEMICOLON);
line(VAR + escape(name) + ": " + getGenericName(true, type) + SEMICOLON);
return compact ? this : nl();
}
private ScalaWriter field(String modifier, Type type, String name) throws IOException{
line(modifier + name + ": " + getGenericName(true, type) + SEMICOLON);
line(modifier + escape(name) + ": " + getGenericName(true, type) + SEMICOLON);
return compact ? this : nl();
}
private ScalaWriter field(String modifier, Type type, String name, String value) throws IOException{
line(modifier + name + ": " + getGenericName(true, type) + ASSIGN + value + SEMICOLON);
line(modifier + escape(name) + ": " + getGenericName(true, type) + ASSIGN + value + SEMICOLON);
return compact ? this : nl();
}
@ -451,7 +452,7 @@ public class ScalaWriter extends AbstractCodeWriter<ScalaWriter>{
private ScalaWriter param(Parameter parameter) throws IOException{
append(parameter.getName());
append(escape(parameter.getName()));
append(": ");
append(getGenericName(true, parameter.getType()));
return this;
@ -539,4 +540,11 @@ public class ScalaWriter extends AbstractCodeWriter<ScalaWriter>{
return rv;
}
private String escape(String token) {
if (ScalaSyntaxUtils.isReserved(token)) {
return "`" + token + "`";
} else {
return token;
}
}
}

View File

@ -0,0 +1,66 @@
package com.mysema.codegen.support;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
public final class ScalaSyntaxUtils {
private ScalaSyntaxUtils(){}
private static final Set<String> reserved = new HashSet<String>(Arrays.asList(
"abstract",
"do",
"finally",
"import",
"object",
"return",
"trait",
"var",
"_",
":",
"case",
"else",
"for",
"lazy",
"override",
"sealed",
"try",
"while",
"=",
"=>",
"<-",
"catch",
"extends",
"forSome",
"match",
"package",
"super",
"true",
"with",
"<:",
"class",
"false",
"if",
"new",
"private",
"this",
"type",
"yield",
"<%",
">:",
"def",
"final",
"implicit",
"null",
"protected",
"throw",
"val",
"#",
"@"
));
public static boolean isReserved(String token) {
return reserved.contains(token);
}
}

View File

@ -347,4 +347,25 @@ public class ScalaWriterTest {
assertTrue(w.toString().contains("public JavaWriterTest(a: Int) {"));
}
@Test
public void ReservedWords() throws IOException {
writer.beginClass(testType);
writer.beginConstructor(new Parameter("type", Types.INT));
writer.end();
writer.publicField(testType, "class");
writer.beginPublicMethod(testType, "var");
writer.end();
writer.end();
System.out.println(w);
assertTrue(w.toString().contains("`type`: Int"));
assertTrue(w.toString().contains("`class`: JavaWriterTest"));
assertTrue(w.toString().contains("`var`(): JavaWriterTest"));
}
}