From fe32e528ace6c086a595d39aa268f5ef0c7939a3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timo=20Westk=C3=A4mper?= Date: Thu, 7 Jul 2011 13:00:01 +0000 Subject: [PATCH] #806838 : fixed primitive handling --- .../query/domain/QueryProjectionTest.java | 27 ++++++++++++------- .../query/types/ConstructorExpression.java | 20 +++++++++----- .../com/mysema/query/types/StringTest.java | 18 ++++++++----- 3 files changed, 43 insertions(+), 22 deletions(-) diff --git a/querydsl-apt/src/test/java/com/mysema/query/domain/QueryProjectionTest.java b/querydsl-apt/src/test/java/com/mysema/query/domain/QueryProjectionTest.java index b78e8ba58..f78cb3646 100644 --- a/querydsl-apt/src/test/java/com/mysema/query/domain/QueryProjectionTest.java +++ b/querydsl-apt/src/test/java/com/mysema/query/domain/QueryProjectionTest.java @@ -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 longExpr = new NumberPath(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 longExpr = new NumberPath(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("",""); } } diff --git a/querydsl-core/src/main/java/com/mysema/query/types/ConstructorExpression.java b/querydsl-core/src/main/java/com/mysema/query/types/ConstructorExpression.java index f8ce99dab..3ad043ba3 100644 --- a/querydsl-core/src/main/java/com/mysema/query/types/ConstructorExpression.java +++ b/querydsl-core/src/main/java/com/mysema/query/types/ConstructorExpression.java @@ -32,26 +32,34 @@ public class ConstructorExpression extends ExpressionBase implements Facto return clazz; } } - - public static ConstructorExpression create(Class 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(type, paramTypes, args); + return paramTypes; } } } throw new ExpressionException("Got no matching constructor"); } + public static ConstructorExpression create(Class 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(type, paramTypes, args); + } + private final List> args; private final Class[] parameterTypes; @@ -65,7 +73,7 @@ public class ConstructorExpression extends ExpressionBase implements Facto public ConstructorExpression(Class type, Class[] paramTypes, List> args) { super(type); - this.parameterTypes = paramTypes.clone(); + this.parameterTypes = getRealParameters(type, paramTypes).clone(); this.args = args; } diff --git a/querydsl-core/src/test/java/com/mysema/query/types/StringTest.java b/querydsl-core/src/test/java/com/mysema/query/types/StringTest.java index cebf1c73b..fd6d71d1e 100644 --- a/querydsl-core/src/test/java/com/mysema/query/types/StringTest.java +++ b/querydsl-core/src/test/java/com/mysema/query/types/StringTest.java @@ -63,25 +63,29 @@ public class StringTest { // Operation toString assertEquals("lower(alias.name)", $(alias.getName()).lower().toString()); - // EConstructor + // ConstructorExpression ConstructorExpression someType = new ConstructorExpression(SomeType.class, new Class[]{SomeType.class}, $(alias)); assertEquals("new SomeType(alias)", someType.toString()); - // EArrayConstructor + // ArrayConstructorExpression ArrayConstructorExpression someTypeArray = new ArrayConstructorExpression(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 getRefs(); + public List getRefs() { return null; }; - int getAmount(); + public int getAmount() { return 0; }; } }