16
16
package org .springframework .data .mongodb .repository .aot ;
17
17
18
18
import java .util .ArrayList ;
19
+ import java .util .Iterator ;
19
20
import java .util .List ;
20
21
import java .util .Optional ;
21
22
import java .util .regex .Pattern ;
23
+ import java .util .stream .Collectors ;
22
24
import java .util .stream .Stream ;
23
25
24
26
import org .bson .Document ;
25
27
import org .jspecify .annotations .NullUnmarked ;
26
28
import org .jspecify .annotations .Nullable ;
27
-
28
29
import org .springframework .core .annotation .MergedAnnotation ;
29
30
import org .springframework .data .domain .SliceImpl ;
30
31
import org .springframework .data .domain .Sort .Order ;
@@ -470,14 +471,14 @@ static class AggregationCodeBlockBuilder {
470
471
private final MongoQueryMethod queryMethod ;
471
472
472
473
private AggregationInteraction source ;
473
- private final List <String > arguments ;
474
+ private final List <CodeBlock > arguments ;
474
475
private String aggregationVariableName ;
475
476
private boolean pipelineOnly ;
476
477
477
478
AggregationCodeBlockBuilder (AotQueryMethodGenerationContext context , MongoQueryMethod queryMethod ) {
478
479
479
480
this .context = context ;
480
- this .arguments = context .getBindableParameterNames ();
481
+ this .arguments = context .getBindableParameterNames (). stream (). map ( CodeBlock :: of ). collect ( Collectors . toList ()) ;
481
482
this .queryMethod = queryMethod ;
482
483
}
483
484
@@ -605,7 +606,7 @@ private CodeBlock aggregationOptions(String aggregationVariableName) {
605
606
}
606
607
607
608
private CodeBlock aggregationStages (String stageListVariableName , Iterable <String > stages , int stageCount ,
608
- List <String > arguments ) {
609
+ List <CodeBlock > arguments ) {
609
610
610
611
Builder builder = CodeBlock .builder ();
611
612
builder .addStatement ("$T<$T> $L = new $T($L)" , List .class , Object .class , stageListVariableName , ArrayList .class ,
@@ -682,20 +683,22 @@ static class QueryCodeBlockBuilder {
682
683
private final MongoQueryMethod queryMethod ;
683
684
684
685
private QueryInteraction source ;
685
- private final List <String > arguments ;
686
+ private final List <CodeBlock > arguments ;
686
687
private String queryVariableName ;
687
688
688
689
QueryCodeBlockBuilder (AotQueryMethodGenerationContext context , MongoQueryMethod queryMethod ) {
689
690
690
691
this .context = context ;
691
- this .arguments = new ArrayList <>();
692
692
693
- for (MongoParameter parameter : queryMethod .getParameters ().getBindableParameters ()) {
693
+ this .arguments = new ArrayList <>();
694
+ for (MongoParameter parameter : queryMethod .getParameters ().getBindableParameters ()) {
694
695
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 ));
697
701
}
698
- arguments .add (parameterName );
699
702
}
700
703
701
704
this .queryMethod = queryMethod ;
@@ -797,8 +800,15 @@ private CodeBlock renderExpressionToQuery(@Nullable String source, String variab
797
800
builder .addStatement ("$T $L = new $T($T.parse($S))" , BasicQuery .class , variableName , BasicQuery .class ,
798
801
Document .class , source );
799
802
} 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 " );
802
812
}
803
813
804
814
return builder .build ();
@@ -809,11 +819,11 @@ private CodeBlock renderExpressionToQuery(@Nullable String source, String variab
809
819
static class UpdateCodeBlockBuilder {
810
820
811
821
private UpdateInteraction source ;
812
- private List <String > arguments ;
822
+ private List <CodeBlock > arguments ;
813
823
private String updateVariableName ;
814
824
815
825
public UpdateCodeBlockBuilder (AotQueryMethodGenerationContext context , MongoQueryMethod queryMethod ) {
816
- this .arguments = context .getBindableParameterNames ();
826
+ this .arguments = context .getBindableParameterNames (). stream (). map ( CodeBlock :: of ). collect ( Collectors . toList ()) ;
817
827
}
818
828
819
829
public UpdateCodeBlockBuilder update (UpdateInteraction update ) {
@@ -841,16 +851,26 @@ CodeBlock build() {
841
851
}
842
852
843
853
private static CodeBlock renderExpressionToDocument (@ Nullable String source , String variableName ,
844
- List <String > arguments ) {
854
+ List <CodeBlock > arguments ) {
845
855
846
856
Builder builder = CodeBlock .builder ();
847
857
if (!StringUtils .hasText (source )) {
848
858
builder .addStatement ("$T $L = new $T()" , Document .class , variableName , Document .class );
849
859
} else if (!containsPlaceholder (source )) {
850
860
builder .addStatement ("$T $L = $T.parse($S)" , Document .class , variableName , Document .class , source );
851
861
} 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, ", "));
854
874
}
855
875
return builder .build ();
856
876
}
0 commit comments