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