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());
}
}
}
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;##");
}
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);
}
}
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 {"));
}
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"));
}
Aggregations