From 680a74c8e1a0b6921d5b9dd703ff8c145621de18 Mon Sep 17 00:00:00 2001 From: Oliver Gierke Date: Fri, 8 Apr 2011 22:05:09 +0200 Subject: [PATCH] DATAJPA-45 - ClassUtils.getReturnedDomainClass works for parameterized component types of Collections and Page. --- .../data/repository/util/ClassUtils.java | 20 +++++++++++-------- .../repository/util/ClassUtilsUnitTests.java | 11 ++++++++++ 2 files changed, 23 insertions(+), 8 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 ef41bee9c..5d9de2a5e 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,8 +22,11 @@ import java.lang.reflect.Type; import java.util.Arrays; import java.util.Collection; +import org.springframework.core.GenericTypeResolver; import org.springframework.data.domain.Page; import org.springframework.data.repository.Repository; +import org.springframework.data.util.ClassTypeInformation; +import org.springframework.data.util.TypeInformation; import org.springframework.util.ReflectionUtils; import org.springframework.util.StringUtils; @@ -53,18 +56,19 @@ public abstract class ClassUtils { */ public static Class getReturnedDomainClass(Method method) { - Class returnType = method.getReturnType(); + Class type = method.getReturnType(); - if (Collection.class.isAssignableFrom(returnType) - || Page.class.isAssignableFrom(returnType)) { + if (Collection.class.isAssignableFrom(type) + || Page.class.isAssignableFrom(type)) { - Type type = method.getGenericReturnType(); - - return (Class) ((ParameterizedType) type) - .getActualTypeArguments()[0]; + ParameterizedType returnType = (ParameterizedType) method.getGenericReturnType(); + Type componentType = returnType.getActualTypeArguments()[0]; + + return componentType instanceof ParameterizedType ? (Class) ((ParameterizedType) componentType).getRawType() + : (Class) componentType; } - return returnType; + return type; } 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 98df4b23b..bc61a3ca7 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 @@ -15,10 +15,13 @@ */ package org.springframework.data.repository.util; +import static org.hamcrest.CoreMatchers.*; import static org.junit.Assert.*; import static org.springframework.data.repository.util.ClassUtils.*; import java.io.Serializable; +import java.util.List; +import java.util.Map; import org.junit.Test; import org.springframework.data.domain.Page; @@ -59,6 +62,11 @@ public class ClassUtilsUnitTests { assertTrue(hasProperty(User.class, "Firstname")); assertFalse(hasProperty(User.class, "address")); } + + @Test + public void handlesGenericTypeInReturnedCollectionCorrectly() throws SecurityException, NoSuchMethodException { + assertEquals(Map.class, getReturnedDomainClass(SomeDao.class.getMethod("anotherMethod"))); + } @SuppressWarnings("unused") private class User { @@ -93,6 +101,9 @@ public class ClassUtilsUnitTests { GenericType someMethod(); + + + List> anotherMethod(); } private class GenericType {