Search in sources :

Example 31 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 source and filter
 *
 * @param <T>
 * @param source source of the query
 * @param filter filter of the query
 * @return evaluator
 */
public <T> Evaluator<List<T>> createEvaluator(QueryMetadata metadata, Expression<? extends T> source, Predicate filter) {
    String typeName = ClassUtils.getName(source.getType());
    CollQuerySerializer ser = new CollQuerySerializer(templates);
    ser.append("java.util.List<" + typeName + "> rv = new java.util.ArrayList<" + typeName + ">();\n");
    ser.append("for (" + typeName + " " + source + " : " + source + "_) {\n");
    ser.append("    try {\n");
    ser.append("        if (").handle(filter).append(") {\n");
    ser.append("            rv.add(" + source + ");\n");
    ser.append("        }\n");
    ser.append("    } catch (NullPointerException npe) { }\n");
    ser.append("}\n");
    ser.append("return rv;");
    Map<Object, String> constantToLabel = ser.getConstantToLabel();
    Map<String, Object> constants = getConstants(metadata, constantToLabel);
    Type sourceType = new ClassType(TypeCategory.SIMPLE, source.getType());
    ClassType sourceListType = new ClassType(TypeCategory.SIMPLE, Iterable.class, sourceType);
    return factory.createEvaluator(ser.toString(), sourceListType, new String[] { source + "_" }, new Type[] { sourceListType }, new Class<?>[] { Iterable.class }, constants);
}
Also used : 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) ClassType(com.querydsl.codegen.utils.model.ClassType)

Example 32 with Type

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

the class ExtendedTypeFactory method createMapType.

protected Type createMapType(Iterator<? extends TypeMirror> typeMirrors, boolean deep) {
    if (!typeMirrors.hasNext()) {
        return new SimpleType(Types.MAP, defaultType, defaultType);
    }
    Type keyType = getType(typeMirrors.next(), deep);
    if (keyType == null) {
        keyType = defaultType;
    }
    Type valueType = getType(typeMirrors.next(), deep);
    if (valueType == null) {
        valueType = defaultType;
    } else if (valueType.getParameters().isEmpty()) {
        TypeElement element = env.getElementUtils().getTypeElement(valueType.getFullName());
        if (element != null) {
            Type type = getType(element.asType(), deep);
            if (!type.getParameters().isEmpty()) {
                valueType = new SimpleType(valueType, new Type[type.getParameters().size()]);
            }
        }
    }
    return new SimpleType(Types.MAP, keyType, valueType);
}
Also used : SimpleType(com.querydsl.codegen.utils.model.SimpleType) ErrorType(javax.lang.model.type.ErrorType) EntityType(com.querydsl.codegen.EntityType) DeclaredType(javax.lang.model.type.DeclaredType) SimpleType(com.querydsl.codegen.utils.model.SimpleType) WildcardType(javax.lang.model.type.WildcardType) ArrayType(javax.lang.model.type.ArrayType) Type(com.querydsl.codegen.utils.model.Type) ExecutableType(javax.lang.model.type.ExecutableType) NoType(javax.lang.model.type.NoType) NullType(javax.lang.model.type.NullType) PrimitiveType(javax.lang.model.type.PrimitiveType) TypeElement(javax.lang.model.element.TypeElement)

Example 33 with Type

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

the class ExtendedTypeFactory method createEnumType.

private Type createEnumType(DeclaredType declaredType, TypeElement typeElement, boolean deep) {
    // fallback
    Type enumType = createType(typeElement, TypeCategory.ENUM, declaredType.getTypeArguments(), deep);
    for (Class<? extends Annotation> entityAnn : entityAnnotations) {
        if (typeElement.getAnnotation(entityAnn) != null) {
            EntityType entityType = new EntityType(enumType, variableNameFunction);
            typeMappings.register(entityType, queryTypeFactory.create(entityType));
            return entityType;
        }
    }
    return enumType;
}
Also used : EntityType(com.querydsl.codegen.EntityType) ErrorType(javax.lang.model.type.ErrorType) EntityType(com.querydsl.codegen.EntityType) DeclaredType(javax.lang.model.type.DeclaredType) SimpleType(com.querydsl.codegen.utils.model.SimpleType) WildcardType(javax.lang.model.type.WildcardType) ArrayType(javax.lang.model.type.ArrayType) Type(com.querydsl.codegen.utils.model.Type) ExecutableType(javax.lang.model.type.ExecutableType) NoType(javax.lang.model.type.NoType) NullType(javax.lang.model.type.NullType) PrimitiveType(javax.lang.model.type.PrimitiveType)

Example 34 with Type

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

the class ExtendedTypeFactory method createClassType.

