#806838 : fixed primitive handling

This commit is contained in:
Timo Westkämper 2011-07-07 13:00:01 +00:00
parent 284646d923
commit fe32e528ac
3 changed files with 43 additions and 22 deletions

View File

@ -16,7 +16,10 @@ import com.mysema.query.annotations.PropertyType;
import com.mysema.query.annotations.QueryEntity;
import com.mysema.query.annotations.QueryProjection;
import com.mysema.query.annotations.QueryType;
import com.mysema.query.types.ConstantImpl;
import com.mysema.query.types.expr.NumberExpression;
import com.mysema.query.types.expr.StringExpression;
import com.mysema.query.types.path.NumberPath;
import com.mysema.query.types.path.StringPath;
public class QueryProjectionTest {
@ -54,10 +57,13 @@ public class QueryProjectionTest {
@Test
public void Entity_Case(){
QQueryProjectionTest_EntityWithProjection.create(ConstantImpl.create(0l)).newInstance(0l);
// QQueryProjectionTest_EntityWithProjection.create(ConstantImpl.create("")).newInstance("");
// QQueryProjectionTest_EntityWithProjection.create(ConstantImpl.create(0l), ConstantImpl.create("")).newInstance(0l,"");
// QQueryProjectionTest_EntityWithProjection.create(ConstantImpl.create(""), ConstantImpl.create("")).newInstance("","");
NumberExpression<Long> longExpr = new NumberPath<Long>(Long.class, "x");
StringExpression stringExpr = new StringPath("x");
QQueryProjectionTest_EntityWithProjection.create(longExpr).newInstance(0l);
QQueryProjectionTest_EntityWithProjection.create(stringExpr).newInstance("");
QQueryProjectionTest_EntityWithProjection.create(longExpr, stringExpr).newInstance(0l,"");
QQueryProjectionTest_EntityWithProjection.create(stringExpr,stringExpr).newInstance("","");
}
public static class DTOWithProjection {
@ -104,10 +110,13 @@ public class QueryProjectionTest {
@Test
public void Dto_Case() throws SecurityException, NoSuchMethodException{
new QQueryProjectionTest_DTOWithProjection(ConstantImpl.create(0l)).newInstance(0l);
// new QQueryProjectionTest_DTOWithProjection(StringConstant.create("")).newInstance("");
// new QQueryProjectionTest_DTOWithProjection(ConstantImpl.create(0l), StringConstant.create("")).newInstance(0l,"");
// new QQueryProjectionTest_DTOWithProjection(StringConstant.create(""), StringConstant.create("")).newInstance("","");
NumberExpression<Long> longExpr = new NumberPath<Long>(Long.class, "x");
StringExpression stringExpr = new StringPath("x");
new QQueryProjectionTest_DTOWithProjection(longExpr).newInstance(0l);
new QQueryProjectionTest_DTOWithProjection(stringExpr).newInstance("");
new QQueryProjectionTest_DTOWithProjection(longExpr, stringExpr).newInstance(0l,"");
new QQueryProjectionTest_DTOWithProjection(stringExpr, stringExpr).newInstance("","");
}
}

View File

@ -32,26 +32,34 @@ public class ConstructorExpression<T> extends ExpressionBase<T> implements Facto
return clazz;
}
}
public static <D> ConstructorExpression<D> create(Class<D> type, Expression<?>... args){
private static Class<?>[] getRealParameters(Class<?> type, Class<?>[] givenTypes) {
for (Constructor<?> c : type.getConstructors()){
Class<?>[] paramTypes = c.getParameterTypes();
if (paramTypes.length == args.length){
if (paramTypes.length == givenTypes.length){
boolean found = true;
for (int i = 0; i < paramTypes.length; i++){
if (!normalize(paramTypes[i]).isAssignableFrom(args[i].getType())){
if (!normalize(paramTypes[i]).isAssignableFrom(normalize(givenTypes[i]))){
found = false;
break;
}
}
if (found){
return new ConstructorExpression<D>(type, paramTypes, args);
return paramTypes;
}
}
}
throw new ExpressionException("Got no matching constructor");
}
public static <D> ConstructorExpression<D> create(Class<D> type, Expression<?>... args){
Class<?>[] paramTypes = new Class[args.length];
for (int i = 0; i < paramTypes.length; i++) {
paramTypes[i] = args[i].getType();
}
return new ConstructorExpression<D>(type, paramTypes, args);
}
private final List<Expression<?>> args;
private final Class<?>[] parameterTypes;
@ -65,7 +73,7 @@ public class ConstructorExpression<T> extends ExpressionBase<T> implements Facto
public ConstructorExpression(Class<T> type, Class<?>[] paramTypes, List<Expression<?>> args) {
super(type);
this.parameterTypes = paramTypes.clone();
this.parameterTypes = getRealParameters(type, paramTypes).clone();
this.args = args;
}

View File

@ -63,25 +63,29 @@ public class StringTest {
// Operation toString
assertEquals("lower(alias.name)", $(alias.getName()).lower().toString());
// EConstructor
// ConstructorExpression
ConstructorExpression<SomeType> someType = new ConstructorExpression<SomeType>(SomeType.class, new Class[]{SomeType.class}, $(alias));
assertEquals("new SomeType(alias)", someType.toString());
// EArrayConstructor
// ArrayConstructorExpression
ArrayConstructorExpression<SomeType> someTypeArray = new ArrayConstructorExpression<SomeType>(SomeType[].class,$(alias));
assertEquals("new SomeType[](alias)", someTypeArray.toString());
}
public interface SomeType{
public static class SomeType{
String getName();
public SomeType(){}
public SomeType(SomeType st){}
public String getName(){return ""; };
SomeType getRef();
public SomeType getRef(){ return null; };
List<SomeType> getRefs();
public List<SomeType> getRefs() { return null; };
int getAmount();
public int getAmount() { return 0; };
}
}