Skip to content

Commit 84de4cf

Browse files
authored
Fix JavaTemplate statement replacement regression for non-J.Block parent cursors (#7123)
1 parent 3d75888 commit 84de4cf

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

rewrite-java/src/main/java/org/openrewrite/java/internal/template/JavaTemplateJavaExtension.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -435,8 +435,11 @@ public J visitMethodInvocation(J.MethodInvocation method, Integer integer) {
435435
}
436436
return m;
437437
}
438+
Object parentValue = getCursor().getParentTreeCursor().getValue();
438439
if (loc == STATEMENT_PREFIX && isScope(method) &&
439-
!(getCursor().getParentTreeCursor().getValue() instanceof J.Block)) {
440+
(parentValue instanceof J.Return ||
441+
parentValue instanceof J.Assignment ||
442+
parentValue instanceof J.AssignmentOperation)) {
440443
// Method invocation is used as an expression (e.g., inside return, assignment),
441444
// not as a standalone statement in a block. Parse as expression replacement.
442445
return autoFormat(unsubstitute(templateParser.parseExpression(
@@ -452,8 +455,11 @@ public J visitMethodInvocation(J.MethodInvocation method, Integer integer) {
452455
@Override
453456
public J visitNewClass(J.NewClass newClass, Integer p) {
454457
if (isScope(newClass)) {
458+
Object parentValue = getCursor().getParentTreeCursor().getValue();
455459
if (loc == STATEMENT_PREFIX &&
456-
!(getCursor().getParentTreeCursor().getValue() instanceof J.Block)) {
460+
(parentValue instanceof J.Return ||
461+
parentValue instanceof J.Assignment ||
462+
parentValue instanceof J.AssignmentOperation)) {
457463
return autoFormat(unsubstitute(templateParser.parseExpression(
458464
getCursor(),
459465
substitutedTemplate,

0 commit comments

Comments
 (0)