Skip to content

Commit fb4ef82

Browse files
switch arguments to code blocks
1 parent 17fc184 commit fb4ef82

File tree

1 file changed

+37
-17
lines changed
  • spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/aot

1 file changed

+37
-17
lines changed

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

Lines changed: 37 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,16 @@
1616
package org.springframework.data.mongodb.repository.aot;
1717

1818
import java.util.ArrayList;
19+
import java.util.Iterator;
1920
import java.util.List;
2021
import java.util.Optional;
2122
import java.util.regex.Pattern;
23+
import java.util.stream.Collectors;
2224
import java.util.stream.Stream;
2325

2426
import org.bson.Document;
2527
import org.jspecify.annotations.NullUnmarked;
2628
import org.jspecify.annotations.Nullable;
27-
2829
import org.springframework.core.annotation.MergedAnnotation;
2930
import org.springframework.data.domain.SliceImpl;
3031
import org.springframework.data.domain.Sort.Order;
@@ -470,14 +471,14 @@ static class AggregationCodeBlockBuilder {
470471
private final MongoQueryMethod queryMethod;
471472

472473
private AggregationInteraction source;
473-
private final List<String> arguments;
474+
private final List<CodeBlock> arguments;
474475
private String aggregationVariableName;
475476
private boolean pipelineOnly;
476477

477478
AggregationCodeBlockBuilder(AotQueryMethodGenerationContext context, MongoQueryMethod queryMethod) {
478479

479480
this.context = context;
480-
this.arguments = context.getBindableParameterNames();
481+
this.arguments = context.getBindableParameterNames().stream().map(CodeBlock::of).collect(Collectors.toList());
481482
this.queryMethod = queryMethod;
482483
}
483484

@@ -605,7 +606,7 @@ private CodeBlock aggregationOptions(String aggregationVariableName) {
605606
}
606607

607608
private CodeBlock aggregationStages(String stageListVariableName, Iterable<String> stages, int stageCount,
608-
List<String> arguments) {
609+
List<CodeBlock> arguments) {
609610

610611
Builder builder = CodeBlock.builder();
611612
builder.addStatement("$T<$T> $L = new $T($L)", List.class, Object.class, stageListVariableName, ArrayList.class,
@@ -682,20 +683,22 @@ static class QueryCodeBlockBuilder {
682683
private final MongoQueryMethod queryMethod;
683684

684685
private QueryInteraction source;
685-
private final List<String> arguments;
686+
private final List<CodeBlock> arguments;
686687
private String queryVariableName;
687688

688689
QueryCodeBlockBuilder(AotQueryMethodGenerationContext context, MongoQueryMethod queryMethod) {
689690

690691
this.context = context;
691-
this.arguments = new ArrayList<>();
692692

693-
for(MongoParameter parameter : queryMethod.getParameters().getBindableParameters()) {
693+
this.arguments = new ArrayList<>();
694+
for (MongoParameter parameter : queryMethod.getParameters().getBindableParameters()) {
694695
String parameterName = context.getParameterName(parameter.getIndex());
695-
if(ClassUtils.isAssignable(Circle.class, parameter.getType())) {
696-
parameterName = "List.of(%s.getCenter(), %s.getRadius().getNormalizedValue())".formatted(parameterName, parameterName);
696+
if (ClassUtils.isAssignable(Circle.class, parameter.getType())) {
697+
arguments.add(CodeBlock.builder().add("$T.of($L.getCenter(), $L.getRadius().getNormalizedValue())",
698+
List.class, parameterName, parameterName).build());
699+
} else {
700+
arguments.add(CodeBlock.of(parameterName));
697701
}
698-
arguments.add(parameterName);
699702
}
700703

701704
this.queryMethod = queryMethod;
@@ -797,8 +800,15 @@ private CodeBlock renderExpressionToQuery(@Nullable String source, String variab
797800
builder.addStatement("$T $L = new $T($T.parse($S))", BasicQuery.class, variableName, BasicQuery.class,
798801
Document.class, source);
799802
} else {
800-
builder.addStatement("$T $L = createQuery($S, new $T[]{ $L })", BasicQuery.class, variableName, source,
801-
Object.class, StringUtils.collectionToDelimitedString(arguments, ", "));
803+
builder.add("$T $L = createQuery($S, new $T[]{ ", BasicQuery.class, variableName, source, Object.class);
804+
Iterator<CodeBlock> iterator = arguments.iterator();
805+
while (iterator.hasNext()) {
806+
builder.add(iterator.next());
807+
if (iterator.hasNext()) {
808+
builder.add(", ");
809+
}
810+
}
811+
builder.add("});\n");
802812
}
803813

804814
return builder.build();
@@ -809,11 +819,11 @@ private CodeBlock renderExpressionToQuery(@Nullable String source, String variab
809819
static class UpdateCodeBlockBuilder {
810820

811821
private UpdateInteraction source;
812-
private List<String> arguments;
822+
private List<CodeBlock> arguments;
813823
private String updateVariableName;
814824

815825
public UpdateCodeBlockBuilder(AotQueryMethodGenerationContext context, MongoQueryMethod queryMethod) {
816-
this.arguments = context.getBindableParameterNames();
826+
this.arguments = context.getBindableParameterNames().stream().map(CodeBlock::of).collect(Collectors.toList());
817827
}
818828

819829
public UpdateCodeBlockBuilder update(UpdateInteraction update) {
@@ -841,16 +851,26 @@ CodeBlock build() {
841851
}
842852

843853
private static CodeBlock renderExpressionToDocument(@Nullable String source, String variableName,
844-
List<String> arguments) {
854+
List<CodeBlock> arguments) {
845855

846856
Builder builder = CodeBlock.builder();
847857
if (!StringUtils.hasText(source)) {
848858
builder.addStatement("$T $L = new $T()", Document.class, variableName, Document.class);
849859
} else if (!containsPlaceholder(source)) {
850860
builder.addStatement("$T $L = $T.parse($S)", Document.class, variableName, Document.class, source);
851861
} else {
852-
builder.addStatement("$T $L = bindParameters($S, new $T[]{ $L })", Document.class, variableName, source,
853-
Object.class, StringUtils.collectionToDelimitedString(arguments, ", "));
862+
863+
builder.add("$T $L = bindParameters($S, new $T[]{ ", Document.class, variableName, source, Object.class);
864+
Iterator<CodeBlock> iterator = arguments.iterator();
865+
while (iterator.hasNext()) {
866+
builder.add(iterator.next());
867+
if (iterator.hasNext()) {
868+
builder.add(", ");
869+
}
870+
}
871+
builder.add("});\n");
872+
// builder.addStatement("$T $L = bindParameters($S, new $T[]{ $L })", Document.class, variableName, source,
873+
// Object.class, StringUtils.collectionToDelimitedString(arguments, ", "));
854874
}
855875
return builder.build();
856876
}

0 commit comments

Comments
 (0)