
com.querydsl.collections.DefaultEvaluatorFactory Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of querydsl-collections Show documentation
Show all versions of querydsl-collections Show documentation
Collections support for Querydsl
The newest version!
/*
* Copyright 2015, The Querydsl Team (http://www.querydsl.com/team)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.querydsl.collections;
import com.querydsl.codegen.utils.ECJEvaluatorFactory;
import com.querydsl.codegen.utils.Evaluator;
import com.querydsl.codegen.utils.EvaluatorFactory;
import com.querydsl.codegen.utils.JDKEvaluatorFactory;
import com.querydsl.codegen.utils.model.ClassType;
import com.querydsl.codegen.utils.model.SimpleType;
import com.querydsl.codegen.utils.model.Type;
import com.querydsl.codegen.utils.model.TypeCategory;
import com.querydsl.codegen.utils.model.Types;
import com.querydsl.codegen.utils.support.ClassUtils;
import com.querydsl.core.JoinExpression;
import com.querydsl.core.JoinType;
import com.querydsl.core.QueryMetadata;
import com.querydsl.core.support.CollectionAnyVisitor;
import com.querydsl.core.support.Context;
import com.querydsl.core.types.Expression;
import com.querydsl.core.types.FactoryExpression;
import com.querydsl.core.types.Operation;
import com.querydsl.core.types.ParamExpression;
import com.querydsl.core.types.ParamNotSetException;
import com.querydsl.core.types.Predicate;
import com.querydsl.core.util.PrimitiveUtils;
import org.jetbrains.annotations.Nullable;
import javax.tools.JavaCompiler;
import javax.tools.ToolProvider;
import java.net.URLClassLoader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* {@code DefaultEvaluatorFactory} provides Java source templates for evaluation of {@link CollQuery} queries
*
* @author tiwe
*
*/
public class DefaultEvaluatorFactory {
private final EvaluatorFactory factory;
private final CollQueryTemplates templates;
private final CollectionAnyVisitor collectionAnyVisitor = new CollectionAnyVisitor();
public DefaultEvaluatorFactory(CollQueryTemplates templates) {
this(templates,
Thread.currentThread().getContextClassLoader() != null ? Thread.currentThread().getContextClassLoader() : DefaultEvaluatorFactory.class.getClassLoader());
}
public DefaultEvaluatorFactory(CollQueryTemplates templates, EvaluatorFactory factory) {
this.templates = templates;
this.factory = factory;
}
protected DefaultEvaluatorFactory(CollQueryTemplates templates,
URLClassLoader classLoader, JavaCompiler compiler) {
this.templates = templates;
this.factory = new JDKEvaluatorFactory(classLoader, compiler);
}
protected DefaultEvaluatorFactory(CollQueryTemplates templates, ClassLoader classLoader) {
this.templates = templates;
final JavaCompiler systemJavaCompiler = ToolProvider.getSystemJavaCompiler();
if (classLoader instanceof URLClassLoader && systemJavaCompiler != null) {
this.factory = new JDKEvaluatorFactory((URLClassLoader) classLoader, systemJavaCompiler);
} else {
// for OSGi and JRE compatibility
this.factory = new ECJEvaluatorFactory(classLoader);
}
}
/**
* Create an Evaluator for the given query sources and projection
*
* @param
* @param metadata query metadata
* @param sources sources of the query
* @param projection projection of the query
* @return evaluator
*/
public Evaluator create(QueryMetadata metadata, List extends Expression>> sources,
Expression projection) {
final CollQuerySerializer serializer = new CollQuerySerializer(templates);
serializer.append("return ");
if (projection instanceof FactoryExpression>) {
serializer.append("(");
serializer.append(ClassUtils.getName(projection.getType()));
serializer.append(")(");
serializer.handle(projection);
serializer.append(")");
} else {
serializer.handle(projection);
}
serializer.append(";");
Map
© 2015 - 2025 Weber Informatics LLC | Privacy Policy