DATAJPA-45 - ClassUtils.getReturnedDomainClass works for parameterized component types of Collections and Page.
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -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<User> someMethod();
|
||||
|
||||
|
||||
List<Map<String, Object>> anotherMethod();
|
||||
}
|
||||
|
||||
private class GenericType<T> {
|
||||
|
||||
Reference in New Issue
Block a user