Skip to content

Commit 46ece81

Browse files
committed
Polishing.
Reformat code. Use existing SimpleTypeHolder instead of static MongoSimpleTypes. See: #5027 Original pull request: #5038
1 parent 00ab16f commit 46ece81

File tree

12 files changed

+119
-132
lines changed

12 files changed

+119
-132
lines changed

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/MongoTemplate.java

Lines changed: 37 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -111,11 +111,9 @@
111111
import org.springframework.data.mongodb.core.timeseries.Granularity;
112112
import org.springframework.data.mongodb.core.validation.Validator;
113113
import org.springframework.data.projection.EntityProjection;
114-
import org.springframework.data.projection.ProjectionFactory;
115114
import org.springframework.data.util.CloseableIterator;
116115
import org.springframework.data.util.Lazy;
117116
import org.springframework.data.util.Optionals;
118-
import org.springframework.data.util.TypeInformation;
119117
import org.springframework.lang.Contract;
120118
import org.springframework.util.Assert;
121119
import org.springframework.util.ClassUtils;
@@ -498,7 +496,7 @@ protected <T> Stream<T> doStream(Query query, Class<?> entityType, String collec
498496
return doStream(query, entityType, collectionName, returnType, QueryResultConverter.entity());
499497
}
500498

501-
@SuppressWarnings({"ConstantConditions", "NullAway"})
499+
@SuppressWarnings({ "ConstantConditions", "NullAway" })
502500
<T, R> Stream<R> doStream(Query query, Class<?> entityType, String collectionName, Class<T> returnType,
503501
QueryResultConverter<? super T, ? extends R> resultConverter) {
504502

@@ -523,8 +521,7 @@ <T, R> Stream<R> doStream(Query query, Class<?> entityType, String collectionNam
523521

524522
DocumentCallback<R> resultReader = getResultReader(projection, collectionName, resultConverter);
525523

526-
return new CloseableIterableCursorAdapter<>(cursor, exceptionTranslator,
527-
resultReader).stream();
524+
return new CloseableIterableCursorAdapter<>(cursor, exceptionTranslator, resultReader).stream();
528525
});
529526
}
530527

