From ff844d7cbcd11573dedbb7836b75adc159a53b48 Mon Sep 17 00:00:00 2001 From: Oliver Gierke Date: Tue, 1 Mar 2011 20:39:52 +0100 Subject: [PATCH] Changed getReturnedDomainClass to only resolve generic type for Collections and Pages. --- .../data/repository/util/ClassUtils.java | 14 +++++++++----- .../repository/util/ClassUtilsUnitTests.java | 18 ++++++++++++++++++ 2 files changed, 27 insertions(+), 5 deletions(-) diff --git a/spring-data-commons-core/src/main/java/org/springframework/data/repository/util/ClassUtils.java b/spring-data-commons-core/src/main/java/org/springframework/data/repository/util/ClassUtils.java index 2fdabe3e9..ef41bee9c 100644 --- a/spring-data-commons-core/src/main/java/org/springframework/data/repository/util/ClassUtils.java +++ b/spring-data-commons-core/src/main/java/org/springframework/data/repository/util/ClassUtils.java @@ -22,6 +22,7 @@ import java.lang.reflect.Type; import java.util.Arrays; import java.util.Collection; +import org.springframework.data.domain.Page; import org.springframework.data.repository.Repository; import org.springframework.util.ReflectionUtils; import org.springframework.util.StringUtils; @@ -52,15 +53,18 @@ public abstract class ClassUtils { */ public static Class getReturnedDomainClass(Method method) { - Type type = method.getGenericReturnType(); + Class returnType = method.getReturnType(); + + if (Collection.class.isAssignableFrom(returnType) + || Page.class.isAssignableFrom(returnType)) { + + Type type = method.getGenericReturnType(); - if (type instanceof ParameterizedType) { return (Class) ((ParameterizedType) type) .getActualTypeArguments()[0]; - - } else { - return method.getReturnType(); } + + return returnType; } diff --git a/spring-data-commons-core/src/test/java/org/springframework/data/repository/util/ClassUtilsUnitTests.java b/spring-data-commons-core/src/test/java/org/springframework/data/repository/util/ClassUtilsUnitTests.java index e8a111f35..98df4b23b 100644 --- a/spring-data-commons-core/src/test/java/org/springframework/data/repository/util/ClassUtilsUnitTests.java +++ b/spring-data-commons-core/src/test/java/org/springframework/data/repository/util/ClassUtilsUnitTests.java @@ -41,6 +41,17 @@ public class ClassUtilsUnitTests { } + @Test + public void determinesReturnType() throws Exception { + + assertEquals(User.class, + getReturnedDomainClass(SomeDao.class.getMethod( + "findByFirstname", Pageable.class, String.class))); + assertEquals(GenericType.class, + getReturnedDomainClass(SomeDao.class.getMethod("someMethod"))); + } + + @Test public void determinesValidFieldsCorrectly() { @@ -79,5 +90,12 @@ public class ClassUtilsUnitTests { private interface SomeDao extends Serializable, UserRepository { Page findByFirstname(Pageable pageable, String firstname); + + + GenericType someMethod(); + } + + private class GenericType { + } }