Skip to content

Commit 6feecb0

Browse files
committed
Use RepositoryMetadata for Parameters construction.
1 parent 42953ce commit 6feecb0

File tree

5 files changed

+43
-37
lines changed

5 files changed

+43
-37
lines changed

src/main/java/org/springframework/data/repository/query/DefaultParameters.java

+7-8
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import java.util.function.IntFunction;
2121

2222
import org.springframework.core.MethodParameter;
23+
import org.springframework.data.repository.core.RepositoryMetadata;
2324
import org.springframework.data.util.TypeInformation;
2425

2526
/**
@@ -44,28 +45,26 @@ public DefaultParameters(Method method) {
4445
* Creates a new {@link DefaultParameters} instance from the given {@link Method} and aggregate
4546
* {@link TypeInformation}. Note that this constructor uses a {@link IntFunction MethodParameterFactory} that doesn't
4647
* resolve generic method parameter types that are declared on the containing class. Use
47-
* {@link DefaultParameters#DefaultParameters(Method, IntFunction, TypeInformation)} with a
48-
* {@link MethodParameter#withContainingClass(Class) contextual factory} to ensure proper gerneric resolution.
48+
* {@link DefaultParameters#DefaultParameters(RepositoryMetadata, Method)} with a
49+
* {@link MethodParameter#withContainingClass(Class) contextual factory} to ensure proper generic resolution.
4950
*
5051
* @param method must not be {@literal null}.
5152
* @param aggregateType must not be {@literal null}.
5253
*/
5354
public DefaultParameters(Method method, TypeInformation<?> aggregateType) {
54-
this(method, index -> new MethodParameter(method, index), aggregateType);
55+
super(method, param -> new Parameter(param, aggregateType));
5556
}
5657

