Skip to content

Commit 0e35478

Browse files
authored
Allow JavaTemplate to throw new Exception (#4260)
1 parent 628e0b4 commit 0e35478

File tree

3 files changed

+43
-5
lines changed

3 files changed

+43
-5
lines changed

rewrite-java-test/src/test/java/org/openrewrite/java/JavaTemplateSubstitutionsTest.java

+33-3
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ class Test {
141141
""",
142142
"""
143143
class Test {
144-
144+
145145
@SuppressWarnings("ALL")
146146
void test2() {
147147
}
@@ -412,10 +412,10 @@ void test(boolean condition) {
412412
""",
413413
"""
414414
import java.util.Arrays;
415-
415+
416416
abstract class Test {
417417
abstract String[] array();
418-
418+
419419
void test(boolean condition) {
420420
Object any = Arrays.asList(condition ? array() : new String[]{"Hello!"});
421421
}
@@ -457,4 +457,34 @@ void test(Map<String, ?> map) {
457457
)
458458
);
459459
}
460+
461+
@Test
462+
void throwNewException() {
463+
rewriteRun(
464+
spec -> spec.recipe(toRecipe(() -> new JavaVisitor<>() {
465+
@Override
466+
public J visitMethodInvocation(J.MethodInvocation methodInvocation, ExecutionContext executionContext) {
467+
return JavaTemplate.builder("throw new RuntimeException()")
468+
.build()
469+
.apply(getCursor(), methodInvocation.getCoordinates().replace());
470+
}
471+
})),
472+
java(
473+
"""
474+
public class Test {
475+
void test() {
476+
System.out.println("Hello");
477+
}
478+
}
479+
""",
480+
"""
481+
public class Test {
482+
void test() {
483+
throw new RuntimeException();
484+
}
485+
}
486+
"""
487+
)
488+
);
489+
}
460490
}

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

+8-2
Original file line numberDiff line numberDiff line change
@@ -212,11 +212,17 @@ protected void contextFreeTemplate(Cursor cursor, J j, StringBuilder before, Str
212212
throw new IllegalArgumentException(
213213
"Templating a method reference requires a cursor so that it can be properly parsed and type-attributed. " +
214214
"Mark this template as context-sensitive by calling JavaTemplate.Builder#contextSensitive().");
215+
} else if (j instanceof J.MethodInvocation) {
216+
before.insert(0, "class Template {{\n");
217+
JavaType.Method methodType = ((J.MethodInvocation) j).getMethodType();
218+
if (methodType == null || methodType.getReturnType() != JavaType.Primitive.Void) {
219+
before.append("Object o = ");
220+
}
221+
after.append(";\n}}");
215222
} else if (j instanceof Expression && !(j instanceof J.Assignment)) {
216223
before.insert(0, "class Template {\n");
217224
before.append("Object o = ");
218-
after.append(";");
219-
after.append("\n}");
225+
after.append(";\n}");
220226
} else if ((j instanceof J.MethodDeclaration || j instanceof J.VariableDeclarations || j instanceof J.Block || j instanceof J.ClassDeclaration)
221227
&& cursor.getValue() instanceof J.Block
222228
&& (cursor.getParent().getValue() instanceof J.ClassDeclaration || cursor.getParent().getValue() instanceof J.NewClass)) {

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

+2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package org.openrewrite.java.internal.template;
1717

1818
import lombok.RequiredArgsConstructor;
19+
import lombok.ToString;
1920
import org.antlr.v4.runtime.*;
2021
import org.openrewrite.internal.ListUtils;
2122
import org.openrewrite.internal.PropertyPlaceholderHelper;
@@ -34,6 +35,7 @@
3435
import java.util.regex.Pattern;
3536

3637
@RequiredArgsConstructor
38+
@ToString
3739
public class Substitutions {
3840
private static final Pattern PATTERN_COMMENT = Pattern.compile("__p(\\d+)__");
3941

0 commit comments

Comments
 (0)