Search in sources :

Example 1 with ScalaWriter

use of com.mysema.codegen.ScalaWriter in project querydsl by querydsl.

the class GenericExporter method write.

private void write(Serializer serializer, String path, SerializerConfig serializerConfig, EntityType type) throws IOException {
    File targetFile = new File(targetFolder, path);
    generatedFiles.add(targetFile);
    Writer w = writerFor(targetFile);
    try {
        CodeWriter writer = createScalaSources ? new ScalaWriter(w) : new JavaWriter(w);
        serializer.serialize(type, serializerConfig, writer);
    } finally {
        w.close();
    }
}
Also used : JavaWriter(com.mysema.codegen.JavaWriter) ScalaWriter(com.mysema.codegen.ScalaWriter) CodeWriter(com.mysema.codegen.CodeWriter) CodeWriter(com.mysema.codegen.CodeWriter) ScalaWriter(com.mysema.codegen.ScalaWriter) JavaWriter(com.mysema.codegen.JavaWriter)

Example 2 with ScalaWriter

use of com.mysema.codegen.ScalaWriter in project querydsl by querydsl.

the class ScalaTypeDump method test.

@Test
@Ignore
public void test() throws IOException {
    List<Class<?>> classes = new ArrayList<Class<?>>();
    classes.add(SimpleExpression.class);
    classes.add(ComparableExpression.class);
    classes.add(BooleanExpression.class);
    classes.add(StringExpression.class);
    classes.add(TemporalExpression.class);
    classes.add(TimeExpression.class);
    classes.add(DateTimeExpression.class);
    classes.add(DateExpression.class);
    classes.add(EnumExpression.class);
    classes.add(NumberExpression.class);
    StringWriter w = new StringWriter();
    ScalaWriter writer = new ScalaWriter(w);
    writer.packageDecl("com.querydsl.scala");
    writer.imports(Expression.class.getPackage());
    for (Class<?> cl : classes) {
        Type type = new ClassType(cl);
        Type superClass = new ClassType(cl.getSuperclass());
        writer.beginClass(type, superClass);
        for (Method m : cl.getDeclaredMethods()) {
            List<Parameter> params = new ArrayList<Parameter>();
            for (Class<?> paramType : m.getParameterTypes()) {
                params.add(new Parameter("arg" + params.size(), new ClassType(paramType)));
            }
            Type returnType = new ClassType(m.getReturnType());
            writer.beginPublicMethod(returnType, ":" + m.getName(), params.toArray(new Parameter[params.size()]));
            writer.end();
        }
        writer.end();
    }
    System.out.println(w);
}
Also used : ClassType(com.mysema.codegen.model.ClassType) Type(com.mysema.codegen.model.Type) StringWriter(java.io.StringWriter) Expression(com.querydsl.core.types.Expression) ArrayList(java.util.ArrayList) Parameter(com.mysema.codegen.model.Parameter) ScalaWriter(com.mysema.codegen.ScalaWriter) Method(java.lang.reflect.Method) ClassType(com.mysema.codegen.model.ClassType) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 3 with ScalaWriter

use of com.mysema.codegen.ScalaWriter in project querydsl by querydsl.

the class MetaDataExporter method write.

private void write(Serializer serializer, File targetFile, EntityType type) throws IOException {
    if (!classes.add(targetFile.getPath())) {
        throw new IllegalStateException("Attempted to write multiple times to " + targetFile.getPath() + ", please check your configuration");
    }
    StringWriter w = new StringWriter();
    CodeWriter writer = createScalaSources ? new ScalaWriter(w) : new JavaWriter(w);
    serializer.serialize(type, SimpleSerializerConfig.DEFAULT, writer);
    // conditional creation
    boolean generate = true;
    byte[] bytes = w.toString().getBytes(sourceEncoding);
    if (targetFile.exists() && targetFile.length() == bytes.length) {
        String str = Files.toString(targetFile, Charset.forName(sourceEncoding));
        if (str.equals(w.toString())) {
            generate = false;
        }
    } else {
        targetFile.getParentFile().mkdirs();
    }
    if (generate) {
        Files.write(bytes, targetFile);
    }
}
Also used : StringWriter(java.io.StringWriter) JavaWriter(com.mysema.codegen.JavaWriter) ScalaWriter(com.mysema.codegen.ScalaWriter) CodeWriter(com.mysema.codegen.CodeWriter)

Aggregations

ScalaWriter (com.mysema.codegen.ScalaWriter)3 CodeWriter (com.mysema.codegen.CodeWriter)2 JavaWriter (com.mysema.codegen.JavaWriter)2 StringWriter (java.io.StringWriter)2 ClassType (com.mysema.codegen.model.ClassType)1 Parameter (com.mysema.codegen.model.Parameter)1 Type (com.mysema.codegen.model.Type)1 Expression (com.querydsl.core.types.Expression)1 Method (java.lang.reflect.Method)1 ArrayList (java.util.ArrayList)1 Ignore (org.junit.Ignore)1 Test (org.junit.Test)1