use of com.querydsl.codegen.utils.model.ClassType 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.model.ClassType in project querydsl by querydsl.
the class MetaDataSerializer method serializeForeignKeys.
protected void serializeForeignKeys(EntityType model, CodeWriter writer, Collection<? extends KeyData> foreignKeys, boolean inverse) throws IOException {
for (KeyData foreignKey : foreignKeys) {
String fieldName;
if (inverse) {
fieldName = namingStrategy.getPropertyNameForInverseForeignKey(foreignKey.getName(), model);
} else {
fieldName = namingStrategy.getPropertyNameForForeignKey(foreignKey.getName(), model);
}
StringBuilder value = new StringBuilder();
if (inverse) {
value.append("createInvForeignKey(");
} else {
value.append("createForeignKey(");
}
if (foreignKey.getForeignColumns().size() == 1) {
value.append(namingStrategy.getPropertyName(foreignKey.getForeignColumns().get(0), model));
value.append(", \"").append(foreignKey.getParentColumns().get(0)).append("\"");
} else {
StringBuilder local = new StringBuilder();
StringBuilder foreign = new StringBuilder();
for (int i = 0; i < foreignKey.getForeignColumns().size(); i++) {
if (i > 0) {
local.append(", ");
foreign.append(", ");
}
local.append(namingStrategy.getPropertyName(foreignKey.getForeignColumns().get(i), model));
foreign.append("\"").append(foreignKey.getParentColumns().get(i)).append("\"");
}
value.append("Arrays.asList(").append(local).append("), Arrays.asList(").append(foreign).append(")");
}
value.append(")");
Type type = new ClassType(ForeignKey.class, foreignKey.getType());
writer.publicFinal(type, fieldName, value.toString());
}
}
use of com.querydsl.codegen.utils.model.ClassType in project querydsl by querydsl.
the class MetaDataSerializer method introClassHeader.
@Override
protected void introClassHeader(CodeWriter writer, EntityType model) throws IOException {
Type queryType = typeMappings.getPathType(model, model, true);
writer.line("@", generatedAnnotationClass.getSimpleName(), "(\"", getClass().getName(), "\")");
TypeCategory category = model.getOriginalCategory();
// serialize annotations only, if no bean types are used
if (model.equals(queryType)) {
for (Annotation annotation : model.getAnnotations()) {
writer.annotation(annotation);
}
}
writer.beginClass(queryType, new ClassType(category, entityPathType, model));
writer.privateStaticFinal(Types.LONG_P, "serialVersionUID", String.valueOf(model.hashCode()));
}
use of com.querydsl.codegen.utils.model.ClassType 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.model.ClassType 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));
}
}
Aggregations