Skip to content

Commit 0ae2054

Browse files
committed
Adopt to deprecated QueryMethod constructor.
Closes #4941
1 parent 0054952 commit 0ae2054

File tree

3 files changed

+62
-48
lines changed

3 files changed

+62
-48
lines changed

Diff for: spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/query/MongoParameters.java

+36-1
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,17 @@
1515
*/
1616
package org.springframework.data.mongodb.repository.query;
1717

18+
import java.io.Serializable;
1819
import java.lang.reflect.Method;
1920
import java.util.Arrays;
2021
import java.util.List;
2122

2223
import org.springframework.core.MethodParameter;
2324
import org.springframework.data.domain.Range;
2425
import org.springframework.data.geo.Distance;
26+
import org.springframework.data.geo.GeoPage;
27+
import org.springframework.data.geo.GeoResult;
28+
import org.springframework.data.geo.GeoResults;
2529
import org.springframework.data.geo.Point;
2630
import org.springframework.data.mongodb.core.query.Collation;
2731
import org.springframework.data.mongodb.core.query.TextCriteria;
@@ -44,6 +48,9 @@
4448
*/
4549
public class MongoParameters extends Parameters<MongoParameters, MongoParameter> {
4650

51+
private static final List<Class<? extends Serializable>> GEO_NEAR_RESULTS = Arrays.asList(GeoResult.class,
52+
GeoResults.class, GeoPage.class);
53+
4754
private final int rangeIndex;
4855
private final int maxDistanceIndex;
4956
private final @Nullable Integer fullTextIndex;
@@ -56,7 +63,17 @@ public class MongoParameters extends Parameters<MongoParameters, MongoParameter>
5663
* Creates a new {@link MongoParameters} instance from the given {@link Method} and {@link MongoQueryMethod}.
5764
*
5865
* @param parametersSource must not be {@literal null}.
59-
* @param isGeoNearMethod indicate if this is a geo spatial query method
66+
* @since 4.5
67+
*/
68+
public MongoParameters(ParametersSource parametersSource) {
69+
this(parametersSource, isGeoNearQuery(parametersSource.getMethod()));
70+
}
71+
72+
/**
73+
* Creates a new {@link MongoParameters} instance from the given {@link Method} and {@link MongoQueryMethod}.
74+
*
75+
* @param parametersSource must not be {@literal null}.
76+
* @param isGeoNearMethod indicate if this is a geo-spatial query method
6077
*/
6178
public MongoParameters(ParametersSource parametersSource, boolean isGeoNearMethod) {
6279
this(parametersSource, new NearIndex(parametersSource, isGeoNearMethod));
@@ -104,6 +121,24 @@ private MongoParameters(List<MongoParameter> parameters, int maxDistanceIndex, @
104121
this.domainType = domainType;
105122
}
106123

124+
static boolean isGeoNearQuery(Method method) {
125+
126+
Class<?> returnType = method.getReturnType();
127+
128+
for (Class<?> type : GEO_NEAR_RESULTS) {
129+
if (type.isAssignableFrom(returnType)) {
130+
return true;
131+
}
132+
}
133+
134+
if (Iterable.class.isAssignableFrom(returnType)) {
135+
TypeInformation<?> from = TypeInformation.fromReturnTypeOf(method);
136+
return GeoResult.class.equals(from.getRequiredComponentType().getType());
137+
}
138+
139+
return false;
140+
}
141+
107142
static class NearIndex {
108143

109144
private final @Nullable Integer nearIndex;

Diff for: spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/query/MongoQueryMethod.java

+18-35
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,13 @@
1515
*/
1616
package org.springframework.data.mongodb.repository.query;
1717

18-
import java.io.Serializable;
1918
import java.lang.annotation.Annotation;
2019
import java.lang.reflect.Method;
21-
import java.util.Arrays;
22-
import java.util.List;
2320
import java.util.Map;
2421
import java.util.Optional;
22+
import java.util.function.Function;
2523

2624
import org.springframework.core.annotation.AnnotatedElementUtils;
27-
import org.springframework.data.geo.GeoPage;
28-
import org.springframework.data.geo.GeoResult;
29-
import org.springframework.data.geo.GeoResults;
3025
import org.springframework.data.mapping.context.MappingContext;
3126
import org.springframework.data.mongodb.core.annotation.Collation;
3227
import org.springframework.data.mongodb.core.mapping.MongoPersistentEntity;
@@ -65,15 +60,12 @@
6560
*/
6661
public class MongoQueryMethod extends QueryMethod {
6762

68-
@SuppressWarnings("unchecked") private static final List<Class<? extends Serializable>> GEO_NEAR_RESULTS = Arrays
69-
.asList(GeoResult.class, GeoResults.class, GeoPage.class);
70-
7163
private final Method method;
7264
private final MappingContext<? extends MongoPersistentEntity<?>, MongoPersistentProperty> mappingContext;
7365
private final Map<Class<? extends Annotation>, Optional<Annotation>> annotationCache;
7466

7567
private @Nullable MongoEntityMetadata<?> metadata;
76-
private Lazy<Boolean> isModifying = Lazy.of(this::resolveModifyingQueryIndicators);
68+
private final Lazy<Boolean> isModifying = Lazy.of(this::resolveModifyingQueryIndicators);
7769

7870
/**
7971
* Creates a new {@link MongoQueryMethod} from the given {@link Method}.
@@ -85,8 +77,22 @@ public class MongoQueryMethod extends QueryMethod {
8577
*/
8678
public MongoQueryMethod(Method method, RepositoryMetadata metadata, ProjectionFactory projectionFactory,
8779
MappingContext<? extends MongoPersistentEntity<?>, MongoPersistentProperty> mappingContext) {
80+
this(method, metadata, projectionFactory, mappingContext, MongoParameters::new);
81+
}
82+
83+
/**
84+
* Creates a new {@link MongoQueryMethod} from the given {@link Method}.
85+
*
86+
* @param method must not be {@literal null}.
87+
* @param metadata must not be {@literal null}.
88+
* @param projectionFactory must not be {@literal null}.
89+
* @param mappingContext must not be {@literal null}.
90+
*/
91+
MongoQueryMethod(Method method, RepositoryMetadata metadata, ProjectionFactory projectionFactory,
92+
MappingContext<? extends MongoPersistentEntity<?>, MongoPersistentProperty> mappingContext,
93+
Function<ParametersSource, ? extends MongoParameters> parametersFunction) {
8894

89-
super(method, metadata, projectionFactory);
95+
super(method, metadata, projectionFactory, parametersFunction);
9096

9197
Assert.notNull(mappingContext, "MappingContext must not be null");
9298

@@ -95,11 +101,6 @@ public MongoQueryMethod(Method method, RepositoryMetadata metadata, ProjectionFa
95101
this.annotationCache = new ConcurrentReferenceHashMap<>();
96102
}
97103

98-
@Override
99-
protected MongoParameters createParameters(ParametersSource parametersSource) {
100-
return new MongoParameters(parametersSource, isGeoNearQuery(parametersSource.getMethod()));
101-
}
102-
103104
/**
104105
* Returns whether the method has an annotated query.
105106
*
@@ -186,25 +187,7 @@ public MongoParameters getParameters() {
186187
* @return
187188
*/
188189
public boolean isGeoNearQuery() {
189-
return isGeoNearQuery(this.method);
190-
}
191-
192-
private boolean isGeoNearQuery(Method method) {
193-
194-
Class<?> returnType = method.getReturnType();
195-
196-
for (Class<?> type : GEO_NEAR_RESULTS) {
197-
if (type.isAssignableFrom(returnType)) {
198-
return true;
199-
}
200-
}
201-
202-
if (Iterable.class.isAssignableFrom(returnType)) {
203-
TypeInformation<?> from = TypeInformation.fromReturnTypeOf(method);
204-
return GeoResult.class.equals(from.getRequiredComponentType().getType());
205-
}
206-
207-
return false;
190+
return MongoParameters.isGeoNearQuery(this.method);
208191
}
209192

210193
/**

Diff for: spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/query/ReactiveMongoQueryMethod.java

+8-12
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@
1515
*/
1616
package org.springframework.data.mongodb.repository.query;
1717

18-
import static org.springframework.data.repository.util.ClassUtils.*;
19-
2018
import java.lang.reflect.Method;
2119

2220
import org.springframework.dao.InvalidDataAccessApiUsageException;
@@ -31,10 +29,10 @@
3129
import org.springframework.data.mongodb.repository.query.MongoParameters.MongoParameter;
3230
import org.springframework.data.projection.ProjectionFactory;
3331
import org.springframework.data.repository.core.RepositoryMetadata;
34-
import org.springframework.data.repository.query.ParametersSource;
3532
import org.springframework.data.repository.util.ReactiveWrapperConverters;
3633
import org.springframework.data.util.Lazy;
3734
import org.springframework.data.util.ReactiveWrappers;
35+
import org.springframework.data.util.ReflectionUtils;
3836
import org.springframework.data.util.TypeInformation;
3937
import org.springframework.util.ClassUtils;
4038

@@ -64,18 +62,16 @@ public class ReactiveMongoQueryMethod extends MongoQueryMethod {
6462
public ReactiveMongoQueryMethod(Method method, RepositoryMetadata metadata, ProjectionFactory projectionFactory,
6563
MappingContext<? extends MongoPersistentEntity<?>, MongoPersistentProperty> mappingContext) {
6664

67-
super(method, metadata, projectionFactory, mappingContext);
65+
super(method, metadata, projectionFactory, mappingContext, parametersSource -> {
66+
return new MongoParameters(parametersSource,
67+
MongoParameters.isGeoNearQuery(parametersSource.getMethod()) || isGeoNearQuery(parametersSource.getMethod()));
68+
});
6869

6970
this.method = method;
7071
this.isCollectionQuery = Lazy.of(() -> (!(isPageQuery() || isSliceQuery() || isScrollQuery())
7172
&& ReactiveWrappers.isMultiValueType(metadata.getReturnType(method).getType()) || super.isCollectionQuery()));
7273
}
7374

74-
@Override
75-
protected MongoParameters createParameters(ParametersSource parametersSource) {
76-
return new MongoParameters(parametersSource, isGeoNearQuery(parametersSource.getMethod()));
77-
}
78-
7975
@Override
8076
public boolean isCollectionQuery() {
8177
return isCollectionQuery.get();
@@ -86,7 +82,7 @@ public boolean isGeoNearQuery() {
8682
return isGeoNearQuery(method);
8783
}
8884

89-
private boolean isGeoNearQuery(Method method) {
85+
private static boolean isGeoNearQuery(Method method) {
9086

9187
if (ReactiveWrappers.supports(method.getReturnType())) {
9288
TypeInformation<?> from = TypeInformation.fromReturnTypeOf(method);
@@ -130,7 +126,7 @@ public boolean hasReactiveWrapperParameter() {
130126
@Override
131127
public void verify() {
132128

133-
if (hasParameterOfType(method, Pageable.class)) {
129+
if (ReflectionUtils.hasParameterOfType(method, Pageable.class)) {
134130

135131
TypeInformation<?> returnType = TypeInformation.fromReturnTypeOf(method);
136132

@@ -139,7 +135,7 @@ public void verify() {
139135
&& (PAGE_TYPE.isAssignableFrom(returnType.getRequiredComponentType())
140136
|| SLICE_TYPE.isAssignableFrom(returnType.getRequiredComponentType()));
141137

142-
if (hasParameterOfType(method, Sort.class)) {
138+
if (ReflectionUtils.hasParameterOfType(method, Sort.class)) {
143139
throw new IllegalStateException(String.format("Method must not have Pageable *and* Sort parameter;"
144140
+ " Use sorting capabilities on Pageable instead; Offending method: %s", method));
145141
}

0 commit comments

Comments
 (0)