// TODO : simplify
private Type createClassType(DeclaredType declaredType, TypeElement typeElement, boolean deep) {
    // other
    String name = typeElement.getQualifiedName().toString();
    if (name.startsWith("java.")) {
        Iterator<? extends TypeMirror> i = declaredType.getTypeArguments().iterator();
        if (isAssignable(declaredType, mapType)) {
            return createMapType(i, deep);
        } else if (isAssignable(declaredType, listType)) {
            return createCollectionType(Types.LIST, i, deep);
        } else if (isAssignable(declaredType, setType)) {
            return createCollectionType(Types.SET, i, deep);
        } else if (isAssignable(declaredType, collectionType)) {
            return createCollectionType(Types.COLLECTION, i, deep);
        }
    }
    TypeCategory typeCategory = TypeCategory.get(name);
    if (typeCategory != TypeCategory.NUMERIC && isAssignable(typeElement.asType(), comparableType) && isSubType(typeElement.asType(), numberType)) {
        typeCategory = TypeCategory.NUMERIC;
    } else if (!typeCategory.isSubCategoryOf(TypeCategory.COMPARABLE) && isAssignable(typeElement.asType(), comparableType)) {
        typeCategory = TypeCategory.COMPARABLE;
    }
    for (Class<? extends Annotation> entityAnn : entityAnnotations) {
        if (isSimpleTypeEntity(typeElement, entityAnn)) {
            typeCategory = TypeCategory.ENTITY;
        }
    }
    List<? extends TypeMirror> arguments = declaredType.getTypeArguments();
    // for intersection types etc
    if (name.equals("")) {
        TypeMirror type = objectType;
        if (typeCategory == TypeCategory.COMPARABLE) {
            type = comparableType;
        }
        // find most specific type of superTypes which is a subtype of type
        List<? extends TypeMirror> superTypes = env.getTypeUtils().directSupertypes(declaredType);
        for (TypeMirror superType : superTypes) {
            if (env.getTypeUtils().isSubtype(superType, type)) {
                type = superType;
            }
        }
        typeElement = (TypeElement) env.getTypeUtils().asElement(type);
        if (type instanceof DeclaredType) {
            arguments = ((DeclaredType) type).getTypeArguments();
        }
    }
    Type type = createType(typeElement, typeCategory, arguments, deep);
    TypeMirror superType = typeElement.getSuperclass();
    TypeElement superTypeElement = null;
    if (superType instanceof DeclaredType) {
        superTypeElement = (TypeElement) ((DeclaredType) superType).asElement();
    }
    // entity type
    for (Class<? extends Annotation> entityAnn : entityAnnotations) {
        if (typeElement.getAnnotation(entityAnn) != null || (superTypeElement != null && superTypeElement.getAnnotation(entityAnn) != null)) {
            EntityType entityType = new EntityType(type, variableNameFunction);
            typeMappings.register(entityType, queryTypeFactory.create(entityType));
            return entityType;
        }
    }
    return type;
}
Also used : EntityType(com.querydsl.codegen.EntityType) ErrorType(javax.lang.model.type.ErrorType) EntityType(com.querydsl.codegen.EntityType) DeclaredType(javax.lang.model.type.DeclaredType) SimpleType(com.querydsl.codegen.utils.model.SimpleType) WildcardType(javax.lang.model.type.WildcardType) ArrayType(javax.lang.model.type.ArrayType) Type(com.querydsl.codegen.utils.model.Type) ExecutableType(javax.lang.model.type.ExecutableType) NoType(javax.lang.model.type.NoType) NullType(javax.lang.model.type.NullType) PrimitiveType(javax.lang.model.type.PrimitiveType) TypeMirror(javax.lang.model.type.TypeMirror) TypeElement(javax.lang.model.element.TypeElement) TypeCategory(com.querydsl.codegen.utils.model.TypeCategory) DeclaredType(javax.lang.model.type.DeclaredType)

Example 35 with Type

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

the class TypeElementHandler method transformParams.

public List<Parameter> transformParams(List<? extends VariableElement> params) {
    List<Parameter> parameters = new ArrayList<Parameter>(params.size());
    for (VariableElement param : params) {
        Type paramType = getType(param);
        parameters.add(new Parameter(param.getSimpleName().toString(), paramType));
    }
    return parameters;
}
Also used : QueryType(com.querydsl.core.annotations.QueryType) EntityType(com.querydsl.codegen.EntityType) Type(com.querydsl.codegen.utils.model.Type) PropertyType(com.querydsl.core.annotations.PropertyType) ArrayList(java.util.ArrayList) Parameter(com.querydsl.codegen.utils.model.Parameter) VariableElement(javax.lang.model.element.VariableElement)

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