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