Changed getReturnedDomainClass to only resolve generic type for Collections and Pages.
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -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<User> findByFirstname(Pageable pageable, String firstname);
|
||||
|
||||
|
||||
GenericType<User> someMethod();
|
||||
}
|
||||
|
||||
private class GenericType<T> {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user