Search in sources :

Example 1 with JavaWriter

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

the class AbstractQuerydslProcessor method serialize.

private void serialize(Serializer serializer, Collection<EntityType> models) {
    for (EntityType model : models) {
        try {
            Type type = conf.getTypeMappings().getPathType(model, model, true);
            String packageName = type.getPackageName();
            String className = !packageName.isEmpty() ? (packageName + "." + type.getSimpleName()) : type.getSimpleName();
            // skip if type is excluded class or in excluded package
            if (conf.isExcludedPackage(model.getPackageName()) || conf.isExcludedClass(model.getFullName())) {
                continue;
            }
            Set<TypeElement> elements = context.typeElements.get(model.getFullName());
            if (elements == null) {
                elements = new HashSet<TypeElement>();
            }
            for (Property property : model.getProperties()) {
                if (property.getType().getCategory() == TypeCategory.CUSTOM) {
                    Set<TypeElement> customElements = context.typeElements.get(property.getType().getFullName());
                    if (customElements != null) {
                        elements.addAll(customElements);
                    }
                }
            }
            processingEnv.getMessager().printMessage(Kind.NOTE, "Generating " + className + " for " + elements);
            JavaFileObject fileObject = processingEnv.getFiler().createSourceFile(className, elements.toArray(new Element[elements.size()]));
            Writer writer = fileObject.openWriter();
            try {
                SerializerConfig serializerConfig = conf.getSerializerConfig(model);
                serializer.serialize(model, serializerConfig, new JavaWriter(writer));
            } finally {
                if (writer != null) {
                    writer.close();
                }
            }
        } catch (IOException e) {
            System.err.println(e.getMessage());
            processingEnv.getMessager().printMessage(Kind.ERROR, e.getMessage());
        }
    }
}
Also used : JavaWriter(com.mysema.codegen.JavaWriter) IOException(java.io.IOException) JavaFileObject(javax.tools.JavaFileObject) NoType(javax.lang.model.type.NoType) Type(com.mysema.codegen.model.Type) DeclaredType(javax.lang.model.type.DeclaredType) Writer(java.io.Writer) JavaWriter(com.mysema.codegen.JavaWriter)

Example 2 with JavaWriter

use of com.mysema.codegen.JavaWriter 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 3 with JavaWriter

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

the class BeanSerializerTest method annotations.

@Test
public void annotations() throws IOException {
    type.addAnnotation(new QueryEntityImpl());
    BeanSerializer serializer = new BeanSerializer();
    serializer.serialize(type, SimpleSerializerConfig.DEFAULT, new JavaWriter(writer));
    String str = writer.toString();
    assertTrue(str.contains("import com.querydsl.core.annotations.QueryEntity;"));
    assertTrue(str.contains("@QueryEntity"));
}
Also used : JavaWriter(com.mysema.codegen.JavaWriter) Test(org.junit.Test)

Example 4 with JavaWriter

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

the class BeanSerializerTest method interfaces.

@Test
public void interfaces() throws IOException {
    BeanSerializer serializer = new BeanSerializer();
    serializer.addInterface(new ClassType(Serializable.class));
    serializer.serialize(type, SimpleSerializerConfig.DEFAULT, new JavaWriter(writer));
    assertTrue(writer.toString().contains("public class DomainClass implements Serializable {"));
}
Also used : Serializable(java.io.Serializable) JavaWriter(com.mysema.codegen.JavaWriter) Test(org.junit.Test)

Example 5 with JavaWriter

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

the class BeanSerializerTest method properties.

@Test
public void properties() throws IOException {
    // property
    type.addProperty(new Property(type, "entityField", type));
    type.addProperty(new Property(type, "collection", new SimpleType(Types.COLLECTION, typeModel)));
    type.addProperty(new Property(type, "listField", new SimpleType(Types.LIST, typeModel)));
    type.addProperty(new Property(type, "setField", new SimpleType(Types.SET, typeModel)));
    type.addProperty(new Property(type, "arrayField", new ClassType(TypeCategory.ARRAY, String[].class)));
    type.addProperty(new Property(type, "mapField", new SimpleType(Types.MAP, typeModel, typeModel)));
    for (Class<?> cl : Arrays.<Class<?>>asList(Boolean.class, Comparable.class, Integer.class, Date.class, java.sql.Date.class, java.sql.Time.class)) {
        Type classType = new ClassType(TypeCategory.get(cl.getName()), cl);
        type.addProperty(new Property(type, StringUtils.uncapitalize(cl.getSimpleName()), classType));
    }
    BeanSerializer serializer = new BeanSerializer();
    serializer.serialize(type, SimpleSerializerConfig.DEFAULT, new JavaWriter(writer));
    String str = writer.toString();
    //System.err.println(str);
    for (String prop : Arrays.asList("String[] arrayField;", "Boolean boolean$;", "Collection<DomainClass> collection;", "Comparable comparable;", "java.util.Date date;", "DomainClass entityField;", "Integer integer;", "List<DomainClass> listField;", "Map<DomainClass, DomainClass> mapField;", "Set<DomainClass> setField;", "java.sql.Time time;")) {
        assertTrue(prop + " was not contained", str.contains(prop));
    }
}
Also used : JavaWriter(com.mysema.codegen.JavaWriter) Test(org.junit.Test)

Aggregations

JavaWriter (com.mysema.codegen.JavaWriter)36 Test (org.junit.Test)32 CodeWriter (com.mysema.codegen.CodeWriter)3 StringWriter (java.io.StringWriter)3 ScalaWriter (com.mysema.codegen.ScalaWriter)2 SimpleType (com.mysema.codegen.model.SimpleType)2 Writer (java.io.Writer)2 EnumMap (java.util.EnumMap)2 Map (java.util.Map)2 ClassType (com.mysema.codegen.model.ClassType)1 Type (com.mysema.codegen.model.Type)1 IOException (java.io.IOException)1 Serializable (java.io.Serializable)1 DeclaredType (javax.lang.model.type.DeclaredType)1 NoType (javax.lang.model.type.NoType)1 JavaFileObject (javax.tools.JavaFileObject)1