Search in sources :

Example 41 with Type

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

the class ComplexEvaluationTest method ComplexDifferentConstants.

@Test
public void ComplexDifferentConstants() {
    ClassType resultType = new ClassType(TypeCategory.LIST, List.class, new ClassType(Cat.class));
    String source = new StringBuilder().append("java.util.List<com.querydsl.codegen.utils.support.Cat> rv = new java.util.ArrayList<com.querydsl.codegen.utils.support.Cat>();\n").append("for (com.querydsl.codegen.utils.support.Cat cat : (java.util.List<com.querydsl.codegen.utils.support.Cat>)cat_){\n").append("if (cat.equals(a1)) {\n").append("rv.add(cat);\n").append("}\n").append("}\n").append("return rv;\n").toString();
    List<Cat> cats = Arrays.asList(new Cat("fuzzy"), new Cat("spot"));
    String[] names = new String[] { "cat_" };
    Type[] types = new Type[] { new ClassType(TypeCategory.LIST, List.class, new ClassType(Cat.class)) };
    Class<?>[] classes = new Class<?>[] { List.class };
    // first pass
    factory.createEvaluator(source, resultType, names, types, classes, Collections.singletonMap("a1", (Object) new SuperCat("superMittens"))).evaluate(cats);
    // second pass
    factory.createEvaluator(source, resultType, names, types, classes, Collections.singletonMap("a1", (Object) new Cat("normalMittens"))).evaluate(cats);
}
Also used : Type(com.querydsl.codegen.utils.model.Type) ClassType(com.querydsl.codegen.utils.model.ClassType) Cat(com.querydsl.codegen.utils.support.Cat) List(java.util.List) ClassType(com.querydsl.codegen.utils.model.ClassType) Test(org.junit.Test)

Example 42 with Type

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

the class DefaultEvaluatorFactory method createEvaluator.

/**
 * Create an Evaluator for the given sources and the given optional filter
 *
 * @param metadata query metadata
 * @param joins joins
 * @param filter where condition
 * @return evaluator
 */
public Evaluator<List<Object[]>> createEvaluator(QueryMetadata metadata, List<JoinExpression> joins, @Nullable Predicate filter) {
    List<String> sourceNames = new ArrayList<String>();
    List<Type> sourceTypes = new ArrayList<Type>();
    List<Class<?>> sourceClasses = new ArrayList<Class<?>>();
    StringBuilder vars = new StringBuilder();
    CollQuerySerializer ser = new CollQuerySerializer(templates);
    ser.append("java.util.List<Object[]> rv = new java.util.ArrayList<Object[]>();\n");
    List<String> anyJoinMatchers = new ArrayList<String>();
    // creating context
    for (JoinExpression join : joins) {
        Expression<?> target = join.getTarget();
        String typeName = com.querydsl.codegen.utils.support.ClassUtils.getName(target.getType());
        if (vars.length() > 0) {
            vars.append(",");
        }
        switch(join.getType()) {
            case DEFAULT:
                ser.append("for (" + typeName + " " + target + " : " + target + "_) {\n");
                vars.append(target);
                sourceNames.add(target + "_");
                sourceTypes.add(new SimpleType(Types.ITERABLE, new ClassType(TypeCategory.SIMPLE, target.getType())));
                sourceClasses.add(Iterable.class);
                break;
            case INNERJOIN:
            case LEFTJOIN:
                Operation<?> alias = (Operation<?>) join.getTarget();
                boolean colAnyJoin = join.getCondition() != null && join.getCondition().toString().equals("any");
                boolean leftJoin = join.getType() == JoinType.LEFTJOIN;
                String matcher = null;
                if (colAnyJoin) {
                    matcher = alias.getArg(1).toString() + "_matched";
                    ser.append("boolean " + matcher + " = false;\n");
                    anyJoinMatchers.add(matcher);
                }
                ser.append("for (" + typeName + " " + alias.getArg(1) + " : ");
                if (leftJoin) {
                    ser.append(CollQueryFunctions.class.getName() + ".leftJoin(");
                }
                if (colAnyJoin) {
                    Context context = new Context();
                    Expression<?> replacement = alias.getArg(0).accept(collectionAnyVisitor, context);
                    ser.handle(replacement);
                } else {
                    ser.handle(alias.getArg(0));
                }
                if (alias.getArg(0).getType().equals(Map.class)) {
                    ser.append(".values()");
                }
                if (leftJoin) {
                    ser.append(")");
                }
                ser.append(") {\n");
                if (matcher != null) {
                    ser.append("if (!" + matcher + ") {\n");
                }
                vars.append(alias.getArg(1));
                break;
            default:
                throw new IllegalArgumentException("Illegal join expression " + join);
        }
    }
    // filter
    if (filter != null) {
        ser.append("try {\n");
        ser.append("if (");
        ser.handle(filter).append(") {\n");
        for (String matcher : anyJoinMatchers) {
            ser.append("    " + matcher + " = true;\n");
        }
        ser.append("    rv.add(new Object[]{" + vars + "});\n");
        ser.append("}\n");
        ser.append("} catch (NullPointerException npe) { }\n");
    } else {
        ser.append("rv.add(new Object[]{" + vars + "});\n");
    }
    // closing context
    int amount = joins.size() + anyJoinMatchers.size();
    for (int i = 0; i < amount; i++) {
        ser.append("}\n");
    }
    ser.append("return rv;");
    Map<Object, String> constantToLabel = ser.getConstantToLabel();
    Map<String, Object> constants = getConstants(metadata, constantToLabel);
    ClassType projectionType = new ClassType(TypeCategory.LIST, List.class, Types.OBJECTS);
    return factory.createEvaluator(ser.toString(), projectionType, sourceNames.toArray(new String[0]), sourceTypes.toArray(new Type[0]), sourceClasses.toArray(new Class<?>[0]), constants);
}
Also used : Context(com.querydsl.core.support.Context) ArrayList(java.util.ArrayList) Operation(com.querydsl.core.types.Operation) ClassType(com.querydsl.codegen.utils.model.ClassType) SimpleType(com.querydsl.codegen.utils.model.SimpleType) SimpleType(com.querydsl.codegen.utils.model.SimpleType) Type(com.querydsl.codegen.utils.model.Type) ClassType(com.querydsl.codegen.utils.model.ClassType) JoinType(com.querydsl.core.JoinType) JoinExpression(com.querydsl.core.JoinExpression)

