Search in sources :

Example 16 with JavaWriter

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

the class EntitySerializerTest method primitive_array.

@Test
public void primitive_array() throws IOException {
    SimpleType type = new SimpleType(TypeCategory.ENTITY, "Entity", "", "Entity", false, false);
    EntityType entityType = new EntityType(type);
    entityType.addProperty(new Property(entityType, "bytes", new ClassType(byte[].class)));
    typeMappings.register(entityType, queryTypeFactory.create(entityType));
    serializer.serialize(entityType, SimpleSerializerConfig.DEFAULT, new JavaWriter(writer));
    assertTrue(writer.toString().contains("public final SimplePath<byte[]> bytes"));
    CompileUtils.assertCompiles("QEntity", writer.toString());
}
Also used : JavaWriter(com.mysema.codegen.JavaWriter) Test(org.junit.Test)

Example 17 with JavaWriter

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

the class EntitySerializerTest method delegates.

@Test
public void delegates() throws IOException {
    SimpleType type = new SimpleType(TypeCategory.ENTITY, "Entity", "", "Entity", false, false);
    EntityType entityType = new EntityType(type);
    Delegate delegate = new Delegate(type, type, "test", Collections.<Parameter>emptyList(), Types.STRING);
    entityType.addDelegate(delegate);
    typeMappings.register(entityType, queryTypeFactory.create(entityType));
    serializer.serialize(entityType, SimpleSerializerConfig.DEFAULT, new JavaWriter(writer));
    assertTrue(writer.toString().contains("return Entity.test(this);"));
    CompileUtils.assertCompiles("QEntity", writer.toString());
}
Also used : JavaWriter(com.mysema.codegen.JavaWriter) Test(org.junit.Test)

Example 18 with JavaWriter

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

the class EntitySerializerTest method original_category.

@Test
public void original_category() throws IOException {
    Map<TypeCategory, String> categoryToSuperClass = new EnumMap<TypeCategory, String>(TypeCategory.class);
    categoryToSuperClass.put(TypeCategory.COMPARABLE, "ComparablePath<Entity>");
    categoryToSuperClass.put(TypeCategory.ENUM, "EnumPath<Entity>");
    categoryToSuperClass.put(TypeCategory.DATE, "DatePath<Entity>");
    categoryToSuperClass.put(TypeCategory.DATETIME, "DateTimePath<Entity>");
    categoryToSuperClass.put(TypeCategory.TIME, "TimePath<Entity>");
    categoryToSuperClass.put(TypeCategory.NUMERIC, "NumberPath<Entity>");
    categoryToSuperClass.put(TypeCategory.STRING, "StringPath");
    categoryToSuperClass.put(TypeCategory.BOOLEAN, "BooleanPath");
    for (Map.Entry<TypeCategory, String> entry : categoryToSuperClass.entrySet()) {
        SimpleType type = new SimpleType(entry.getKey(), "Entity", "", "Entity", false, false);
        EntityType entityType = new EntityType(type);
        typeMappings.register(entityType, queryTypeFactory.create(entityType));
        serializer.serialize(entityType, SimpleSerializerConfig.DEFAULT, new JavaWriter(writer));
        assertTrue(entry.toString(), writer.toString().contains("public class QEntity extends " + entry.getValue() + " {"));
    }
}
Also used : JavaWriter(com.mysema.codegen.JavaWriter) EnumMap(java.util.EnumMap) EnumMap(java.util.EnumMap) Map(java.util.Map) Test(org.junit.Test)

Example 19 with JavaWriter

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

the class EntitySerializerTest method different_package.

@Test
public void different_package() throws IOException {
    queryTypeFactory = new QueryTypeFactoryImpl("Q", "", ".gen");
    EntityType entityType = new EntityType(new ClassType(Entity.class));
    typeMappings.register(entityType, queryTypeFactory.create(entityType));
    serializer.serialize(entityType, SimpleSerializerConfig.DEFAULT, new JavaWriter(writer));
    assertTrue(writer.toString().contains("public class QEntitySerializerTest_Entity " + "extends EntityPathBase<EntitySerializerTest.Entity>"));
    CompileUtils.assertCompiles("QEntitySerializerTest_Entity", writer.toString());
}
Also used : JavaWriter(com.mysema.codegen.JavaWriter) Test(org.junit.Test)

Example 20 with JavaWriter

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

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