Search in sources :

Example 11 with ClassType

use of com.querydsl.codegen.utils.model.ClassType in project querydsl by querydsl.

the class JavaWriterTest method setUp.

@Before
public void setUp() {
    w = new StringWriter();
    writer = new JavaWriter(w);
    testType = new ClassType(JavaWriterTest.class);
    testType2 = new SimpleType("com.querydsl.codegen.utils.Test", "com.querydsl.codegen.utils", "Test");
    testSuperType = new SimpleType("com.querydsl.codegen.utils.Superclass", "com.querydsl.codegen.utils", "Superclass");
    testInterface1 = new SimpleType("com.querydsl.codegen.utils.TestInterface1", "com.querydsl.codegen.utils", "TestInterface1");
    testInterface2 = new SimpleType("com.querydsl.codegen.utils.TestInterface2", "com.querydsl.codegen.utils", "TestInterface2");
}
Also used : SimpleType(com.querydsl.codegen.utils.model.SimpleType) StringWriter(java.io.StringWriter) ClassType(com.querydsl.codegen.utils.model.ClassType) Before(org.junit.Before)

Example 12 with ClassType

use of com.querydsl.codegen.utils.model.ClassType in project querydsl by querydsl.

the class JavaWriterTest method Arrays.

@Test
public void Arrays() throws IOException {
    writer.beginClass(new SimpleType("Main"));
    writer.field(Types.STRING.asArrayType(), "stringArray");
    writer.beginPublicMethod(Types.VOID, "main", new Parameter("args", Types.STRING.asArrayType()));
    writer.line("//");
    writer.end();
    writer.beginPublicMethod(Types.VOID, "main2", new Parameter("args", new ClassType(TypeCategory.ARRAY, String[].class)));
    writer.line("//");
    writer.end();
    writer.end();
    System.out.println(w);
    assertTrue(w.toString().contains("String[] stringArray;"));
    assertTrue(w.toString().contains("public void main(String[] args) {"));
    assertTrue(w.toString().contains("public void main2(String[] args) {"));
}
Also used : SimpleType(com.querydsl.codegen.utils.model.SimpleType) Parameter(com.querydsl.codegen.utils.model.Parameter) ClassType(com.querydsl.codegen.utils.model.ClassType) Test(org.junit.Test)

Example 13 with ClassType

use of com.querydsl.codegen.utils.model.ClassType in project querydsl by querydsl.

the class MetaDataExporter method handleColumn.

private void handleColumn(EntityType classModel, String tableName, ResultSet columns) throws SQLException {
    String columnName = normalize(columns.getString("COLUMN_NAME"));
    String normalizedColumnName = namingStrategy.normalizeColumnName(columnName);
    int columnType = columns.getInt("DATA_TYPE");
    String typeName = columns.getString("TYPE_NAME");
    Number columnSize = (Number) columns.getObject("COLUMN_SIZE");
    Number columnDigits = (Number) columns.getObject("DECIMAL_DIGITS");
    int columnIndex = columns.getInt("ORDINAL_POSITION");
    int nullable = columns.getInt("NULLABLE");
    String columnDefaultValue = columns.getString("COLUMN_DEF");
    String propertyName = namingStrategy.getPropertyName(normalizedColumnName, classModel);
    Class<?> clazz = configuration.getJavaType(columnType, typeName, columnSize != null ? columnSize.intValue() : 0, columnDigits != null ? columnDigits.intValue() : 0, tableName, columnName);
    if (clazz == null) {
        clazz = Object.class;
    }
    TypeCategory fieldType = TypeCategory.get(clazz.getName());
    if (Number.class.isAssignableFrom(clazz)) {
        fieldType = TypeCategory.NUMERIC;
    } else if (Enum.class.isAssignableFrom(clazz)) {
        fieldType = TypeCategory.ENUM;
    }
    Type typeModel = new ClassType(fieldType, clazz);
    Property property = createProperty(classModel, normalizedColumnName, propertyName, typeModel);
    ColumnMetadata column = ColumnMetadata.named(normalizedColumnName).ofType(columnType).withIndex(columnIndex);
    if (nullable == DatabaseMetaData.columnNoNulls) {
        column = column.notNull();
    }
    if (columnSize != null) {
        column = column.withSize(columnSize.intValue());
    }
    if (columnDigits != null) {
        column = column.withDigits(columnDigits.intValue());
    }
    property.getData().put("COLUMN", column);
    if (columnAnnotations) {
        property.addAnnotation(new ColumnImpl(normalizedColumnName));
    }
    if (validationAnnotations) {
        if (nullable == DatabaseMetaData.columnNoNulls && columnDefaultValue == null) {
            property.addAnnotation(new NotNullImpl());
        }
        int size = columns.getInt("COLUMN_SIZE");
        if (size > 0 && clazz.equals(String.class)) {
            property.addAnnotation(new SizeImpl(0, size));
        }
    }
    classModel.addProperty(property);
}
Also used : ColumnMetadata(com.querydsl.sql.ColumnMetadata) NotNullImpl(com.querydsl.sql.codegen.support.NotNullImpl) ClassType(com.querydsl.codegen.utils.model.ClassType) SizeImpl(com.querydsl.sql.codegen.support.SizeImpl) EntityType(com.querydsl.codegen.EntityType) 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) ColumnImpl(com.querydsl.sql.ColumnImpl) Property(com.querydsl.codegen.Property)