Example 43 with Type

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

the class MetaDataSerializer method serializePrimaryKeys.

protected void serializePrimaryKeys(EntityType model, CodeWriter writer, Collection<PrimaryKeyData> primaryKeys) throws IOException {
    for (PrimaryKeyData primaryKey : primaryKeys) {
        String fieldName = namingStrategy.getPropertyNameForPrimaryKey(primaryKey.getName(), model);
        StringBuilder value = new StringBuilder("createPrimaryKey(");
        boolean first = true;
        for (String column : primaryKey.getColumns()) {
            if (!first) {
                value.append(", ");
            }
            value.append(namingStrategy.getPropertyName(column, model));
            first = false;
        }
        value.append(")");
        Type type = new ClassType(PrimaryKey.class, model);
        writer.publicFinal(type, fieldName, value.toString());
    }
}
Also used : PrimaryKeyData(com.querydsl.sql.codegen.support.PrimaryKeyData) 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)

Example 44 with Type

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

the class ExtendedBeanSerializerTest method setUp.

@Before
public void setUp() throws IOException {
    Type typeModel = new SimpleType(TypeCategory.ENTITY, FULL_NAME, PACKAGE, CLASS_NAME, false, false);
    type = new EntityType(typeModel);
    File srcFolder = compileFolder.newFolder(PATH);
    srcFile = new File(srcFolder, CLASS_NAME + ".java");
}
Also used : EntityType(com.querydsl.codegen.EntityType) SimpleType(com.querydsl.codegen.utils.model.SimpleType) 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) File(java.io.File) Before(org.junit.Before)

Example 45 with Type

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

the class TypeFactory method getGenericParameter.

@SuppressWarnings("rawtypes")
private Type getGenericParameter(Class<?> cl, java.lang.reflect.Type genericType, int i) {
    java.lang.reflect.Type parameter = ReflectionUtils.getTypeParameter(genericType, i);
    if (parameter instanceof TypeVariable) {
        TypeVariable variable = (TypeVariable) parameter;
        Type rv = get(ReflectionUtils.getTypeParameterAsClass(genericType, i), null, parameter);
        return new TypeExtends(variable.getName(), rv);
    } else if (parameter instanceof WildcardType && ((WildcardType) parameter).getUpperBounds()[0].equals(Object.class) && ((WildcardType) parameter).getLowerBounds().length == 0) {
        return ANY;
    } else {
        Type rv = get(ReflectionUtils.getTypeParameterAsClass(genericType, i), null, parameter);
        if (parameter instanceof WildcardType) {
            rv = new TypeExtends(rv);
        }
        return rv;
    }
}
Also used : TypeExtends(com.querydsl.codegen.utils.model.TypeExtends) Type(com.querydsl.codegen.utils.model.Type) ClassType(com.querydsl.codegen.utils.model.ClassType) WildcardType(java.lang.reflect.WildcardType) SimpleType(com.querydsl.codegen.utils.model.SimpleType) WildcardType(java.lang.reflect.WildcardType) TypeVariable(java.lang.reflect.TypeVariable)

Aggregations

Type (com.querydsl.codegen.utils.model.Type)56 SimpleType (com.querydsl.codegen.utils.model.SimpleType)35 ClassType (com.querydsl.codegen.utils.model.ClassType)23 EntityType (com.querydsl.codegen.EntityType)15 Test (org.junit.Test)14 PropertyType (com.querydsl.core.annotations.PropertyType)9 QueryType (com.querydsl.core.annotations.QueryType)9 DeclaredType (javax.lang.model.type.DeclaredType)9 NoType (javax.lang.model.type.NoType)9 Parameter (com.querydsl.codegen.utils.model.Parameter)7 ArrayList (java.util.ArrayList)7 TypeCategory (com.querydsl.codegen.utils.model.TypeCategory)6 StringWriter (java.io.StringWriter)6 ArrayType (javax.lang.model.type.ArrayType)6 ErrorType (javax.lang.model.type.ErrorType)6 ExecutableType (javax.lang.model.type.ExecutableType)6 NullType (javax.lang.model.type.NullType)6 PrimitiveType (javax.lang.model.type.PrimitiveType)6 WildcardType (javax.lang.model.type.WildcardType)6 Annotation (java.lang.annotation.Annotation)5