Skip to content

Commit

Permalink
Allow JavaTemplate to throw new Exception (#4260)
Browse files Browse the repository at this point in the history
  • Loading branch information
timtebeek authored Jun 17, 2024
1 parent 628e0b4 commit 0e35478
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ class Test {
""",
"""
class Test {
@SuppressWarnings("ALL")
void test2() {
}
Expand Down Expand Up @@ -412,10 +412,10 @@ void test(boolean condition) {
""",
"""
import java.util.Arrays;
abstract class Test {
abstract String[] array();
void test(boolean condition) {
Object any = Arrays.asList(condition ? array() : new String[]{"Hello!"});
}
Expand Down Expand Up @@ -457,4 +457,34 @@ void test(Map<String, ?> map) {
)
);
}

@Test
void throwNewException() {
rewriteRun(
spec -> spec.recipe(toRecipe(() -> new JavaVisitor<>() {
@Override
public J visitMethodInvocation(J.MethodInvocation methodInvocation, ExecutionContext executionContext) {
return JavaTemplate.builder("throw new RuntimeException()")
.build()
.apply(getCursor(), methodInvocation.getCoordinates().replace());
}
})),
java(
"""
public class Test {
void test() {
System.out.println("Hello");
}
}
""",
"""
public class Test {
void test() {
throw new RuntimeException();
}
}
"""
)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -212,11 +212,17 @@ protected void contextFreeTemplate(Cursor cursor, J j, StringBuilder before, Str
throw new IllegalArgumentException(
"Templating a method reference requires a cursor so that it can be properly parsed and type-attributed. " +
"Mark this template as context-sensitive by calling JavaTemplate.Builder#contextSensitive().");
} else if (j instanceof J.MethodInvocation) {
before.insert(0, "class Template {{\n");
JavaType.Method methodType = ((J.MethodInvocation) j).getMethodType();
if (methodType == null || methodType.getReturnType() != JavaType.Primitive.Void) {
before.append("Object o = ");
}
after.append(";\n}}");
} else if (j instanceof Expression && !(j instanceof J.Assignment)) {
before.insert(0, "class Template {\n");
before.append("Object o = ");
after.append(";");
after.append("\n}");
after.append(";\n}");
} else if ((j instanceof J.MethodDeclaration || j instanceof J.VariableDeclarations || j instanceof J.Block || j instanceof J.ClassDeclaration)
&& cursor.getValue() instanceof J.Block
&& (cursor.getParent().getValue() instanceof J.ClassDeclaration || cursor.getParent().getValue() instanceof J.NewClass)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package org.openrewrite.java.internal.template;

import lombok.RequiredArgsConstructor;
import lombok.ToString;
import org.antlr.v4.runtime.*;
import org.openrewrite.internal.ListUtils;
import org.openrewrite.internal.PropertyPlaceholderHelper;
Expand All @@ -34,6 +35,7 @@
import java.util.regex.Pattern;

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

Expand Down

0 comments on commit 0e35478

Please sign in to comment.