Skip to content

Commit 18e2d49

Browse files
committed
DATACMNS-769 - Fixed assignability check for from QueryMethod.
The assignability check of method return types now explicitly detects wrapper types supported in QueryExceutionConverters and automatically unwraps those before the actual check.
1 parent 8359857 commit 18e2d49

File tree

2 files changed

+32
-8
lines changed

2 files changed

+32
-8
lines changed

src/main/java/org/springframework/data/repository/util/ClassUtils.java

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2008-2013 the original author or authors.
2+
* Copyright 2008-2015 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -21,6 +21,9 @@
2121
import java.util.Collection;
2222

2323
import org.springframework.data.repository.Repository;
24+
import org.springframework.data.util.ClassTypeInformation;
25+
import org.springframework.data.util.TypeInformation;
26+
import org.springframework.util.Assert;
2427
import org.springframework.util.ReflectionUtils;
2528
import org.springframework.util.StringUtils;
2629

@@ -34,9 +37,7 @@ public abstract class ClassUtils {
3437
/**
3538
* Private constructor to prevent instantiation.
3639
*/
37-
private ClassUtils() {
38-
39-
}
40+
private ClassUtils() {}
4041

4142
/**
4243
* Returns whether the given class contains a property with the given name.
@@ -96,15 +97,22 @@ public static int getNumberOfOccurences(Method method, Class<?> type) {
9697
}
9798

9899
/**
99-
* Asserts the given {@link Method}'s return type to be one of the given types.
100+
* Asserts the given {@link Method}'s return type to be one of the given types. Will unwrap known wrapper types before
101+
* the assignment check (see {@link QueryExecutionConverters}).
100102
*
101-
* @param method
102-
* @param types
103+
* @param method must not be {@literal null}.
104+
* @param types must not be {@literal null} or empty.
103105
*/
104106
public static void assertReturnTypeAssignable(Method method, Class<?>... types) {
105107

108+
Assert.notNull(method, "Method must not be null!");
109+
Assert.notEmpty(types, "Types must not be null or empty!");
110+
111+
TypeInformation<?> returnType = ClassTypeInformation.fromReturnTypeOf(method);
112+
returnType = QueryExecutionConverters.supports(returnType.getType()) ? returnType.getComponentType() : returnType;
113+
106114
for (Class<?> type : types) {
107-
if (type.isAssignableFrom(method.getReturnType())) {
115+
if (type.isAssignableFrom(returnType.getType())) {
108116
return;
109117
}
110118
}

src/test/java/org/springframework/data/repository/util/ClassUtilsUnitTests.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,16 @@
1919
import static org.springframework.data.repository.util.ClassUtils.*;
2020

2121
import java.io.Serializable;
22+
import java.lang.reflect.Method;
2223
import java.util.List;
2324
import java.util.Map;
25+
import java.util.concurrent.Future;
2426

2527
import org.junit.Test;
2628
import org.springframework.data.domain.Page;
2729
import org.springframework.data.domain.Pageable;
2830
import org.springframework.data.repository.Repository;
31+
import org.springframework.scheduling.annotation.Async;
2932

3033
/**
3134
* Unit test for {@link ClassUtils}.
@@ -48,6 +51,17 @@ public void determinesValidFieldsCorrectly() {
4851
assertFalse(hasProperty(User.class, "address"));
4952
}
5053

54+
/**
55+
* @see DATACMNS-769
56+
*/
57+
@Test
58+
public void unwrapsWrapperTypesBeforeAssignmentCheck() throws Exception {
59+
60+
Method method = UserRepository.class.getMethod("findAsync", Pageable.class);
61+
62+
assertReturnTypeAssignable(method, Page.class);
63+
}
64+
5165
@SuppressWarnings("unused")
5266
private class User {
5367

@@ -61,6 +75,8 @@ public String getAddress() {
6175

6276
static interface UserRepository extends Repository<User, Integer> {
6377

78+
@Async
79+
Future<Page<User>> findAsync(Pageable pageable);
6480
}
6581

6682
interface SomeDao extends Serializable, UserRepository {

0 commit comments

Comments
 (0)