@@ -1152,7 +1149,7 @@ <T, R> GeoResults<R> doGeoNear(NearQuery near, Class<?> domainType, String colle
11521149

11531150
@Override
11541151
public <T> @Nullable T findAndModify(Query query, UpdateDefinition update, FindAndModifyOptions options,
1155-
Class<T> entityClass, String collectionName) {
1152+
Class<T> entityClass, String collectionName) {
11561153
return findAndModify(query, update, options, entityClass, collectionName, QueryResultConverter.entity());
11571154
}
11581155

@@ -1182,12 +1179,13 @@ <T, R> GeoResults<R> doGeoNear(NearQuery near, Class<?> domainType, String colle
11821179

11831180
@Override
11841181
public <S, T> @Nullable T findAndReplace(Query query, S replacement, FindAndReplaceOptions options,
1185-
Class<S> entityType, String collectionName, Class<T> resultType) {
1186-
return findAndReplace(query, replacement, options, entityType, collectionName, resultType, QueryResultConverter.entity());
1182+
Class<S> entityType, String collectionName, Class<T> resultType) {
1183+
return findAndReplace(query, replacement, options, entityType, collectionName, resultType,
1184+
QueryResultConverter.entity());
11871185
}
11881186

1189-
<S, T, R> @Nullable R findAndReplace(Query query, S replacement, FindAndReplaceOptions options,
1190-
Class<S> entityType, String collectionName, Class<T> resultType, QueryResultConverter<? super T, ? extends R> resultConverter) {
1187+
<S, T, R> @Nullable R findAndReplace(Query query, S replacement, FindAndReplaceOptions options, Class<S> entityType,
1188+
String collectionName, Class<T> resultType, QueryResultConverter<? super T, ? extends R> resultConverter) {
11911189

11921190
Assert.notNull(query, "Query must not be null");
11931191
Assert.notNull(replacement, "Replacement must not be null");
@@ -1215,7 +1213,8 @@ <T, R> GeoResults<R> doGeoNear(NearQuery near, Class<?> domainType, String colle
12151213
maybeCallBeforeSave(replacement, mappedReplacement, collectionName);
12161214

12171215
R saved = doFindAndReplace(collectionPreparer, collectionName, mappedQuery, mappedFields, mappedSort,
1218-
queryContext.getCollation(entityType).orElse(null), entityType, mappedReplacement, options, projection, resultConverter);
1216+
queryContext.getCollation(entityType).orElse(null), entityType, mappedReplacement, options, projection,
1217+
resultConverter);
12191218

12201219
if (saved != null) {
12211220
maybeEmitEvent(new AfterSaveEvent<>(saved, mappedReplacement, collectionName));
@@ -1508,7 +1507,7 @@ protected <T> Collection<T> doInsertBatch(String collectionName, Collection<? ex
15081507
initialized = maybeCallBeforeSave(initialized, document, collectionName);
15091508

15101509
MappedDocument mappedDocument = queryOperations.createInsertContext(MappedDocument.of(document))
1511-
.prepareId(uninitialized.getClass());
1510+
.prepareId(uninitialized.getClass());
15121511

15131512
documentList.add(mappedDocument.getDocument());
15141513
initializedBatchToSave.add(initialized);
@@ -2221,14 +2220,14 @@ public T doWith(Document object) {
22212220
}
22222221
};
22232222

2224-
List<T> result = doFind(collectionName, createDelegate(query), query.getQueryObject(), query.getFieldsObject(), entityClass,
2225-
new QueryCursorPreparer(query, entityClass), callback);
2223+
List<T> result = doFind(collectionName, createDelegate(query), query.getQueryObject(), query.getFieldsObject(),
2224+
entityClass, new QueryCursorPreparer(query, entityClass), callback);
22262225

22272226
if (!CollectionUtils.isEmpty(result)) {
22282227

22292228
Criteria[] criterias = ids.stream() //
2230-
.map(it -> Criteria.where("_id").is(it)) //
2231-
.toArray(Criteria[]::new);
2229+
.map(it -> Criteria.where("_id").is(it)) //
2230+
.toArray(Criteria[]::new);
22322231

22332232
Query removeQuery = new Query(criterias.length == 1 ? criterias[0] : new Criteria().orOperator(criterias));
22342233
if (query.hasReadPreference()) {
@@ -2270,20 +2269,19 @@ protected <O> AggregationResults<O> doAggregate(Aggregation aggregation, String
22702269
return doAggregate(aggregation, collectionName, outputType, QueryResultConverter.entity(), context);
22712270
}
22722271

2273-
@SuppressWarnings({"ConstantConditions", "NullAway"})
2272+
@SuppressWarnings({ "ConstantConditions", "NullAway" })
22742273
<T, O> AggregationResults<O> doAggregate(Aggregation aggregation, String collectionName, Class<T> outputType,
22752274
QueryResultConverter<? super T, ? extends O> resultConverter, AggregationOperationContext context) {
22762275

22772276
final DocumentCallback<O> callback;
2278-
if(aggregation instanceof TypedAggregation<?> ta && outputType.isInterface()) {
2277+
if (aggregation instanceof TypedAggregation<?> ta && outputType.isInterface()) {
22792278
EntityProjection<T, ?> projection = operations.introspectProjection(outputType, ta.getInputType());
22802279
ProjectingReadCallback cb = new ProjectingReadCallback(mongoConverter, projection, collectionName);
2281-
callback = new QueryResultConverterCallback<>(resultConverter,
2282-
cb);
2280+
callback = new QueryResultConverterCallback<>(resultConverter, cb);
22832281
} else {
22842282

22852283
callback = new QueryResultConverterCallback<>(resultConverter,
2286-
new ReadDocumentCallback<>(mongoConverter, outputType, collectionName));
2284+
new ReadDocumentCallback<>(mongoConverter, outputType, collectionName));
22872285
}
22882286

22892287
AggregationOptions options = aggregation.getOptions();
@@ -2370,8 +2368,7 @@ protected <O> Stream<O> aggregateStream(Aggregation aggregation, String collecti
23702368

23712369
@SuppressWarnings({ "ConstantConditions", "NullAway" })
23722370
<T, O> Stream<O> doAggregateStream(Aggregation aggregation, String collectionName, Class<T> outputType,
2373-
QueryResultConverter<? super T, ? extends O> resultConverter,
2374-
@Nullable AggregationOperationContext context) {
2371+
QueryResultConverter<? super T, ? extends O> resultConverter, @Nullable AggregationOperationContext context) {
23752372

23762373
Assert.notNull(aggregation, "Aggregation pipeline must not be null");
23772374
Assert.hasText(collectionName, "Collection name must not be null or empty");
@@ -2649,9 +2646,8 @@ private CreateCollectionOptions getCreateCollectionOptions(Document document) {
26492646
* @return the converted object or {@literal null} if none exists.
26502647
*/
26512648
@Nullable
2652-
protected <T> T doFindOne(String collectionName,
2653-
CollectionPreparer<MongoCollection<Document>> collectionPreparer, Document query, Document fields,
2654-
Class<T> entityClass) {
2649+
protected <T> T doFindOne(String collectionName, CollectionPreparer<MongoCollection<Document>> collectionPreparer,
2650+
Document query, Document fields, Class<T> entityClass) {
26552651
return doFindOne(collectionName, collectionPreparer, query, fields, CursorPreparer.NO_OP_PREPARER, entityClass);
26562652
}
26572653

@@ -2670,9 +2666,8 @@ protected <T> T doFindOne(String collectionName,
26702666
*/
26712667
@Nullable
26722668
@SuppressWarnings("ConstantConditions")
2673-
protected <T> T doFindOne(String collectionName,
2674-
CollectionPreparer<MongoCollection<Document>> collectionPreparer, Document query, Document fields,
2675-
CursorPreparer preparer, Class<T> entityClass) {
2669+
protected <T> T doFindOne(String collectionName, CollectionPreparer<MongoCollection<Document>> collectionPreparer,
2670+
Document query, Document fields, CursorPreparer preparer, Class<T> entityClass) {
26762671

26772672
MongoPersistentEntity<?> entity = mappingContext.getPersistentEntity(entityClass);
26782673

@@ -2870,9 +2865,9 @@ Document getMappedValidator(Validator validator, Class<?> domainType) {
28702865

28712866
@SuppressWarnings("ConstantConditions")
28722867
<S, T> @Nullable T doFindAndModify(CollectionPreparer<MongoCollection<Document>> collectionPreparer,
2873-
String collectionName,
2874-
Document query, @Nullable Document fields, @Nullable Document sort, Class<S> entityClass, UpdateDefinition update,
2875-
@Nullable FindAndModifyOptions options, QueryResultConverter<? super S, ? extends T> resultConverter) {
2868+
String collectionName, Document query, @Nullable Document fields, @Nullable Document sort, Class<S> entityClass,
2869+
UpdateDefinition update, @Nullable FindAndModifyOptions options,
2870+
QueryResultConverter<? super S, ? extends T> resultConverter) {
28762871

28772872
if (options == null) {
28782873
options = new FindAndModifyOptions();
@@ -2894,7 +2889,8 @@ Document getMappedValidator(Validator validator, Class<?> domainType) {
28942889
serializeToJsonSafely(mappedUpdate), collectionName));
28952890
}
28962891

2897-
DocumentCallback<T> callback = getResultReader(EntityProjection.nonProjecting(entityClass), collectionName, resultConverter);
2892+
DocumentCallback<T> callback = getResultReader(EntityProjection.nonProjecting(entityClass), collectionName,
2893+
resultConverter);
28982894

28992895
return executeFindOneInternal(
29002896
new FindAndModifyCallback(collectionPreparer, mappedQuery, fields, sort, mappedUpdate,
@@ -2919,8 +2915,7 @@ Document getMappedValidator(Validator validator, Class<?> domainType) {
29192915
*/
29202916
@Nullable
29212917
protected <S, T> T doFindAndReplace(CollectionPreparer<MongoCollection<Document>> collectionPreparer,
2922-
String collectionName,
2923-
Document mappedQuery, Document mappedFields, Document mappedSort,
2918+
String collectionName, Document mappedQuery, Document mappedFields, Document mappedSort,
29242919
com.mongodb.client.model.@Nullable Collation collation, Class<S> entityType, Document replacement,
29252920
FindAndReplaceOptions options, Class<T> resultType) {
29262921

@@ -2963,10 +2958,10 @@ CollectionPreparer<MongoCollection<Document>> createCollectionPreparer(Query que
29632958
*/
29642959
@Nullable
29652960
private <S, T, R> R doFindAndReplace(CollectionPreparer<MongoCollection<Document>> collectionPreparer,
2966-
String collectionName,
2967-
Document mappedQuery, Document mappedFields, Document mappedSort,
2961+
String collectionName, Document mappedQuery, Document mappedFields, Document mappedSort,
29682962
com.mongodb.client.model.@Nullable Collation collation, Class<T> entityType, Document replacement,
2969-
FindAndReplaceOptions options, EntityProjection<S, T> projection, QueryResultConverter<? super S, ? extends R> resultConverter) {
2963+
FindAndReplaceOptions options, EntityProjection<S, T> projection,
2964+
QueryResultConverter<? super S, ? extends R> resultConverter) {
29702965

29712966
if (LOGGER.isDebugEnabled()) {
29722967
LOGGER
@@ -2979,8 +2974,7 @@ private <S, T, R> R doFindAndReplace(CollectionPreparer<MongoCollection<Document
29792974

29802975
DocumentCallback<R> callback = getResultReader(projection, collectionName, resultConverter);
29812976
return executeFindOneInternal(new FindAndReplaceCallback(collectionPreparer, mappedQuery, mappedFields, mappedSort,
2982-
replacement, collation, options),callback,
2983-
collectionName);
2977+
replacement, collation, options), callback, collectionName);
29842978
}
29852979

29862980
@SuppressWarnings("NullAway")
@@ -3158,8 +3152,8 @@ private Document getMappedSortObject(@Nullable Query query, Class<?> type) {
31583152
return getMappedSortObject(sortObject, mappingContext.getPersistentEntity(type));
31593153
}
31603154

3161-
3162-
private @Nullable Document getMappedSortObject(@Nullable Document sortObject, @Nullable MongoPersistentEntity<?> entity) {
3155+
private @Nullable Document getMappedSortObject(@Nullable Document sortObject,
3156+
@Nullable MongoPersistentEntity<?> entity) {
31633157

31643158
if (ObjectUtils.isEmpty(sortObject)) {
31653159
return null;
@@ -3392,7 +3386,7 @@ private static class FindAndReplaceCallback implements CollectionCallback<Docume
33923386
private final Document fields;
33933387
private final Document sort;
33943388
private final Document update;
3395-
private final com.mongodb.client.model.@Nullable Collation collation;
3389+
private final com.mongodb.client.model.@Nullable Collation collation;
33963390
private final FindAndReplaceOptions options;
33973391

33983392
FindAndReplaceCallback(CollectionPreparer<MongoCollection<Document>> collectionPreparer, Document query,

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/QueryOperations.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,7 @@ Document getMappedFields(@Nullable MongoPersistentEntity<?> entity, EntityProjec
377377
mappedFields = queryMapper.getMappedFields(fields, entity);
378378
} else {
379379
mappedFields = propertyOperations.computeMappedFieldsForProjection(projection, fields);
380-
if(projection.getMappedType().getType().isInterface()) {
380+
if (projection.getMappedType().getType().isInterface()) {
381381
mappedFields = queryMapper.getMappedFields(mappedFields, entity);
382382
}
383383
mappedFields = queryMapper.addMetaAttributes(mappedFields, entity);

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/aot/AggregationBlocks.java

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,19 @@
2222

2323
import org.bson.Document;
2424
import org.jspecify.annotations.NullUnmarked;
25+
2526
import org.springframework.core.ResolvableType;
2627
import org.springframework.core.annotation.MergedAnnotation;
2728
import org.springframework.data.domain.SliceImpl;
2829
import org.springframework.data.domain.Sort.Order;
30+
import org.springframework.data.mapping.model.SimpleTypeHolder;
2931
import org.springframework.data.mongodb.core.MongoOperations;
3032
import org.springframework.data.mongodb.core.aggregation.Aggregation;
3133
import org.springframework.data.mongodb.core.aggregation.AggregationOperation;
3234
import org.springframework.data.mongodb.core.aggregation.AggregationOptions;
3335
import org.springframework.data.mongodb.core.aggregation.AggregationPipeline;
3436
import org.springframework.data.mongodb.core.aggregation.AggregationResults;
3537
import org.springframework.data.mongodb.core.aggregation.TypedAggregation;
36-
import org.springframework.data.mongodb.core.mapping.MongoSimpleTypes;
3738
import org.springframework.data.mongodb.core.query.Collation;
3839
import org.springframework.data.mongodb.repository.Hint;
3940
import org.springframework.data.mongodb.repository.ReadPreference;
@@ -58,12 +59,15 @@ class AggregationBlocks {
5859
static class AggregationExecutionCodeBlockBuilder {
5960

6061
private final AotQueryMethodGenerationContext context;
62+
private final SimpleTypeHolder simpleTypeHolder;
6163
private final MongoQueryMethod queryMethod;
6264
private String aggregationVariableName;
6365

64-
AggregationExecutionCodeBlockBuilder(AotQueryMethodGenerationContext context, MongoQueryMethod queryMethod) {
66+
AggregationExecutionCodeBlockBuilder(AotQueryMethodGenerationContext context, SimpleTypeHolder simpleTypeHolder,
67+
MongoQueryMethod queryMethod) {
6568

6669
this.context = context;
70+
this.simpleTypeHolder = simpleTypeHolder;
6771
this.queryMethod = queryMethod;
6872
}
6973

@@ -80,7 +84,7 @@ CodeBlock build() {
8084

8185
builder.add("\n");
8286

83-
Class<?> outputType = getOutputType(queryMethod);
87+
Class<?> outputType = getOutputType(simpleTypeHolder, queryMethod);
8488

8589
if (ReflectionUtils.isVoid(queryMethod.getReturnedObjectType())) {
8690
builder.addStatement("$L.aggregate($L, $T.class)", mongoOpsRef, aggregationVariableName, outputType);
@@ -152,20 +156,27 @@ CodeBlock build() {
152156

153157
}
154158

155-
private static Class<?> getOutputType(MongoQueryMethod queryMethod) {
159+
private static Class<?> getOutputType(SimpleTypeHolder simpleTypeHolder, MongoQueryMethod queryMethod) {
160+
156161
Class<?> outputType = queryMethod.getReturnedObjectType();
157-
if (MongoSimpleTypes.HOLDER.isSimpleType(outputType)) {
158-
outputType = Document.class;
159-
} else if (ClassUtils.isAssignable(AggregationResults.class, outputType) && queryMethod.getReturnType().getComponentType() != null) {
160-
outputType = queryMethod.getReturnType().getComponentType().getType();
162+
163+
if (simpleTypeHolder.isSimpleType(outputType)) {
164+
return Document.class;
161165
}
166+
167+
if (ClassUtils.isAssignable(AggregationResults.class, outputType)
168+
&& queryMethod.getReturnType().getComponentType() != null) {
169+
return queryMethod.getReturnType().getComponentType().getType();
170+
}
171+
162172
return outputType;
163173
}
164174

165175
@NullUnmarked
166176
static class AggregationCodeBlockBuilder {
167177

168178
private final AotQueryMethodGenerationContext context;
179+
private final SimpleTypeHolder simpleTypeHolder;
169180
private final MongoQueryMethod queryMethod;
170181
private final String parameterNames;
171182

@@ -174,9 +185,11 @@ static class AggregationCodeBlockBuilder {
174185
private String aggregationVariableName;
175186
private boolean pipelineOnly;
176187

177-
AggregationCodeBlockBuilder(AotQueryMethodGenerationContext context, MongoQueryMethod queryMethod) {
188+
AggregationCodeBlockBuilder(AotQueryMethodGenerationContext context, SimpleTypeHolder simpleTypeHolder,
189+
MongoQueryMethod queryMethod) {
178190

179191
this.context = context;
192+
this.simpleTypeHolder = simpleTypeHolder;
180193
this.queryMethod = queryMethod;
181194
this.parameterNames = StringUtils.collectionToDelimitedString(context.getAllParameterNames(), ", ");
182195
}
@@ -230,7 +243,7 @@ private CodeBlock pipeline(String pipelineVariableName) {
230243
builder.add(aggregationStages(context.localVariable("stages"), source.stages()));
231244

232245
if (StringUtils.hasText(sortParameter)) {
233-
Class<?> outputType = getOutputType(queryMethod);
246+
Class<?> outputType = getOutputType(simpleTypeHolder, queryMethod);
234247
builder.add(sortingStage(sortParameter, outputType));
235248
}
236249

@@ -324,7 +337,7 @@ private CodeBlock sortingStage(String sortProvider, Class<?> outputType) {
324337
context.localVariable("sortDocument"), context.localVariable("order"));
325338
builder.endControlFlow();
326339

327-
if (outputType == Document.class || MongoSimpleTypes.HOLDER.isSimpleType(outputType)
340+
if (outputType == Document.class || simpleTypeHolder.isSimpleType(outputType)
328341
|| ClassUtils.isAssignable(context.getRepositoryInformation().getDomainType(), outputType)) {
329342
builder.addStatement("$L.add(new $T($S, $L))", context.localVariable("stages"), Document.class, "$sort",
330343
context.localVariable("sortDocument"));
@@ -343,7 +356,7 @@ private CodeBlock pagingStage(String pageableProvider, boolean slice) {
343356

344357
Builder builder = CodeBlock.builder();
345358

346-
builder.add(sortingStage(pageableProvider + ".getSort()", getOutputType(queryMethod)));
359+
builder.add(sortingStage(pageableProvider + ".getSort()", getOutputType(simpleTypeHolder, queryMethod)));
347360

348361
builder.beginControlFlow("if ($L.isPaged())", pageableProvider);
349362
builder.beginControlFlow("if ($L.getOffset() > 0)", pageableProvider);

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/aot/AotPlaceholders.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,8 @@ static class RegexPlaceholder implements Placeholder {
279279
this.options = options;
280280
}
281281

282-
@Nullable String regexOptions() {
282+
@Nullable
283+
String regexOptions() {
283284
return options;
284285
}
285286

0 commit comments

Comments
 (0)