Skip to content

Commit d76f016

Browse files
committed
Use LocalVariableNameFactory to avoid parameter name clashes.
Closes #4965
1 parent 9893203 commit d76f016

File tree

6 files changed

+219
-220
lines changed

6 files changed

+219
-220
lines changed

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

+3
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,10 @@
3535
import org.springframework.util.ObjectUtils;
3636

3737
/**
38+
* Support class for MongoDB AOT repository fragments.
39+
*
3840
* @author Christoph Strobl
41+
* @since 5.0
3942
*/
4043
public class MongoAotRepositoryFragmentSupport {
4144

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

+82-70
Original file line numberDiff line numberDiff line change
@@ -246,20 +246,24 @@ CodeBlock build() {
246246
builder.add("\n");
247247

248248
String updateReference = updateVariableName;
249-
builder.addStatement("$T<$T> updater = $L.update($T.class)", ExecutableUpdate.class,
250-
context.getRepositoryInformation().getDomainType(), mongoOpsRef,
251-
context.getRepositoryInformation().getDomainType());
249+
Class<?> domainType = context.getRepositoryInformation().getDomainType();
250+
builder.addStatement("$T<$T> $L = $L.update($T.class)", ExecutableUpdate.class, domainType,
251+
context.localVariable("updater"), mongoOpsRef, domainType);
252252

253253
Class<?> returnType = ClassUtils.resolvePrimitiveIfNecessary(queryMethod.getReturnedObjectType());
254254
if (ReflectionUtils.isVoid(returnType)) {
255-
builder.addStatement("updater.matching($L).apply($L).all()", queryVariableName, updateReference);
255+
builder.addStatement("$L.matching($L).apply($L).all()", context.localVariable("updater"), queryVariableName,
256+
updateReference);
256257
} else if (ClassUtils.isAssignable(Long.class, returnType)) {
257-
builder.addStatement("return updater.matching($L).apply($L).all().getModifiedCount()", queryVariableName,
258+
builder.addStatement("return $L.matching($L).apply($L).all().getModifiedCount()",
259+
context.localVariable("updater"), queryVariableName,
258260
updateReference);
259261
} else {
260-
builder.addStatement("$T modifiedCount = updater.matching($L).apply($L).all().getModifiedCount()", Long.class,
262+
builder.addStatement("$T $L = $L.matching($L).apply($L).all().getModifiedCount()", Long.class,
263+
context.localVariable("modifiedCount"), context.localVariable("updater"),
261264
queryVariableName, updateReference);
262-
builder.addStatement("return $T.convertNumberToTargetClass(modifiedCount, $T.class)", NumberUtils.class,
265+
builder.addStatement("return $T.convertNumberToTargetClass($L, $T.class)", NumberUtils.class,
266+
context.localVariable("modifiedCount"),
263267
returnType);
264268
}
265269

@@ -314,24 +318,29 @@ CodeBlock build() {
314318

315319
Class<?> returnType = ClassUtils.resolvePrimitiveIfNecessary(queryMethod.getReturnedObjectType());
316320

317-
builder.addStatement("$T results = $L.aggregate($L, $T.class)", AggregationResults.class, mongoOpsRef,
321+
builder.addStatement("$T $L = $L.aggregate($L, $T.class)", AggregationResults.class,
322+
context.localVariable("results"), mongoOpsRef,
318323
aggregationVariableName, outputType);
319324
if (!queryMethod.isCollectionQuery()) {
320325
builder.addStatement(
321-
"return $T.<$T>firstElement(convertSimpleRawResults($T.class, results.getMappedResults()))",
322-
CollectionUtils.class, returnType, returnType);
326+
"return $T.<$T>firstElement(convertSimpleRawResults($T.class, $L.getMappedResults()))",
327+
CollectionUtils.class, returnType, returnType, context.localVariable("results"));
323328
} else {
324-
builder.addStatement("return convertSimpleRawResults($T.class, results.getMappedResults())", returnType);
329+
builder.addStatement("return convertSimpleRawResults($T.class, $L.getMappedResults())", returnType,
330+
context.localVariable("results"));
325331
}
326332
} else {
327333
if (queryMethod.isSliceQuery()) {
328-
builder.addStatement("$T results = $L.aggregate($L, $T.class)", AggregationResults.class, mongoOpsRef,
334+
builder.addStatement("$T $L = $L.aggregate($L, $T.class)", AggregationResults.class,
335+
context.localVariable("results"), mongoOpsRef,
329336
aggregationVariableName, outputType);
330-
builder.addStatement("boolean hasNext = results.getMappedResults().size() > $L.getPageSize()",
331-
context.getPageableParameterName());
337+
builder.addStatement("boolean $L = $L.getMappedResults().size() > $L.getPageSize()",
338+
context.localVariable("hasNext"), context.localVariable("results"), context.getPageableParameterName());
332339
builder.addStatement(
333-
"return new $T<>(hasNext ? results.getMappedResults().subList(0, $L.getPageSize()) : results.getMappedResults(), $L, hasNext)",
334-
SliceImpl.class, context.getPageableParameterName(), context.getPageableParameterName());
340+
"return new $T<>($L ? $L.getMappedResults().subList(0, $L.getPageSize()) : $L.getMappedResults(), $L, $L)",
341+
SliceImpl.class, context.localVariable("hasNext"), context.localVariable("results"),
342+
context.getPageableParameterName(), context.localVariable("results"), context.getPageableParameterName(),
343+
context.localVariable("hasNext"));
335344
} else {
336345
builder.addStatement("return $L.aggregate($L, $T.class).getMappedResults()", mongoOpsRef,
337346
aggregationVariableName, outputType);
@@ -368,18 +377,19 @@ CodeBlock build() {
368377
Builder builder = CodeBlock.builder();
369378

370379
boolean isProjecting = context.getReturnedType().isProjecting();
380+
Class<?> domainType = context.getRepositoryInformation().getDomainType();
371381
Object actualReturnType = isProjecting ? context.getActualReturnType().getType()
372-
: context.getRepositoryInformation().getDomainType();
382+
: domainType;
373383

374384
builder.add("\n");
375385

376386
if (isProjecting) {
377-
builder.addStatement("$T<$T> finder = $L.query($T.class).as($T.class)", FindWithQuery.class, actualReturnType,
378-
mongoOpsRef, context.getRepositoryInformation().getDomainType(), actualReturnType);
387+
builder.addStatement("$T<$T> $L = $L.query($T.class).as($T.class)", FindWithQuery.class, actualReturnType,
388+
context.localVariable("finder"), mongoOpsRef, domainType, actualReturnType);
379389
} else {
380390

381-
builder.addStatement("$T<$T> finder = $L.query($T.class)", FindWithQuery.class, actualReturnType, mongoOpsRef,
382-
context.getRepositoryInformation().getDomainType());
391+
builder.addStatement("$T<$T> $L = $L.query($T.class)", FindWithQuery.class, actualReturnType,
392+
context.localVariable("finder"), mongoOpsRef, domainType);
383393
}
384394

385395
String terminatingMethod;
@@ -395,13 +405,14 @@ CodeBlock build() {
395405
}
396406

397407
if (queryMethod.isPageQuery()) {
398-
builder.addStatement("return new $T(finder, $L).execute($L)", PagedExecution.class,
408+
builder.addStatement("return new $T($L, $L).execute($L)", PagedExecution.class, context.localVariable("finder"),
399409
context.getPageableParameterName(), query.name());
400410
} else if (queryMethod.isSliceQuery()) {
401-
builder.addStatement("return new $T(finder, $L).execute($L)", SlicedExecution.class,
402-
context.getPageableParameterName(), query.name());
411+
builder.addStatement("return new $T($L, $L).execute($L)", SlicedExecution.class,
412+
context.localVariable("finder"), context.getPageableParameterName(), query.name());
403413
} else {
404-
builder.addStatement("return finder.matching($L).$L", query.name(), terminatingMethod);
414+
builder.addStatement("return $L.matching($L).$L", context.localVariable("finder"), query.name(),
415+
terminatingMethod);
405416
}
406417

407418
return builder.build();
@@ -415,7 +426,7 @@ static class AggregationCodeBlockBuilder {
415426
private final MongoQueryMethod queryMethod;
416427

417428
private AggregationInteraction source;
418-
private List<String> arguments;
429+
private final List<String> arguments;
419430
private String aggregationVariableName;
420431
private boolean pipelineOnly;
421432

@@ -449,7 +460,7 @@ CodeBlock build() {
449460
CodeBlock.Builder builder = CodeBlock.builder();
450461
builder.add("\n");
451462

452-
String pipelineName = aggregationVariableName + (pipelineOnly ? "" : "Pipeline");
463+
String pipelineName = context.localVariable(aggregationVariableName + (pipelineOnly ? "" : "Pipeline"));
453464
builder.add(pipeline(pipelineName));
454465

455466
if (!pipelineOnly) {
@@ -486,8 +497,7 @@ private CodeBlock pipeline(String pipelineVariableName) {
486497
}
487498

488499
Builder builder = CodeBlock.builder();
489-
String stagesVariableName = "stages";
490-
builder.add(aggregationStages(stagesVariableName, source.stages(), stageCount, arguments));
500+
builder.add(aggregationStages(context.localVariable("stages"), source.stages(), stageCount, arguments));
491501

492502
if (mightBeSorted) {
493503
builder.add(sortingStage(sortParameter));
@@ -502,7 +512,7 @@ private CodeBlock pipeline(String pipelineVariableName) {
502512
}
503513

504514
builder.addStatement("$T $L = createPipeline($L)", AggregationPipeline.class, pipelineVariableName,
505-
stagesVariableName);
515+
context.localVariable("stages"));
506516
return builder.build();
507517
}
508518

@@ -533,7 +543,8 @@ private CodeBlock aggregationOptions(String aggregationVariableName) {
533543
if (!options.isEmpty()) {
534544

535545
Builder optionsBuilder = CodeBlock.builder();
536-
optionsBuilder.add("$T aggregationOptions = $T.builder()\n", AggregationOptions.class,
546+
optionsBuilder.add("$T $L = $T.builder()\n", AggregationOptions.class,
547+
context.localVariable("aggregationOptions"),
537548
AggregationOptions.class);
538549
optionsBuilder.indent();
539550
for (CodeBlock optionBlock : options) {
@@ -544,67 +555,81 @@ private CodeBlock aggregationOptions(String aggregationVariableName) {
544555
optionsBuilder.unindent();
545556
builder.add(optionsBuilder.build());
546557

547-
builder.addStatement("$L = $L.withOptions(aggregationOptions)", aggregationVariableName,
548-
aggregationVariableName);
558+
builder.addStatement("$L = $L.withOptions($L)", aggregationVariableName, aggregationVariableName,
559+
context.localVariable("aggregationOptions"));
549560
}
550561
return builder.build();
551562
}
552563

553-
private static CodeBlock aggregationStages(String stageListVariableName, Iterable<String> stages, int stageCount,
564+
private CodeBlock aggregationStages(String stageListVariableName, Iterable<String> stages, int stageCount,
554565
List<String> arguments) {
555566

556567
Builder builder = CodeBlock.builder();
557568
builder.addStatement("$T<$T> $L = new $T($L)", List.class, Object.class, stageListVariableName, ArrayList.class,
558569
stageCount);
559570
int stageCounter = 0;
571+
560572
for (String stage : stages) {
561-
String stageName = "stage_%s".formatted(stageCounter++);
573+
String stageName = context.localVariable("stage_%s".formatted(stageCounter++));
562574
builder.add(renderExpressionToDocument(stage, stageName, arguments));
563-
builder.addStatement("stages.add($L)", stageName);
575+
builder.addStatement("$L.add($L)", context.localVariable("stages"), stageName);
564576
}
577+
565578
return builder.build();
566579
}
567580

568-
private static CodeBlock sortingStage(String sortProvider) {
581+
private CodeBlock sortingStage(String sortProvider) {
569582

570583
Builder builder = CodeBlock.builder();
571-
builder.beginControlFlow("if($L.isSorted())", sortProvider);
572-
builder.addStatement("$T sortDocument = new $T()", Document.class, Document.class);
573-
builder.beginControlFlow("for ($T order : $L)", Order.class, sortProvider);
574-
builder.addStatement("sortDocument.append(order.getProperty(), order.isAscending() ? 1 : -1);");
584+
585+
builder.beginControlFlow("if ($L.isSorted())", sortProvider);
586+
builder.addStatement("$T $L = new $T()", Document.class, context.localVariable("sortDocument"), Document.class);
587+
builder.beginControlFlow("for ($T $L : $L)", Order.class, context.localVariable("order"), sortProvider);
588+
builder.addStatement("$L.append($L.getProperty(), $L.isAscending() ? 1 : -1);",
589+
context.localVariable("sortDocument"), context.localVariable("order"), context.localVariable("order"));
575590
builder.endControlFlow();
576-
builder.addStatement("stages.add(new $T($S, sortDocument))", Document.class, "$sort");
591+
builder.addStatement("stages.add(new $T($S, $L))", Document.class, "$sort",
592+
context.localVariable("sortDocument"));
577593
builder.endControlFlow();
594+
578595
return builder.build();
579596
}
580597

581-
private static CodeBlock pagingStage(String pageableProvider, boolean slice) {
598+
private CodeBlock pagingStage(String pageableProvider, boolean slice) {
582599

583600
Builder builder = CodeBlock.builder();
601+
584602
builder.add(sortingStage(pageableProvider + ".getSort()"));
585603

586-
builder.beginControlFlow("if($L.isPaged())", pageableProvider);
587-
builder.beginControlFlow("if($L.getOffset() > 0)", pageableProvider);
588-
builder.addStatement("stages.add($T.skip($L.getOffset()))", Aggregation.class, pageableProvider);
604+
builder.beginControlFlow("if ($L.isPaged())", pageableProvider);
605+
builder.beginControlFlow("if ($L.getOffset() > 0)", pageableProvider);
606+
builder.addStatement("$L.add($T.skip($L.getOffset()))", context.localVariable("stages"), Aggregation.class,
607+
pageableProvider);
589608
builder.endControlFlow();
590609
if (slice) {
591-
builder.addStatement("stages.add($T.limit($L.getPageSize() + 1))", Aggregation.class, pageableProvider);
610+
builder.addStatement("$L.add($T.limit($L.getPageSize() + 1))", context.localVariable("stages"),
611+
Aggregation.class, pageableProvider);
592612
} else {
593-
builder.addStatement("stages.add($T.limit($L.getPageSize()))", Aggregation.class, pageableProvider);
613+
builder.addStatement("$L.add($T.limit($L.getPageSize()))", context.localVariable("stages"), Aggregation.class,
614+
pageableProvider);
594615
}
595616
builder.endControlFlow();
596617

597618
return builder.build();
598619
}
599620

600-
private static CodeBlock limitingStage(String limitProvider) {
621+
private CodeBlock limitingStage(String limitProvider) {
601622

602623
Builder builder = CodeBlock.builder();
603-
builder.beginControlFlow("if($L.isLimited())", limitProvider);
604-
builder.addStatement("stages.add($T.limit($L.max()))", Aggregation.class, limitProvider);
624+
625+
builder.beginControlFlow("if ($L.isLimited())", limitProvider);
626+
builder.addStatement("$L.add($T.limit($L.max()))", context.localVariable("stages"), Aggregation.class,
627+
limitProvider);
605628
builder.endControlFlow();
629+
606630
return builder.build();
607631
}
632+
608633
}
609634

610635
@NullUnmarked
@@ -614,7 +639,7 @@ static class QueryCodeBlockBuilder {
614639
private final MongoQueryMethod queryMethod;
615640

616641
private QueryInteraction source;
617-
private List<String> arguments;
642+
private final List<String> arguments;
618643
private String queryVariableName;
619644

620645
QueryCodeBlockBuilder(AotQueryMethodGenerationContext context, MongoQueryMethod queryMethod) {
@@ -697,17 +722,10 @@ private CodeBlock renderExpressionToQuery(@Nullable String source, String variab
697722
builder.addStatement("$T $L = new $T(new $T())", BasicQuery.class, variableName, BasicQuery.class,
698723
Document.class);
699724
} else if (!containsPlaceholder(source)) {
700-
701-
String tmpVarName = "%sString".formatted(variableName);
702-
builder.addStatement("String $L = $S", tmpVarName, source);
703-
704-
builder.addStatement("$T $L = new $T($T.parse($L))", BasicQuery.class, variableName, BasicQuery.class,
705-
Document.class, tmpVarName);
725+
builder.addStatement("$T $L = new $T($T.parse($S))", BasicQuery.class, variableName, BasicQuery.class,
726+
Document.class, source);
706727
} else {
707-
708-
String tmpVarName = "%sString".formatted(variableName);
709-
builder.addStatement("String $L = $S", tmpVarName, source);
710-
builder.addStatement("$T $L = createQuery($L, new $T[]{ $L })", BasicQuery.class, variableName, tmpVarName,
728+
builder.addStatement("$T $L = createQuery($S, new $T[]{ $L })", BasicQuery.class, variableName, source,
711729
Object.class, StringUtils.collectionToDelimitedString(arguments, ", "));
712730
}
713731

@@ -757,15 +775,9 @@ private static CodeBlock renderExpressionToDocument(@Nullable String source, Str
757775
if (!StringUtils.hasText(source)) {
758776
builder.addStatement("$T $L = new $T()", Document.class, variableName, Document.class);
759777
} else if (!containsPlaceholder(source)) {
760-
761-
String tmpVarName = "%sString".formatted(variableName);
762-
builder.addStatement("String $L = $S", tmpVarName, source);
763-
builder.addStatement("$T $L = $T.parse($L)", Document.class, variableName, Document.class, tmpVarName);
778+
builder.addStatement("$T $L = $T.parse($S)", Document.class, variableName, Document.class, source);
764779
} else {
765-
766-
String tmpVarName = "%sString".formatted(variableName);
767-
builder.addStatement("String $L = $S", tmpVarName, source);
768-
builder.addStatement("$T $L = bindParameters($L, new $T[]{ $L })", Document.class, variableName, tmpVarName,
780+
builder.addStatement("$T $L = bindParameters($S, new $T[]{ $L })", Document.class, variableName, source,
769781
Object.class, StringUtils.collectionToDelimitedString(arguments, ", "));
770782
}
771783
return builder.build();

0 commit comments

Comments
 (0)