Add CodeWriter.getClassConstant() to abstract the difference between A.class in Java and classOf[A] in Scala. To be used in querydsl-codegen shortly.

This commit is contained in:
jtsay362 2013-10-20 10:38:32 -07:00
parent 7493e1af30
commit 8ff357f6d0
5 changed files with 24 additions and 3 deletions

View File

@ -33,6 +33,8 @@ public interface CodeWriter extends Appendable {
String getGenericName(boolean asArgType, Type type);
String getClassConstant(String className);
CodeWriter annotation(Annotation annotation) throws IOException;
CodeWriter annotation(Class<? extends Annotation> annotation) throws IOException;

View File

@ -303,6 +303,12 @@ public final class JavaWriter extends AbstractCodeWriter<JavaWriter> {
+ value + SEMICOLON).nl();
}
@Override
public String getClassConstant(String className) {
return className + ".class";
}
@Override
public String getGenericName(boolean asArgType, Type type) {
return type.getGenericName(asArgType, packages, classes);

View File

@ -352,6 +352,11 @@ public class ScalaWriter extends AbstractCodeWriter<ScalaWriter> {
return compact ? this : nl();
}
@Override
public String getClassConstant(String className) {
return "classOf[" + className + "]";
}
@Override
public String getGenericName(boolean asArgType, Type type) {
if (type.getParameters().isEmpty()) {

View File

@ -223,6 +223,11 @@ public class JavaWriterTest {
assertTrue(w.toString().contains("@Target({FIELD, METHOD})"));
}
@Test
public void ClassConstants() {
assertEquals("SomeClass.class", writer.getClassConstant("SomeClass"));
}
@Test
public void Fields() throws IOException {
writer.beginClass(testType);
@ -330,5 +335,4 @@ public class JavaWriterTest {
match("/testSuppressWarnings", w.toString());
}
}

View File

@ -366,6 +366,11 @@ public class ScalaWriterTest {
assertEquals("case class TestType(a: String, b: String)\n", w.toString());
}
@Test
public void ClassConstants() {
assertEquals("classOf[SomeClass]", writer.getClassConstant("SomeClass"));
}
@Test
public void Primitive() throws IOException {
writer.beginClass(testType);
@ -381,7 +386,7 @@ public class ScalaWriterTest {
}
@Test
public void Primive_Types() throws IOException {
public void Primitive_Types() throws IOException {
writer.field(Types.BOOLEAN_P, "field");
writer.field(Types.BYTE_P, "field");
writer.field(Types.CHAR, "field");
@ -417,5 +422,4 @@ public class ScalaWriterTest {
assertTrue(w.toString().contains("`class`: JavaWriterTest"));
assertTrue(w.toString().contains("`var`(): JavaWriterTest"));
}
}