From efc38e3371b1713245ba4f3848e32e38541f896e Mon Sep 17 00:00:00 2001 From: Laurens Westerlaken Date: Wed, 4 Dec 2024 20:33:57 +0100 Subject: [PATCH] Take J.Case statements into account in JavaTemplate (#4744) --- .../internal/template/BlockStatementTemplateGenerator.java | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/rewrite-java/src/main/java/org/openrewrite/java/internal/template/BlockStatementTemplateGenerator.java b/rewrite-java/src/main/java/org/openrewrite/java/internal/template/BlockStatementTemplateGenerator.java index 3be2c6e2b0b..e6e13c0aff8 100644 --- a/rewrite-java/src/main/java/org/openrewrite/java/internal/template/BlockStatementTemplateGenerator.java +++ b/rewrite-java/src/main/java/org/openrewrite/java/internal/template/BlockStatementTemplateGenerator.java @@ -463,16 +463,17 @@ private void contextTemplate(Cursor cursor, J prior, StringBuilder before, Strin // If prior is a type parameter, wrap in __M__.anyT() // For anything else, ignore the invocation J.MethodInvocation m = (J.MethodInvocation) j; + J firstEnclosing = cursor.getParentOrThrow().firstEnclosing(J.class); if (m.getArguments().stream().anyMatch(arg -> referToSameElement(prior, arg))) { before.insert(0, "__M__.any("); - if (cursor.getParentOrThrow().firstEnclosing(J.class) instanceof J.Block) { + if (firstEnclosing instanceof J.Block || firstEnclosing instanceof J.Case) { after.append(");"); } else { after.append(")"); } } else if (m.getTypeParameters() != null && m.getTypeParameters().stream().anyMatch(tp -> referToSameElement(prior, tp))) { before.insert(0, "__M__.anyT<"); - if (cursor.getParentOrThrow().firstEnclosing(J.class) instanceof J.Block) { + if (firstEnclosing instanceof J.Block || firstEnclosing instanceof J.Case) { after.append(">();"); } else { after.append(">()"); @@ -481,7 +482,7 @@ private void contextTemplate(Cursor cursor, J prior, StringBuilder before, Strin List comments = new ArrayList<>(1); comments.add(new TextComment(true, STOP_COMMENT, "", Markers.EMPTY)); after.append(".").append(m.withSelect(null).withComments(comments).printTrimmed(cursor.getParentOrThrow())); - if (cursor.getParentOrThrow().firstEnclosing(J.class) instanceof J.Block) { + if (firstEnclosing instanceof J.Block || firstEnclosing instanceof J.Case) { after.append(";"); } }