Browse Source

Fix AOT evaluation code generation when method has no parameters.

See #5006
Original pull request: #5005
pull/5030/head
Mark Paluch 5 months ago
parent
commit
eba608ba86
No known key found for this signature in database
GPG Key ID: 55BC6374BAA9D973
  1. 8
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/aot/AggregationBlocks.java
  2. 16
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/aot/MongoCodeBlocks.java
  3. 13
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/aot/QueryBlocks.java
  4. 11
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/aot/UpdateBlocks.java

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

@ -173,7 +173,13 @@ class AggregationBlocks {
this.context = context; this.context = context;
this.queryMethod = queryMethod; this.queryMethod = queryMethod;
this.parameterNames = StringUtils.collectionToDelimitedString(context.getAllParameterNames(), ", "); String parameterNames = StringUtils.collectionToDelimitedString(context.getAllParameterNames(), ", ");
if (StringUtils.hasText(parameterNames)) {
this.parameterNames = ", " + parameterNames;
} else {
this.parameterNames = "";
}
} }
AggregationCodeBlockBuilder stages(AggregationInteraction aggregation) { AggregationCodeBlockBuilder stages(AggregationInteraction aggregation) {

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

@ -169,7 +169,7 @@ class MongoCodeBlocks {
if (!StringUtils.hasText(source)) { if (!StringUtils.hasText(source)) {
builder.add("new $T()", Document.class); builder.add("new $T()", Document.class);
} else if (containsPlaceholder(source)) { } else if (containsPlaceholder(source)) {
builder.add("bindParameters(ExpressionMarker.class.getEnclosingMethod(), $S, $L);\n", source, argNames); builder.add("bindParameters(ExpressionMarker.class.getEnclosingMethod(), $S$L);\n", source, argNames);
} else { } else {
builder.add("parse($S)", source); builder.add("parse($S)", source);
} }
@ -183,7 +183,7 @@ class MongoCodeBlocks {
if (!StringUtils.hasText(source)) { if (!StringUtils.hasText(source)) {
builder.addStatement("$1T $2L = new $1T()", Document.class, variableName); builder.addStatement("$1T $2L = new $1T()", Document.class, variableName);
} else if (containsPlaceholder(source)) { } else if (containsPlaceholder(source)) {
builder.add("$T $L = bindParameters(ExpressionMarker.class.getEnclosingMethod(), $S, $L);\n", Document.class, builder.add("$T $L = bindParameters(ExpressionMarker.class.getEnclosingMethod(), $S$L);\n", Document.class,
variableName, source, argNames); variableName, source, argNames);
} else { } else {
builder.addStatement("$1T $2L = parse($3S)", Document.class, variableName, source); builder.addStatement("$1T $2L = parse($3S)", Document.class, variableName, source);
@ -198,9 +198,17 @@ class MongoCodeBlocks {
return CodeBlock.of("$L", number); return CodeBlock.of("$L", number);
} catch (IllegalArgumentException e) { } catch (IllegalArgumentException e) {
String parameterNames = StringUtils.collectionToDelimitedString(context.getAllParameterNames(), ", ");
if (StringUtils.hasText(parameterNames)) {
parameterNames = ", " + parameterNames;
} else {
parameterNames = "";
}
Builder builder = CodeBlock.builder(); Builder builder = CodeBlock.builder();
builder.add("($T) evaluate(ExpressionMarker.class.getEnclosingMethod(), $S, $L)", targetType, value, builder.add("($T) evaluate(ExpressionMarker.class.getEnclosingMethod(), $S$L)", targetType, value,
StringUtils.collectionToDelimitedString(context.getAllParameterNames(), ", ")); parameterNames);
return builder.build(); return builder.build();
} }
} }

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

@ -146,7 +146,14 @@ class QueryBlocks {
this.context = context; this.context = context;
this.queryMethod = queryMethod; this.queryMethod = queryMethod;
this.parameterNames = StringUtils.collectionToDelimitedString(context.getAllParameterNames(), ", ");
String parameterNames = StringUtils.collectionToDelimitedString(context.getAllParameterNames(), ", ");
if (StringUtils.hasText(parameterNames)) {
this.parameterNames = ", " + parameterNames;
} else {
this.parameterNames = "";
}
} }
QueryCodeBlockBuilder filter(QueryInteraction query) { QueryCodeBlockBuilder filter(QueryInteraction query) {
@ -235,7 +242,7 @@ class QueryBlocks {
} else { } else {
builder.addStatement( builder.addStatement(
"$L.collation(collationOf(evaluate(ExpressionMarker.class.getEnclosingMethod(), $S, $L)))", "$L.collation(collationOf(evaluate(ExpressionMarker.class.getEnclosingMethod(), $S$L)))",
queryVariableName, collationString, parameterNames); queryVariableName, collationString, parameterNames);
} }
} }
@ -260,7 +267,7 @@ class QueryBlocks {
return CodeBlock.of("new $T(new $T())", BasicQuery.class, Document.class); return CodeBlock.of("new $T(new $T())", BasicQuery.class, Document.class);
} else if (MongoCodeBlocks.containsPlaceholder(source)) { } else if (MongoCodeBlocks.containsPlaceholder(source)) {
Builder builder = CodeBlock.builder(); Builder builder = CodeBlock.builder();
builder.add("createQuery(ExpressionMarker.class.getEnclosingMethod(), $S, $L)", source, parameterNames); builder.add("createQuery(ExpressionMarker.class.getEnclosingMethod(), $S$L)", source, parameterNames);
return builder.build(); return builder.build();
} }
else { else {

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

@ -102,11 +102,20 @@ class UpdateBlocks {
static class UpdateCodeBlockBuilder { static class UpdateCodeBlockBuilder {
private final AotQueryMethodGenerationContext context; private final AotQueryMethodGenerationContext context;
private final String parameterNames;
private UpdateInteraction source; private UpdateInteraction source;
private String updateVariableName; private String updateVariableName;
public UpdateCodeBlockBuilder(AotQueryMethodGenerationContext context) { public UpdateCodeBlockBuilder(AotQueryMethodGenerationContext context) {
this.context = context; this.context = context;
String parameterNames = StringUtils.collectionToDelimitedString(context.getAllParameterNames(), ", ");
if (StringUtils.hasText(parameterNames)) {
this.parameterNames = ", " + parameterNames;
} else {
this.parameterNames = "";
}
} }
public UpdateCodeBlockBuilder update(UpdateInteraction update) { public UpdateCodeBlockBuilder update(UpdateInteraction update) {
@ -127,7 +136,7 @@ class UpdateBlocks {
String tmpVariableName = updateVariableName + "Document"; String tmpVariableName = updateVariableName + "Document";
builder.add( builder.add(
MongoCodeBlocks.renderExpressionToDocument(source.getUpdate().getUpdateString(), tmpVariableName, MongoCodeBlocks.renderExpressionToDocument(source.getUpdate().getUpdateString(), tmpVariableName,
StringUtils.collectionToDelimitedString(context.getAllParameterNames(), ", "))); parameterNames));
builder.addStatement("$1T $2L = new $1T($3L)", BasicUpdate.class, updateVariableName, tmpVariableName); builder.addStatement("$1T $2L = new $1T($3L)", BasicUpdate.class, updateVariableName, tmpVariableName);
return builder.build(); return builder.build();

Loading…
Cancel
Save