Skip to content

Commit 25ae2fa

Browse files
use positional parameters to simplify javapoet statements
1 parent 871bea6 commit 25ae2fa

File tree

1 file changed

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

1 file changed

+37
-43
lines changed

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

Lines changed: 37 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -203,8 +203,8 @@ CodeBlock build() {
203203
Object actualReturnType = isProjecting ? context.getActualReturnType().getType() : domainType;
204204

205205
builder.add("\n");
206-
builder.addStatement("$T<$T> $L = $L.remove($T.class)", ExecutableRemove.class, domainType,
207-
context.localVariable("remover"), mongoOpsRef, domainType);
206+
builder.addStatement("$1T<$2T> $3L = $4L.remove($2T.class)", ExecutableRemove.class, domainType,
207+
context.localVariable("remover"), mongoOpsRef);
208208

209209
DeleteExecution.Type type = DeleteExecution.Type.FIND_AND_REMOVE_ALL;
210210
if (!queryMethod.isCollectionQuery()) {
@@ -270,8 +270,8 @@ CodeBlock build() {
270270

271271
String updateReference = updateVariableName;
272272
Class<?> domainType = context.getRepositoryInformation().getDomainType();
273-
builder.addStatement("$T<$T> $L = $L.update($T.class)", ExecutableUpdate.class, domainType,
274-
context.localVariable("updater"), mongoOpsRef, domainType);
273+
builder.addStatement("$1T<$2T> $3L = $4L.update($2T.class)", ExecutableUpdate.class, domainType,
274+
context.localVariable("updater"), mongoOpsRef);
275275

276276
Class<?> returnType = ClassUtils.resolvePrimitiveIfNecessary(queryMethod.getReturnedObjectType());
277277
if (ReflectionUtils.isVoid(returnType)) {
@@ -344,16 +344,17 @@ CodeBlock build() {
344344
builder.addStatement("$T<$T> $L = $L.aggregateStream($L, $T.class)", Stream.class, Document.class,
345345
context.localVariable("results"), mongoOpsRef, aggregationVariableName, outputType);
346346

347-
builder.addStatement("return $L.map(it -> ($T) convertSimpleRawResult($T.class, it))",
348-
context.localVariable("results"), returnType, returnType);
347+
builder.addStatement("return $1L.map(it -> ($2T) convertSimpleRawResult($2T.class, it))",
348+
context.localVariable("results"), returnType);
349349
} else {
350350

351351
builder.addStatement("$T $L = $L.aggregate($L, $T.class)", AggregationResults.class,
352352
context.localVariable("results"), mongoOpsRef, aggregationVariableName, outputType);
353353

354354
if (!queryMethod.isCollectionQuery()) {
355-
builder.addStatement("return $T.<$T>firstElement(convertSimpleRawResults($T.class, $L.getMappedResults()))",
356-
CollectionUtils.class, returnType, returnType, context.localVariable("results"));
355+
builder.addStatement(
356+
"return $1T.<$2T>firstElement(convertSimpleRawResults($2T.class, $3L.getMappedResults()))",
357+
CollectionUtils.class, returnType, context.localVariable("results"));
357358
} else {
358359
builder.addStatement("return convertSimpleRawResults($T.class, $L.getMappedResults())", returnType,
359360
context.localVariable("results"));
@@ -366,10 +367,9 @@ CodeBlock build() {
366367
builder.addStatement("boolean $L = $L.getMappedResults().size() > $L.getPageSize()",
367368
context.localVariable("hasNext"), context.localVariable("results"), context.getPageableParameterName());
368369
builder.addStatement(
369-
"return new $T<>($L ? $L.getMappedResults().subList(0, $L.getPageSize()) : $L.getMappedResults(), $L, $L)",
370+
"return new $1T<>($2L ? $3L.getMappedResults().subList(0, $4L.getPageSize()) : $3L.getMappedResults(), $4L, $2L)",
370371
SliceImpl.class, context.localVariable("hasNext"), context.localVariable("results"),
371-
context.getPageableParameterName(), context.localVariable("results"), context.getPageableParameterName(),
372-
context.localVariable("hasNext"));
372+
context.getPageableParameterName());
373373
} else {
374374

375375
if (queryMethod.isStreamQuery()) {
@@ -584,9 +584,9 @@ CodeBlock build() {
584584

585585
if (!pipelineOnly) {
586586

587-
builder.addStatement("$T<$T> $L = $T.newAggregation($T.class, $L.getOperations())", TypedAggregation.class,
588-
context.getRepositoryInformation().getDomainType(), aggregationVariableName, Aggregation.class,
589-
context.getRepositoryInformation().getDomainType(), pipelineName);
587+
builder.addStatement("$1T<$2T> $3L = $4T.newAggregation($2T.class, $5L.getOperations())",
588+
TypedAggregation.class, context.getRepositoryInformation().getDomainType(), aggregationVariableName,
589+
Aggregation.class, pipelineName);
590590

591591
builder.add(aggregationOptions(aggregationVariableName));
592592
}
@@ -662,8 +662,8 @@ private CodeBlock aggregationOptions(String aggregationVariableName) {
662662
if (!options.isEmpty()) {
663663

664664
Builder optionsBuilder = CodeBlock.builder();
665-
optionsBuilder.add("$T $L = $T.builder()\n", AggregationOptions.class,
666-
context.localVariable("aggregationOptions"), AggregationOptions.class);
665+
optionsBuilder.add("$1T $2L = $1T.builder()\n", AggregationOptions.class,
666+
context.localVariable("aggregationOptions"));
667667
optionsBuilder.indent();
668668
for (CodeBlock optionBlock : options) {
669669
optionsBuilder.add(optionBlock);
@@ -673,7 +673,7 @@ private CodeBlock aggregationOptions(String aggregationVariableName) {
673673
optionsBuilder.unindent();
674674
builder.add(optionsBuilder.build());
675675

676-
builder.addStatement("$L = $L.withOptions($L)", aggregationVariableName, aggregationVariableName,
676+
builder.addStatement("$1L = $1L.withOptions($2L)", aggregationVariableName,
677677
context.localVariable("aggregationOptions"));
678678
}
679679
return builder.build();
@@ -701,10 +701,10 @@ private CodeBlock sortingStage(String sortProvider) {
701701
Builder builder = CodeBlock.builder();
702702

703703
builder.beginControlFlow("if ($L.isSorted())", sortProvider);
704-
builder.addStatement("$T $L = new $T()", Document.class, context.localVariable("sortDocument"), Document.class);
704+
builder.addStatement("$1T $2L = new $1T()", Document.class, context.localVariable("sortDocument"));
705705
builder.beginControlFlow("for ($T $L : $L)", Order.class, context.localVariable("order"), sortProvider);
706-
builder.addStatement("$L.append($L.getProperty(), $L.isAscending() ? 1 : -1);",
707-
context.localVariable("sortDocument"), context.localVariable("order"), context.localVariable("order"));
706+
builder.addStatement("$1L.append($2L.getProperty(), $2L.isAscending() ? 1 : -1);",
707+
context.localVariable("sortDocument"), context.localVariable("order"));
708708
builder.endControlFlow();
709709
builder.addStatement("stages.add(new $T($S, $L))", Document.class, "$sort",
710710
context.localVariable("sortDocument"));
@@ -768,28 +768,26 @@ static class QueryCodeBlockBuilder {
768768
for (MongoParameter parameter : queryMethod.getParameters().getBindableParameters()) {
769769
String parameterName = context.getParameterName(parameter.getIndex());
770770
if (ClassUtils.isAssignable(Circle.class, parameter.getType())) {
771-
arguments.add(CodeBlock.builder()
772-
.add("$T.of($T.of($L.getCenter().getX(), $L.getCenter().getY()), $L.getRadius().getNormalizedValue())",
773-
List.class, List.class, parameterName, parameterName, parameterName)
774-
.build());
771+
arguments.add(CodeBlock.builder().add(
772+
"$1T.of($1T.of($2L.getCenter().getX(), $2L.getCenter().getY()), $2L.getRadius().getNormalizedValue())",
773+
List.class, parameterName).build());
775774
} else if (ClassUtils.isAssignable(Box.class, parameter.getType())) {
776775

777776
// { $geoWithin: { $box: [ [ <x1>, <y1> ], [ <x2>, <y2> ] ] }
778777
arguments.add(CodeBlock.builder().add(
779-
"$T.of($T.of($L.getFirst().getX(), $L.getFirst().getY()), $T.of($L.getSecond().getX(), $L.getSecond().getY()))",
780-
List.class, List.class, parameterName, parameterName, List.class, parameterName, parameterName).build());
778+
"$1T.of($1T.of($2L.getFirst().getX(), $2L.getFirst().getY()), $1T.of($2L.getSecond().getX(), $2L.getSecond().getY()))",
779+
List.class, parameterName).build());
781780
} else if (ClassUtils.isAssignable(Sphere.class, parameter.getType())) {
782781
// { $centerSphere: [ [ <x>, <y> ], <radius> ] }
783-
arguments.add(CodeBlock.builder()
784-
.add("$T.of($T.of($L.getCenter().getX(), $L.getCenter().getY()), $L.getRadius().getNormalizedValue())",
785-
List.class, List.class, parameterName, parameterName, parameterName)
786-
.build());
782+
arguments.add(CodeBlock.builder().add(
783+
"$1T.of($1T.of($2L.getCenter().getX(), $2L.getCenter().getY()), $2L.getRadius().getNormalizedValue())",
784+
List.class, parameterName).build());
787785
} else if (ClassUtils.isAssignable(Polygon.class, parameter.getType())) {
788786
// $polygon: [ [ <x1> , <y1> ], [ <x2> , <y2> ], [ <x3> , <y3> ], ... ]
789787
String localVar = context.localVariable("_p");
790-
arguments
791-
.add(CodeBlock.builder().add("$L.getPoints().stream().map($L -> $T.of($L.getX(), $L.getY())).toList()",
792-
parameterName, localVar, List.class, localVar, localVar).build());
788+
arguments.add(
789+
CodeBlock.builder().add("$1L.getPoints().stream().map($2L -> $3T.of($2L.getX(), $2L.getY())).toList()",
790+
parameterName, localVar, List.class).build());
793791
}
794792

795793
else {
@@ -890,11 +888,10 @@ private CodeBlock renderExpressionToQuery(@Nullable String source, String variab
890888
Builder builder = CodeBlock.builder();
891889
if (!StringUtils.hasText(source)) {
892890

893-
builder.addStatement("$T $L = new $T(new $T())", BasicQuery.class, variableName, BasicQuery.class,
894-
Document.class);
891+
builder.addStatement("$1T $2L = new $1T(new $3T())", BasicQuery.class, variableName, Document.class);
895892
} else if (!containsPlaceholder(source)) {
896-
builder.addStatement("$T $L = new $T($T.parse($S))", BasicQuery.class, variableName, BasicQuery.class,
897-
Document.class, source);
893+
builder.addStatement("$1T $2L = new $1T($3T.parse($4S))", BasicQuery.class, variableName, Document.class,
894+
source);
898895
} else {
899896
builder.add("$T $L = createQuery($S, new $T[]{ ", BasicQuery.class, variableName, source, Object.class);
900897
Iterator<CodeBlock> iterator = arguments.iterator();
@@ -939,8 +936,7 @@ CodeBlock build() {
939936
builder.add("\n");
940937
String tmpVariableName = updateVariableName + "Document";
941938
builder.add(renderExpressionToDocument(source.getUpdate().getUpdateString(), tmpVariableName, arguments));
942-
builder.addStatement("$T $L = new $T($L)", BasicUpdate.class, updateVariableName, BasicUpdate.class,
943-
tmpVariableName);
939+
builder.addStatement("$1T $2L = new $1T($3L)", BasicUpdate.class, updateVariableName, tmpVariableName);
944940

945941
return builder.build();
946942
}
@@ -951,9 +947,9 @@ private static CodeBlock renderExpressionToDocument(@Nullable String source, Str
951947

952948
Builder builder = CodeBlock.builder();
953949
if (!StringUtils.hasText(source)) {
954-
builder.addStatement("$T $L = new $T()", Document.class, variableName, Document.class);
950+
builder.addStatement("$1T $2L = new $1T()", Document.class, variableName);
955951
} else if (!containsPlaceholder(source)) {
956-
builder.addStatement("$T $L = $T.parse($S)", Document.class, variableName, Document.class, source);
952+
builder.addStatement("$1T $2L = $1T.parse($3S)", Document.class, variableName, source);
957953
} else {
958954

959955
builder.add("$T $L = bindParameters($S, new $T[]{ ", Document.class, variableName, source, Object.class);
@@ -965,8 +961,6 @@ private static CodeBlock renderExpressionToDocument(@Nullable String source, Str
965961
}
966962
}
967963
builder.add("});\n");
968-
// builder.addStatement("$T $L = bindParameters($S, new $T[]{ $L })", Document.class, variableName, source,
969-
// Object.class, StringUtils.collectionToDelimitedString(arguments, ", "));
970964
}
971965
return builder.build();
972966
}

0 commit comments

Comments
 (0)