mirror of
https://github.com/querydsl/querydsl.git
synced 2026-07-03 21:07:49 +08:00
extended Type
This commit is contained in:
parent
f68da31101
commit
7ada1f4bb0
93
src/main/java/com/mysema/codegen/ClassType.java
Normal file
93
src/main/java/com/mysema/codegen/ClassType.java
Normal file
@ -0,0 +1,93 @@
|
||||
package com.mysema.codegen;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* Type represents a generic type used in code generation
|
||||
*
|
||||
* @author tiwe
|
||||
*
|
||||
* @param <T>
|
||||
*/
|
||||
public class ClassType<T> implements Type<T> {
|
||||
|
||||
private final Class<T> javaClass;
|
||||
|
||||
private final List<Type<?>> parameters;
|
||||
|
||||
public ClassType(Class<T> javaClass, List<Type<?>> parameters) {
|
||||
this.javaClass = javaClass;
|
||||
this.parameters = parameters;
|
||||
}
|
||||
|
||||
public ClassType(Class<T> clazz, Type<?>... parameters) {
|
||||
this(clazz, Arrays.asList(parameters));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o){
|
||||
if (o == this){
|
||||
return true;
|
||||
}else if (o instanceof ClassType<?>){
|
||||
ClassType<?> t = (ClassType<?>)o;
|
||||
return t.javaClass.equals(javaClass) && t.parameters.equals(parameters);
|
||||
}else{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getGenericName() {
|
||||
return getGenericName(Collections.<String>emptySet(), Collections.<String>emptySet());
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getGenericName(Set<String> packages, Set<String> classes) {
|
||||
if (parameters.isEmpty()){
|
||||
return ClassUtils.getName(javaClass, packages, classes);
|
||||
}else{
|
||||
StringBuilder builder = new StringBuilder();
|
||||
builder.append(ClassUtils.getName(javaClass, packages, classes));
|
||||
builder.append("<");
|
||||
boolean first = true;
|
||||
for (Type<?> parameter : parameters){
|
||||
builder.append(parameter.getGenericName(packages, classes));
|
||||
if (!first){
|
||||
builder.append(",");
|
||||
}
|
||||
first = false;
|
||||
}
|
||||
builder.append(">");
|
||||
return builder.toString();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return javaClass.getName();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getPackageName() {
|
||||
return javaClass.getPackage().getName();
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Type<?>> getParameters() {
|
||||
return parameters;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getSimpleName() {
|
||||
return javaClass.getSimpleName();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode(){
|
||||
return javaClass.hashCode();
|
||||
}
|
||||
|
||||
}
|
||||
@ -30,31 +30,7 @@ public final class ClassUtils {
|
||||
return cl.getName().replace('$', '.');
|
||||
}
|
||||
}
|
||||
|
||||
public static String getName(Type<?> cl){
|
||||
return getName(cl, Collections.singleton("java.lang"), Collections.<String>emptySet());
|
||||
}
|
||||
|
||||
public static String getName(Type<?> type, Set<String> packages, Set<String> classes){
|
||||
if (type.getParameters().isEmpty()){
|
||||
return getName(type.getJavaClass(), packages, classes);
|
||||
}else{
|
||||
StringBuilder builder = new StringBuilder();
|
||||
builder.append(getName(type.getJavaClass(), packages, classes));
|
||||
builder.append("<");
|
||||
boolean first = true;
|
||||
for (Type<?> parameter : type.getParameters()){
|
||||
builder.append(getName(parameter, packages, classes));
|
||||
if (!first){
|
||||
builder.append(",");
|
||||
}
|
||||
first = false;
|
||||
}
|
||||
builder.append(">");
|
||||
return builder.toString();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static Class<?> normalize(Class<?> clazz){
|
||||
if (List.class.isAssignableFrom(clazz)){
|
||||
return List.class;
|
||||
|
||||
@ -54,7 +54,7 @@ public class EvaluatorFactory {
|
||||
this.compilationOptions = Arrays.asList("-classpath", classpath, "-g:none");
|
||||
}
|
||||
|
||||
private void compile(String source, Type<?> projectionType,
|
||||
private void compile(String source, Class<?> projectionType,
|
||||
String[] names, Type<?>[] types, String id, Map<String,Object> constants) throws IOException {
|
||||
// create source
|
||||
StringWriter writer = new StringWriter();
|
||||
@ -62,7 +62,7 @@ public class EvaluatorFactory {
|
||||
javaw.beginClass(id, null);
|
||||
String[] params = new String[names.length];
|
||||
for (int i = 0; i < params.length; i++) {
|
||||
params[i] = ClassUtils.getName(types[i]) + " " + names[i];
|
||||
params[i] = types[i].getGenericName() + " " + names[i];
|
||||
}
|
||||
|
||||
for (Map.Entry<String,Object> entry : constants.entrySet()){
|
||||
@ -105,9 +105,9 @@ public class EvaluatorFactory {
|
||||
Map<String,Object> constants) {
|
||||
Type<?>[] types = new Type[classes.length];
|
||||
for (int i = 0; i < types.length; i++){
|
||||
types[i] = new Type(classes[i]);
|
||||
types[i] = new ClassType(classes[i]);
|
||||
}
|
||||
return createEvaluator(source, new Type(projectionType), names, types, classes, constants);
|
||||
return createEvaluator(source, projectionType, names, types, classes, constants);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -123,7 +123,7 @@ public class EvaluatorFactory {
|
||||
*/
|
||||
public <T> Evaluator<T> createEvaluator(
|
||||
String source,
|
||||
Type<? extends T> projection,
|
||||
Class<? extends T> projection,
|
||||
String[] names,
|
||||
Type<?>[] types,
|
||||
Class<?>[] classes,
|
||||
@ -147,7 +147,7 @@ public class EvaluatorFactory {
|
||||
}
|
||||
|
||||
Method method = clazz.getMethod("eval", classes);
|
||||
return new MethodEvaluator<T>(method, object, projection.getJavaClass());
|
||||
return new MethodEvaluator<T>(method, object, projection);
|
||||
} catch (ClassNotFoundException e) {
|
||||
throw new CodegenException(e);
|
||||
} catch (SecurityException e) {
|
||||
@ -168,7 +168,7 @@ public class EvaluatorFactory {
|
||||
|
||||
}
|
||||
|
||||
protected String toId(String source, Type<?> returnType, Type<?>... types) {
|
||||
protected String toId(String source, Class<?> returnType, Type<?>... types) {
|
||||
StringBuilder b = new StringBuilder("Q");
|
||||
b.append("_").append(source.hashCode());
|
||||
b.append("_").append(returnType.getName().hashCode());
|
||||
|
||||
@ -1,57 +1,25 @@
|
||||
package com.mysema.codegen;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* Type represents a generic type used in code generation
|
||||
*
|
||||
* @author tiwe
|
||||
*
|
||||
* @param <T>
|
||||
*/
|
||||
public class Type<T> {
|
||||
public interface Type<T> {
|
||||
|
||||
String getGenericName();
|
||||
|
||||
private final Class<T> javaClass;
|
||||
String getGenericName(Set<String> packages, Set<String> classes);
|
||||
|
||||
private final List<Type<?>> parameters;
|
||||
|
||||
public Type(Class<T> javaClass, List<Type<?>> parameters) {
|
||||
this.javaClass = javaClass;
|
||||
this.parameters = parameters;
|
||||
}
|
||||
|
||||
public Type(Class<T> clazz, Type<?>... parameters) {
|
||||
this(clazz, Arrays.asList(parameters));
|
||||
}
|
||||
|
||||
public Class<T> getJavaClass() {
|
||||
return javaClass;
|
||||
}
|
||||
String getName();
|
||||
|
||||
public List<Type<?>> getParameters() {
|
||||
return parameters;
|
||||
}
|
||||
String getPackageName();
|
||||
|
||||
List<Type<?>> getParameters();
|
||||
|
||||
String getSimpleName();
|
||||
|
||||
public String getName() {
|
||||
return javaClass.getName();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode(){
|
||||
return javaClass.hashCode();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o){
|
||||
if (o == this){
|
||||
return true;
|
||||
}else if (o instanceof Type<?>){
|
||||
Type<?> t = (Type<?>)o;
|
||||
return t.javaClass.equals(javaClass) && t.parameters.equals(parameters);
|
||||
}else{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@ -15,8 +15,8 @@ public class ComplexEvaluationTest {
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
public void testComplex(){Type<String> stringType = new Type<String>(String.class);
|
||||
Type<List> resultType = new Type<List>(List.class, stringType);
|
||||
public void testComplex(){ClassType<String> stringType = new ClassType<String>(String.class);
|
||||
Type<List> resultType = new ClassType<List>(List.class, stringType);
|
||||
StringBuilder source = new StringBuilder();
|
||||
source.append("java.util.List<String> rv = new java.util.ArrayList<String>();\n");
|
||||
source.append("for (String a : a_){\n");
|
||||
@ -30,7 +30,7 @@ public class ComplexEvaluationTest {
|
||||
|
||||
Evaluator<List> evaluator = factory.createEvaluator(
|
||||
source.toString(),
|
||||
resultType,
|
||||
List.class,
|
||||
new String[]{"a_","b_"},
|
||||
new Type[]{resultType, resultType},
|
||||
new Class[]{List.class,List.class},
|
||||
|
||||
Loading…
Reference in New Issue
Block a user