Changed getReturnedDomainClass to only resolve generic type for Collections and Pages.

This commit is contained in:
Oliver Gierke
2011-03-01 20:39:52 +01:00
parent 1b98d32a4f
commit ff844d7cbc
2 changed files with 27 additions and 5 deletions

View File

@@ -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;
}

View File

@@ -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> {
}
}