Skip to content

Commit eba608b

Browse files
committed
Fix AOT evaluation code generation when method has no parameters.
See #5006 Original pull request: #5005
1 parent d9899c4 commit eba608b

File tree

4 files changed

+39
-9
lines changed

4 files changed

+39
-9
lines changed

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,13 @@ static class AggregationCodeBlockBuilder {
173173

174174
this.context = context;
175175
this.queryMethod = queryMethod;
176-
this.parameterNames = StringUtils.collectionToDelimitedString(context.getAllParameterNames(), ", ");
176+
String parameterNames = StringUtils.collectionToDelimitedString(context.getAllParameterNames(), ", ");
177+
178+
if (StringUtils.hasText(parameterNames)) {
179+
this.parameterNames = ", " + parameterNames;
180+
} else {
181+
this.parameterNames = "";
182+
}
177183
}
178184

179185
AggregationCodeBlockBuilder stages(AggregationInteraction aggregation) {

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

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ static CodeBlock asDocument(String source, String argNames) {
169169
if (!StringUtils.hasText(source)) {
170170
builder.add("new $T()", Document.class);
171171
} else if (containsPlaceholder(source)) {
172-
builder.add("bindParameters(ExpressionMarker.class.getEnclosingMethod(), $S, $L);\n", source, argNames);
172+
builder.add("bindParameters(ExpressionMarker.class.getEnclosingMethod(), $S$L);\n", source, argNames);
173173
} else {
174174
builder.add("parse($S)", source);
175175
}
@@ -183,7 +183,7 @@ static CodeBlock renderExpressionToDocument(@Nullable String source, String vari
183183
if (!StringUtils.hasText(source)) {
184184
builder.addStatement("$1T $2L = new $1T()", Document.class, variableName);
185185
} else if (containsPlaceholder(source)) {
186-
builder.add("$T $L = bindParameters(ExpressionMarker.class.getEnclosingMethod(), $S, $L);\n", Document.class,
186+
builder.add("$T $L = bindParameters(ExpressionMarker.class.getEnclosingMethod(), $S$L);\n", Document.class,
187187
variableName, source, argNames);
188188
} else {
189189
builder.addStatement("$1T $2L = parse($3S)", Document.class, variableName, source);
@@ -198,9 +198,17 @@ static CodeBlock evaluateNumberPotentially(String value, Class<? extends Number>
198198
return CodeBlock.of("$L", number);
199199
} catch (IllegalArgumentException e) {
200200

201+
String parameterNames = StringUtils.collectionToDelimitedString(context.getAllParameterNames(), ", ");
202+
203+
if (StringUtils.hasText(parameterNames)) {
204+
parameterNames = ", " + parameterNames;
205+
} else {
206+
parameterNames = "";
207+
}
208+
201209
Builder builder = CodeBlock.builder();
202-
builder.add("($T) evaluate(ExpressionMarker.class.getEnclosingMethod(), $S, $L)", targetType, value,
203-
StringUtils.collectionToDelimitedString(context.getAllParameterNames(), ", "));
210+
builder.add("($T) evaluate(ExpressionMarker.class.getEnclosingMethod(), $S$L)", targetType, value,
211+
parameterNames);
204212
return builder.build();
205213
}
206214
}

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

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,14 @@ static class QueryCodeBlockBuilder {
146146

147147
this.context = context;
148148
this.queryMethod = queryMethod;
149-
this.parameterNames = StringUtils.collectionToDelimitedString(context.getAllParameterNames(), ", ");
149+
150+
String parameterNames = StringUtils.collectionToDelimitedString(context.getAllParameterNames(), ", ");
151+
152+
if (StringUtils.hasText(parameterNames)) {
153+
this.parameterNames = ", " + parameterNames;
154+
} else {
155+
this.parameterNames = "";
156+
}
150157
}
151158

152159
QueryCodeBlockBuilder filter(QueryInteraction query) {
@@ -235,7 +242,7 @@ CodeBlock build() {
235242
} else {
236243

237244
builder.addStatement(
238-
"$L.collation(collationOf(evaluate(ExpressionMarker.class.getEnclosingMethod(), $S, $L)))",
245+
"$L.collation(collationOf(evaluate(ExpressionMarker.class.getEnclosingMethod(), $S$L)))",
239246
queryVariableName, collationString, parameterNames);
240247
}
241248
}
@@ -260,7 +267,7 @@ private CodeBlock renderExpressionToQuery() {
260267
return CodeBlock.of("new $T(new $T())", BasicQuery.class, Document.class);
261268
} else if (MongoCodeBlocks.containsPlaceholder(source)) {
262269
Builder builder = CodeBlock.builder();
263-
builder.add("createQuery(ExpressionMarker.class.getEnclosingMethod(), $S, $L)", source, parameterNames);
270+
builder.add("createQuery(ExpressionMarker.class.getEnclosingMethod(), $S$L)", source, parameterNames);
264271
return builder.build();
265272
}
266273
else {

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,11 +102,20 @@ CodeBlock build() {
102102
static class UpdateCodeBlockBuilder {
103103

104104
private final AotQueryMethodGenerationContext context;
105+
private final String parameterNames;
105106
private UpdateInteraction source;
106107
private String updateVariableName;
107108

108109
public UpdateCodeBlockBuilder(AotQueryMethodGenerationContext context) {
109110
this.context = context;
111+
112+
String parameterNames = StringUtils.collectionToDelimitedString(context.getAllParameterNames(), ", ");
113+
114+
if (StringUtils.hasText(parameterNames)) {
115+
this.parameterNames = ", " + parameterNames;
116+
} else {
117+
this.parameterNames = "";
118+
}
110119
}
111120

112121
public UpdateCodeBlockBuilder update(UpdateInteraction update) {
@@ -127,7 +136,7 @@ CodeBlock build() {
127136
String tmpVariableName = updateVariableName + "Document";
128137
builder.add(
129138
MongoCodeBlocks.renderExpressionToDocument(source.getUpdate().getUpdateString(), tmpVariableName,
130-
StringUtils.collectionToDelimitedString(context.getAllParameterNames(), ", ")));
139+
parameterNames));
131140
builder.addStatement("$1T $2L = new $1T($3L)", BasicUpdate.class, updateVariableName, tmpVariableName);
132141

133142
return builder.build();

0 commit comments

Comments
 (0)