Example 14 with ClassType

use of com.querydsl.codegen.utils.model.ClassType in project querydsl by querydsl.

the class AbstractEvaluatorFactory method createSource.

/**
 * @param source
 * @param projectionType
 * @param names
 * @param types
 * @param id
 * @param constants
 * @return
 * @throws IOException
 */
protected String createSource(String source, ClassType projectionType, String[] names, Type[] types, String id, Map<String, Object> constants) throws IOException {
    // create source
    StringWriter writer = new StringWriter();
    JavaWriter javaw = new JavaWriter(writer);
    SimpleType idType = new SimpleType(id, "", id);
    javaw.beginClass(idType, null);
    Parameter[] params = new Parameter[names.length + constants.size()];
    for (int i = 0; i < names.length; i++) {
        params[i] = new Parameter(names[i], types[i]);
    }
    int i = names.length;
    for (Map.Entry<String, Object> entry : constants.entrySet()) {
        Type type = new ClassType(TypeCategory.SIMPLE, ClassUtils.normalize(entry.getValue().getClass()));
        params[i++] = new Parameter(entry.getKey(), type);
    }
    javaw.beginStaticMethod(projectionType, "eval", params);
    javaw.append(source);
    javaw.end();
    javaw.end();
    return writer.toString();
}
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) StringWriter(java.io.StringWriter) Parameter(com.querydsl.codegen.utils.model.Parameter) ClassType(com.querydsl.codegen.utils.model.ClassType) Map(java.util.Map) WeakHashMap(java.util.WeakHashMap)

Example 15 with ClassType

use of com.querydsl.codegen.utils.model.ClassType in project querydsl by querydsl.

the class ComplexEvaluationTest method ComplexClassLoadingFailure.

@Test(expected = CodegenException.class)
@SuppressWarnings("unchecked")
public void ComplexClassLoadingFailure() {
    ClassType resultType = new ClassType(TypeCategory.LIST, List.class, Types.STRING);
    StringBuilder source = new StringBuilder();
    source.append("java.util.List<String> rv = (java.util.List<String>) new java.util.ArrayList<Franklin>();\n");
    source.append("for (String a : a_){\n");
    source.append("    for (String b : b_){\n");
    source.append("        if (a.equals(b)){\n");
    source.append("            rv.add(a);\n");
    source.append("        }\n");
    source.append("    }\n");
    source.append("}\n");
    source.append("return rv;");
    // cannot specify further than List.class
    @SuppressWarnings("rawtypes") Evaluator<List> evaluator = factory.createEvaluator(source.toString(), resultType, new String[] { "a_", "b_" }, new Type[] { resultType, resultType }, new Class<?>[] { List.class, List.class }, Collections.<String, Object>emptyMap());
    List<String> a = Arrays.asList("1", "2", "3", "4");
    List<String> b = Arrays.asList("2", "4", "6", "8");
    assertEquals(Arrays.asList("2", "4"), evaluator.evaluate(a, b));
}
Also used : List(java.util.List) 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