Browse Source

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

pull/2/head
Oliver Gierke 15 years ago
parent
commit
ff844d7cbc
  1. 14
      spring-data-commons-core/src/main/java/org/springframework/data/repository/util/ClassUtils.java
  2. 18
      spring-data-commons-core/src/test/java/org/springframework/data/repository/util/ClassUtilsUnitTests.java

14
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.Arrays;
import java.util.Collection; import java.util.Collection;
import org.springframework.data.domain.Page;
import org.springframework.data.repository.Repository; import org.springframework.data.repository.Repository;
import org.springframework.util.ReflectionUtils; import org.springframework.util.ReflectionUtils;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
@ -52,15 +53,18 @@ public abstract class ClassUtils {
*/ */
public static Class<?> getReturnedDomainClass(Method method) { 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) return (Class<?>) ((ParameterizedType) type)
.getActualTypeArguments()[0]; .getActualTypeArguments()[0];
} else {
return method.getReturnType();
} }
return returnType;
} }

18
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 @Test
public void determinesValidFieldsCorrectly() { public void determinesValidFieldsCorrectly() {
@ -79,5 +90,12 @@ public class ClassUtilsUnitTests {
private interface SomeDao extends Serializable, UserRepository { private interface SomeDao extends Serializable, UserRepository {
Page<User> findByFirstname(Pageable pageable, String firstname); Page<User> findByFirstname(Pageable pageable, String firstname);
GenericType<User> someMethod();
}
private class GenericType<T> {
} }
} }

Loading…
Cancel
Save