5758
/**
5859
* Creates a new {@link DefaultParameters} instance from the given {@link Method} and aggregate
5960
* {@link TypeInformation}.
6061
*
62+
* @param metadata must not be {@literal null}.
6163
* @param method must not be {@literal null}.
62-
* @param parameterFactory must not be {@literal null}.
63-
* @param aggregateType must not be {@literal null}.
6464
* @since 3.2.1
6565
*/
66-
public DefaultParameters(Method method, IntFunction<MethodParameter> parameterFactory,
67-
TypeInformation<?> aggregateType) {
68-
super(method, parameterFactory, param -> new Parameter(param, aggregateType));
66+
public DefaultParameters(RepositoryMetadata metadata, Method method) {
67+
super(metadata, method, param -> new Parameter(param, metadata.getDomainTypeInformation()));
6968
}
7069

7170
private DefaultParameters(List<Parameter> parameters) {

src/main/java/org/springframework/data/repository/query/Parameters.java

+11-5
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
import java.util.Iterator;
2424
import java.util.List;
2525
import java.util.function.Function;
26-
import java.util.function.IntFunction;
2726

2827
import org.springframework.core.DefaultParameterNameDiscoverer;
2928
import org.springframework.core.MethodParameter;
@@ -32,6 +31,7 @@
3231
import org.springframework.data.domain.Pageable;
3332
import org.springframework.data.domain.ScrollPosition;
3433
import org.springframework.data.domain.Sort;
34+
import org.springframework.data.repository.core.RepositoryMetadata;
3535
import org.springframework.data.util.Lazy;
3636
import org.springframework.data.util.Streamable;
3737
import org.springframework.util.Assert;
@@ -84,21 +84,23 @@ public Parameters(Method method) {
8484
* @param method must not be {@literal null}.
8585
* @param parameterFactory must not be {@literal null}.
8686
* @since 3.0.2
87+
* @deprecated since 3.2.1, use {@link Parameters(RepositoryMetadata, Method, Function)} instead.
8788
*/
89+
@Deprecated(since = "3.2.1", forRemoval = true)
8890
protected Parameters(Method method, Function<MethodParameter, T> parameterFactory) {
89-
this(method, index -> new MethodParameter(method, index), parameterFactory);
91+
this(null, method, parameterFactory);
9092
}
9193

9294
/**
9395
* Creates a new {@link Parameters} instance for the given {@link Method} and {@link Function} to create a
9496
* {@link Parameter} instance from a {@link MethodParameter}.
9597
*
98+
* @param repositoryMetadata must not be {@literal null}.
9699
* @param method must not be {@literal null}.
97-
* @param methodParameterFactory must not be {@literal null}.
98100
* @param parameterFactory must not be {@literal null}.
99101
* @since 3.2.1
100102
*/
101-
protected Parameters(Method method, IntFunction<MethodParameter> methodParameterFactory,
103+
protected Parameters(RepositoryMetadata repositoryMetadata, Method method,
102104
Function<MethodParameter, T> parameterFactory) {
103105

104106
Assert.notNull(method, "Method must not be null");
@@ -118,7 +120,11 @@ protected Parameters(Method method, IntFunction<MethodParameter> methodParameter
118120

119121
for (int i = 0; i < parameterCount; i++) {
120122

121-
MethodParameter methodParameter = methodParameterFactory.apply(i);
123+
MethodParameter methodParameter = new MethodParameter(method, i);
124+
125+
if (repositoryMetadata != null) {
126+
methodParameter = methodParameter.withContainingClass(repositoryMetadata.getRepositoryInterface());
127+
}
122128

123129
methodParameter.initParameterNameDiscovery(PARAMETER_NAME_DISCOVERER);
124130

src/main/java/org/springframework/data/repository/query/QueryMethod.java

+10-11
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,9 @@
2020
import java.lang.reflect.Method;
2121
import java.util.Collections;
2222
import java.util.Set;
23-
import java.util.function.IntFunction;
2423
import java.util.function.Predicate;
2524
import java.util.stream.Stream;
2625

27-
import org.springframework.core.MethodParameter;
2826
import org.springframework.data.domain.Limit;
2927
import org.springframework.data.domain.Page;
3028
import org.springframework.data.domain.Pageable;
@@ -63,7 +61,6 @@ public class QueryMethod {
6361
private final ResultProcessor resultProcessor;
6462
private final Lazy<Class<?>> domainClass;
6563
private final Lazy<Boolean> isCollectionQuery;
66-
private final IntFunction<MethodParameter> methodParameterFactory;
6764

6865
/**
6966
* Creates a new {@link QueryMethod} from the given parameters. Looks up the correct query to use for following
@@ -88,8 +85,6 @@ public QueryMethod(Method method, RepositoryMetadata metadata, ProjectionFactory
8885
this.method = method;
8986
this.unwrappedReturnType = potentiallyUnwrapReturnTypeFor(metadata, method);
9087
this.metadata = metadata;
91-
this.methodParameterFactory = index -> new MethodParameter(method, index)
92-
.withContainingClass(metadata.getRepositoryInterface());
9388
this.parameters = createParameters(method);
9489

9590
this.domainClass = Lazy.of(() -> {
@@ -188,20 +183,24 @@ private boolean calculateIsCollectionQuery() {
188183
* @param method must not be {@literal null}.
189184
* @param domainType must not be {@literal null}.
190185
* @return must not return {@literal null}.
186+
* @deprecated since 3.2.1
191187
* @since 3.0.2
192188
*/
189+
@Deprecated(since = "3.2.1", forRemoval = true)
193190
protected Parameters<?, ?> createParameters(Method method, TypeInformation<?> domainType) {
194-
return new DefaultParameters(method, getMethodParameterFactory(), domainType);
191+
return createParameters(metadata, method);
195192
}
196193

197194
/**
198-
* The {@link IntFunction MethodParameterFactory} to create method parameters.
195+
* Creates a {@link Parameters} instance.
199196
*
200-
* @return factory to create method parameters.
197+
* @param metadata must not be {@literal null}.
198+
* @param method must not be {@literal null}.
199+
* @return must not return {@literal null}.
201200
* @since 3.2.1
202201
*/
203-
protected IntFunction<MethodParameter> getMethodParameterFactory() {
204-
return methodParameterFactory;
202+
protected Parameters<?, ?> createParameters(RepositoryMetadata metadata, Method method) {
203+
return new DefaultParameters(metadata, method);
205204
}
206205

207206
/**
@@ -329,7 +328,7 @@ public ResultProcessor getResultProcessor() {
329328
return resultProcessor;
330329
}
331330

332-
protected RepositoryMetadata getMetadata() {
331+
RepositoryMetadata getMetadata() {
333332
return metadata;
334333
}
335334

src/test/java/org/springframework/data/repository/query/ParametersParameterAccessorUnitTests.java

+2
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import org.springframework.data.domain.ScrollPosition;
2626
import org.springframework.data.domain.Sort;
2727
import org.springframework.data.domain.Sort.Order;
28+
import org.springframework.data.repository.core.RepositoryMetadata;
2829

2930
/**
3031
* Unit tests for {@link ParametersParameterAccessor}.
@@ -36,6 +37,7 @@
3637
class ParametersParameterAccessorUnitTests {
3738

3839
Parameters<?, ?> parameters;
40+
RepositoryMetadata metadata;
3941

4042
@BeforeEach
4143
void setUp() throws Exception {

src/test/java/org/springframework/data/repository/query/ParametersUnitTests.java

+13-13
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,15 @@
2727
import org.junit.jupiter.api.Test;
2828
import org.reactivestreams.Publisher;
2929

30-
import org.springframework.core.MethodParameter;
3130
import org.springframework.data.domain.Limit;
3231
import org.springframework.data.domain.OffsetScrollPosition;
3332
import org.springframework.data.domain.Page;
3433
import org.springframework.data.domain.Pageable;
3534
import org.springframework.data.domain.Sort;
3635
import org.springframework.data.domain.Window;
3736
import org.springframework.data.repository.Repository;
38-
import org.springframework.data.util.TypeInformation;
37+
import org.springframework.data.repository.core.RepositoryMetadata;
38+
import org.springframework.data.repository.core.support.DefaultRepositoryMetadata;
3939
import org.springframework.test.util.ReflectionTestUtils;
4040

4141
/**
@@ -47,11 +47,13 @@
4747
class ParametersUnitTests {
4848

4949
private Method valid;
50+
private RepositoryMetadata metadata;
5051

5152
@BeforeEach
5253
void setUp() throws SecurityException, NoSuchMethodException {
5354

5455
valid = SampleDao.class.getMethod("valid", String.class);
56+
metadata = new DefaultRepositoryMetadata(SampleDao.class);
5557
}
5658

5759
@Test
@@ -60,14 +62,14 @@ void checksValidMethodCorrectly() throws Exception {
6062
var validWithPageable = SampleDao.class.getMethod("validWithPageable", String.class, Pageable.class);
6163
var validWithSort = SampleDao.class.getMethod("validWithSort", String.class, Sort.class);
6264

63-
new DefaultParameters(valid);
64-
new DefaultParameters(validWithPageable);
65-
new DefaultParameters(validWithSort);
65+
new DefaultParameters(metadata, valid);
66+
new DefaultParameters(metadata, validWithPageable);
67+
new DefaultParameters(metadata, validWithSort);
6668
}
6769

6870
@Test
6971
void rejectsNullMethod() {
70-
assertThatIllegalArgumentException().isThrownBy(() -> new DefaultParameters(null));
72+
assertThatIllegalArgumentException().isThrownBy(() -> new DefaultParameters(metadata, null));
7173
}
7274

7375
@Test
@@ -91,12 +93,12 @@ void calculatesPlaceholderPositionCorrectly() throws Exception {
9193

9294
var method = SampleDao.class.getMethod("validWithSortFirst", Sort.class, String.class);
9395

94-
Parameters<?, ?> parameters = new DefaultParameters(method);
96+
Parameters<?, ?> parameters = new DefaultParameters(metadata, method);
9597
assertThat(parameters.getBindableParameter(0).getIndex()).isEqualTo(1);
9698

9799
method = SampleDao.class.getMethod("validWithSortInBetween", String.class, Sort.class, String.class);
98100

99-
parameters = new DefaultParameters(method);
101+
parameters = new DefaultParameters(metadata, method);
100102

101103
assertThat(parameters.getBindableParameter(0).getIndex()).isEqualTo(0);
102104
assertThat(parameters.getBindableParameter(1).getIndex()).isEqualTo(2);
@@ -211,9 +213,7 @@ void considersGenericType() throws Exception {
211213

212214
var method = TypedInterface.class.getMethod("foo", Object.class);
213215

214-
var parameters = new DefaultParameters(method,
215-
index -> new MethodParameter(method, index).withContainingClass(TypedInterface.class),
216-
TypeInformation.of(User.class));
216+
var parameters = new DefaultParameters(new DefaultRepositoryMetadata(TypedInterface.class), method);
217217

218218
assertThat(parameters.getParameter(0).getType()).isEqualTo(Long.class);
219219
}
@@ -223,14 +223,14 @@ private Parameters<?, Parameter> getParametersFor(String methodName, Class<?>...
223223

224224
var method = SampleDao.class.getMethod(methodName, parameterTypes);
225225

226-
return new DefaultParameters(method);
226+
return new DefaultParameters(metadata, method);
227227
}
228228

229229
static class User {
230230

231231
}
232232

233-
interface SampleDao {
233+
interface SampleDao extends Repository<User, String> {
234234

235235
User valid(@Param("username") String username);
236236

0 commit comments

Comments
 (0)