Search in sources :

Example 1 with ClassType

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;##");
}
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 2 with ClassType

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());
    }
}
Also used : SimpleType(com.querydsl.codegen.utils.model.SimpleType) Type(com.querydsl.codegen.utils.model.Type) ClassType(com.querydsl.codegen.utils.model.ClassType) ClassType(com.querydsl.codegen.utils.model.ClassType) ForeignKeyData(com.querydsl.sql.codegen.support.ForeignKeyData) PrimaryKeyData(com.querydsl.sql.codegen.support.PrimaryKeyData) KeyData(com.querydsl.sql.codegen.support.KeyData) InverseForeignKeyData(com.querydsl.sql.codegen.support.InverseForeignKeyData)

Example 3 with ClassType

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()));
}
Also used : SimpleType(com.querydsl.codegen.utils.model.SimpleType) Type(com.querydsl.codegen.utils.model.Type) ClassType(com.querydsl.codegen.utils.model.ClassType) TypeCategory(com.querydsl.codegen.utils.model.TypeCategory) ClassType(com.querydsl.codegen.utils.model.ClassType) Annotation(java.lang.annotation.Annotation)

Example 4 with ClassType

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 {"));
}
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 ClassType

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));
    }
}
Also used : SimpleType(com.querydsl.codegen.utils.model.SimpleType) Type(com.querydsl.codegen.utils.model.Type) ClassType(com.querydsl.codegen.utils.model.ClassType) SimpleType(com.querydsl.codegen.utils.model.SimpleType) JavaWriter(com.querydsl.codegen.utils.JavaWriter) Matchers.containsString(org.hamcrest.Matchers.containsString) ClassType(com.querydsl.codegen.utils.model.ClassType) Test(org.junit.Test)

Aggregations

ClassType (com.querydsl.codegen.utils.model.ClassType)35 Test (org.junit.Test)24 SimpleType (com.querydsl.codegen.utils.model.SimpleType)18 Type (com.querydsl.codegen.utils.model.Type)14 List (java.util.List)7 JavaWriter (com.querydsl.codegen.utils.JavaWriter)6 StringWriter (java.io.StringWriter)6 Parameter (com.querydsl.codegen.utils.model.Parameter)4 TypeCategory (com.querydsl.codegen.utils.model.TypeCategory)3 PrimaryKeyData (com.querydsl.sql.codegen.support.PrimaryKeyData)3 ArrayList (java.util.ArrayList)3 Matchers.containsString (org.hamcrest.Matchers.containsString)3 Property (com.querydsl.codegen.Property)2 Cat (com.querydsl.codegen.utils.support.Cat)2 JoinType (com.querydsl.core.JoinType)2 ColumnImpl (com.querydsl.sql.ColumnImpl)2 Annotation (java.lang.annotation.Annotation)2 Before (org.junit.Before)2 EntityType (com.querydsl.codegen.EntityType)1 ScalaWriter (com.querydsl.codegen.utils.ScalaWriter)1