Search in sources :

Example 1 with JavaWriter

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

the class AbstractQuerydslProcessor method serialize.

private void serialize(Serializer serializer, Collection<EntityType> models) {
    for (EntityType model : models) {
        try {
            String className = getClassName(model);
            // 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);
                    }
                }
            }
            logInfo("Generating " + className + " for " + elements);
            try (Writer writer = conf.getFiler().createFile(processingEnv, className, elements)) {
                SerializerConfig serializerConfig = conf.getSerializerConfig(model);
                serializer.serialize(model, serializerConfig, new JavaWriter(writer));
            }
        } catch (IOException e) {
            System.err.println(e.getMessage());
            processingEnv.getMessager().printMessage(Kind.ERROR, e.getMessage());
        }
    }
}
Also used : JavaWriter(com.querydsl.codegen.utils.JavaWriter) IOException(java.io.IOException) Writer(java.io.Writer) JavaWriter(com.querydsl.codegen.utils.JavaWriter)

Example 2 with JavaWriter

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

the class ExtendedBeanSerializerTest method equals_hashcode_tostring.

@Test
public void equals_hashcode_tostring() throws Exception {
    Property idCol = new Property(type, "id", new ClassType(Integer.class));
    idCol.addAnnotation(new ColumnImpl("ID"));
    Property subIdCol = new Property(type, "sub_id", new ClassType(String.class));
    subIdCol.addAnnotation(new ColumnImpl("SUB_ID"));
    Property nameCol = new Property(type, "name", new ClassType(String.class));
    nameCol.addAnnotation(new ColumnImpl("NAME"));
    type.addProperty(idCol);
    type.addProperty(subIdCol);
    type.addProperty(nameCol);
    type.getData().put(PrimaryKeyData.class, Arrays.asList(new PrimaryKeyData("PK", new String[] { "ID", "SUB_ID" })));
    ExtendedBeanSerializer extendedBeanSerializer = new ExtendedBeanSerializer();
    extendedBeanSerializer.setAddToString(true);
    FileWriter fw = new FileWriter(srcFile);
    extendedBeanSerializer.serialize(type, SimpleSerializerConfig.DEFAULT, new JavaWriter(fw));
    fw.close();
    URLClassLoader classLoader = URLClassLoader.newInstance(new URL[] { compileFolder.getRoot().toURI().toURL() });
    int retCode = new SimpleCompiler().run(null, System.out, System.err, srcFile.getAbsolutePath());
    assertEquals("The generated source should compile", 0, retCode);
    Class<?> cls = Class.forName(FULL_NAME, true, classLoader);
    ReflectionHelper reflection = new ReflectionHelper(cls);
    Object obj1 = cls.newInstance();
    Object obj1a = cls.newInstance();
    Object obj2 = cls.newInstance();
    reflection.setValues(obj1, 1, "##", "X");
    reflection.setValues(obj1a, 1, "##", null);
    reflection.setValues(obj2, 2, "--", "Y");
    assertEquals(obj1, obj1a);
    assertEquals(obj1.hashCode(), obj1a.hashCode());
    assertNotEquals(obj1, obj2);
    assertEquals(obj1.toString(), "DomainClass#1;##");
}
Also used : SimpleCompiler(com.querydsl.codegen.utils.SimpleCompiler) FileWriter(java.io.FileWriter) JavaWriter(com.querydsl.codegen.utils.JavaWriter) ClassType(com.querydsl.codegen.utils.model.ClassType) PrimaryKeyData(com.querydsl.sql.codegen.support.PrimaryKeyData) URLClassLoader(java.net.URLClassLoader) ColumnImpl(com.querydsl.sql.ColumnImpl) Property(com.querydsl.codegen.Property) Test(org.junit.Test)

Example 3 with JavaWriter

use of com.querydsl.codegen.utils.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);
    try (Writer w = writerFor(targetFile)) {
        CodeWriter writer = createScalaSources ? new ScalaWriter(w) : new JavaWriter(w);
        serializer.serialize(type, serializerConfig, writer);
    }
}
Also used : JavaWriter(com.querydsl.codegen.utils.JavaWriter) ScalaWriter(com.querydsl.codegen.utils.ScalaWriter) File(java.io.File) CodeWriter(com.querydsl.codegen.utils.CodeWriter) ScalaWriter(com.querydsl.codegen.utils.ScalaWriter) OutputStreamWriter(java.io.OutputStreamWriter) CodeWriter(com.querydsl.codegen.utils.CodeWriter) Writer(java.io.Writer) JavaWriter(com.querydsl.codegen.utils.JavaWriter)

Example 4 with JavaWriter

use of com.querydsl.codegen.utils.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.querydsl.codegen.utils.JavaWriter) ClassType(com.querydsl.codegen.utils.model.ClassType) Test(org.junit.Test)

Example 5 with JavaWriter

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

the class BeanSerializerTest method defaultsGeneratedAnnotation.

@Test
public void defaultsGeneratedAnnotation() throws IOException {
    Serializer serializer = new BeanSerializer();
    serializer.serialize(type, SimpleSerializerConfig.DEFAULT, new JavaWriter(writer));
    String generatedSource = String.valueOf(writer);
    assertThat(generatedSource, containsString(String.format("import %s;", GeneratedAnnotationResolver.resolveDefault().getName())));
    assertThat(generatedSource, containsString("@Generated(\"com.querydsl.codegen.BeanSerializer\")\npublic class"));
}
Also used : JavaWriter(com.querydsl.codegen.utils.JavaWriter) Matchers.containsString(org.hamcrest.Matchers.containsString) Test(org.junit.Test)

Aggregations

JavaWriter (com.querydsl.codegen.utils.JavaWriter)45 Test (org.junit.Test)41 Matchers.containsString (org.hamcrest.Matchers.containsString)13 SimpleType (com.querydsl.codegen.utils.model.SimpleType)8 ClassType (com.querydsl.codegen.utils.model.ClassType)6 Writer (java.io.Writer)6 StringWriter (java.io.StringWriter)5 Type (com.querydsl.codegen.utils.model.Type)4 CodeWriter (com.querydsl.codegen.utils.CodeWriter)3 ScalaWriter (com.querydsl.codegen.utils.ScalaWriter)2 File (java.io.File)2 OutputStreamWriter (java.io.OutputStreamWriter)2 EnumMap (java.util.EnumMap)2 Map (java.util.Map)2 Property (com.querydsl.codegen.Property)1 SimpleCompiler (com.querydsl.codegen.utils.SimpleCompiler)1 Constructor (com.querydsl.codegen.utils.model.Constructor)1 Parameter (com.querydsl.codegen.utils.model.Parameter)1 ColumnImpl (com.querydsl.sql.ColumnImpl)1 PrimaryKeyData (com.querydsl.sql.codegen.support.PrimaryKeyData)1