Skip to content

Commit

Permalink
Fix JavaTemplate annotation insertion sometimes adding an unnecessary…
Browse files Browse the repository at this point in the history
… space in front of modifiers
  • Loading branch information
sambsnyd committed Nov 11, 2024
1 parent a126fc8 commit f50e53b
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,44 @@

import java.util.List;

import static java.util.Comparator.comparing;
import static org.assertj.core.api.Assertions.assertThat;
import static org.openrewrite.java.Assertions.java;
import static org.openrewrite.test.RewriteTest.toRecipe;

@SuppressWarnings({"ConstantConditions", "PatternVariableCanBeUsed", "UnnecessaryBoxing", "StatementWithEmptyBody", "UnusedAssignment", "SizeReplaceableByIsEmpty", "ResultOfMethodCallIgnored", "RedundantOperationOnEmptyContainer"})
class JavaTemplateTest implements RewriteTest {

@Test
void addAnnotation() {
rewriteRun(
spec -> spec.recipe(toRecipe(() -> new JavaIsoVisitor<>() {
private final JavaTemplate template = JavaTemplate.builder("@SuppressWarnings({\"ALL\"})")
.build();
@Override
public J.ClassDeclaration visitClassDeclaration(J.ClassDeclaration classDecl, ExecutionContext executionContext) {
J.ClassDeclaration cd = classDecl;
if(!cd.getLeadingAnnotations().isEmpty()) {
return cd;
}
cd = template.apply(updateCursor(cd), cd.getCoordinates().addAnnotation(comparing(J.Annotation::getSimpleName)));
return cd;
}
})),
java(
"""
public class A {
}
""",
"""
@SuppressWarnings({"ALL"})
public class A {
}
"""
)
);
}

@Issue("https://github.com/openrewrite/rewrite/issues/2090")
@Test
void assignmentWithinIfPredicate() {
Expand Down Expand Up @@ -371,7 +402,7 @@ public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx)
java(
"""
import java.util.Arrays;
class T {
void m() {
Object l = Arrays.asList(new java.util.HashMap<String, String>() {
Expand All @@ -383,7 +414,7 @@ void foo() {
""",
"""
import java.util.Collections;
class T {
void m() {
Object l = Collections.singletonList(new java.util.HashMap<String, String>() {
Expand Down Expand Up @@ -427,7 +458,7 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Integ
java(
"""
import java.util.Arrays;
class T<T> {
void m(T o) {
Object l = Arrays.asList(o);
Expand All @@ -436,7 +467,7 @@ void m(T o) {
""",
"""
import java.util.Collections;
class T<T> {
void m(T o) {
Object l = Collections.singletonList(o);
Expand Down Expand Up @@ -480,7 +511,7 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Integ
"""
import java.util.Arrays;
import java.util.Collection;
class T<T> {
void m(Collection<T> o) {
Object l = Arrays.asList(o);
Expand All @@ -490,7 +521,7 @@ void m(Collection<T> o) {
"""
import java.util.Collection;
import java.util.Collections;
class T<T> {
void m(Collection<T> o) {
Object l = Collections.singletonList(o);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,7 @@ public J visitClassDeclaration(J.ClassDeclaration classDecl, Integer p) {
getComparatorOrThrow()));
}
}
return autoFormat(c, c.getLeadingAnnotations().get(c.getLeadingAnnotations().size() - 1), p,
getCursor().getParentOrThrow());
return autoFormat(c, c.getName(), p, getCursor().getParentOrThrow());
}
case EXTENDS: {
TypeTree anExtends = substitutions.unsubstitute(templateParser.parseExtends(getCursor(), substitutedTemplate));
Expand Down

0 comments on commit f50e53b

Please sign in to comment.