mirror of
https://github.com/querydsl/querydsl.git
synced 2026-07-03 21:07:49 +08:00
move common code to superclass
This commit is contained in:
parent
b2d91a2934
commit
e017835bea
@ -14,14 +14,18 @@
|
||||
package com.mysema.codegen;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.StringWriter;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.Collection;
|
||||
import java.util.Map;
|
||||
import java.util.WeakHashMap;
|
||||
|
||||
import com.mysema.codegen.model.ClassType;
|
||||
import com.mysema.codegen.model.Parameter;
|
||||
import com.mysema.codegen.model.SimpleType;
|
||||
import com.mysema.codegen.model.Type;
|
||||
import com.mysema.codegen.model.TypeCategory;
|
||||
import com.mysema.codegen.support.ClassUtils;
|
||||
|
||||
/**
|
||||
* @author tiwe
|
||||
@ -44,6 +48,40 @@ public abstract class AbstractEvaluatorFactory implements EvaluatorFactory {
|
||||
*/
|
||||
protected abstract void compile(String source, ClassType projection, String[] names, Type[] types,
|
||||
String id, Map<String, Object> constants) throws IOException;
|
||||
|
||||
/**
|
||||
* @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();
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
|
||||
@ -16,7 +16,6 @@ package com.mysema.codegen;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.io.StringWriter;
|
||||
import java.nio.charset.Charset;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
@ -50,11 +49,7 @@ import com.google.common.collect.Lists;
|
||||
import com.google.common.collect.Maps;
|
||||
import com.google.common.io.ByteStreams;
|
||||
import com.mysema.codegen.model.ClassType;
|
||||
import com.mysema.codegen.model.Parameter;
|
||||
import com.mysema.codegen.model.SimpleType;
|
||||
import com.mysema.codegen.model.Type;
|
||||
import com.mysema.codegen.model.TypeCategory;
|
||||
import com.mysema.codegen.support.ClassUtils;
|
||||
|
||||
/**
|
||||
* EvaluatorFactory is a factory implementation for creating Evaluator instances
|
||||
@ -95,28 +90,11 @@ public class ECJEvaluatorFactory extends AbstractEvaluatorFactory {
|
||||
protected void compile(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();
|
||||
source = createSource(source, projectionType, names, types, id, constants);
|
||||
|
||||
// compile
|
||||
final char[] targetContents = writer.toString().toCharArray();
|
||||
final String targetName = idType.getFullName();
|
||||
final char[] targetContents = source.toCharArray();
|
||||
final String targetName = id;
|
||||
final ICompilationUnit[] targetCompilationUnits = new ICompilationUnit[] { new ICompilationUnit() {
|
||||
@Override
|
||||
public char[] getContents() {
|
||||
|
||||
@ -50,7 +50,6 @@ public interface EvaluatorFactory {
|
||||
* @param constants
|
||||
* @return
|
||||
*/
|
||||
@SuppressWarnings(value = "unchecked")
|
||||
<T> Evaluator<T> createEvaluator(String source, ClassType projection, String[] names,
|
||||
Type[] types, Class<?>[] classes, Map<String, Object> constants);
|
||||
|
||||
|
||||
@ -29,11 +29,7 @@ import javax.tools.StandardLocation;
|
||||
import javax.tools.ToolProvider;
|
||||
|
||||
import com.mysema.codegen.model.ClassType;
|
||||
import com.mysema.codegen.model.Parameter;
|
||||
import com.mysema.codegen.model.SimpleType;
|
||||
import com.mysema.codegen.model.Type;
|
||||
import com.mysema.codegen.model.TypeCategory;
|
||||
import com.mysema.codegen.support.ClassUtils;
|
||||
|
||||
/**
|
||||
* JDKEvaluatorFactory is a factory implementation for creating Evaluator instances
|
||||
@ -66,27 +62,10 @@ public class JDKEvaluatorFactory extends AbstractEvaluatorFactory {
|
||||
protected void compile(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();
|
||||
source = createSource(source, projectionType, names, types, id, constants);
|
||||
|
||||
// compile
|
||||
SimpleJavaFileObject javaFileObject = new MemSourceFileObject(id, writer.toString());
|
||||
SimpleJavaFileObject javaFileObject = new MemSourceFileObject(id, source);
|
||||
Writer out = new StringWriter();
|
||||
|
||||
CompilationTask task = compiler.getTask(out, fileManager, null, compilationOptions, null,
|
||||
@ -94,7 +73,6 @@ public class JDKEvaluatorFactory extends AbstractEvaluatorFactory {
|
||||
if (!task.call().booleanValue()) {
|
||||
throw new CodegenException("Compilation of " + source + " failed.\n" + out.toString());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user