Skip to content

Commit 7387a47

Browse files
authored
Use the Comments trait and ListUtils/reduce instead of hand-built comments and manual list handling (#1226)
1 parent ac5b545 commit 7387a47

8 files changed

Lines changed: 24 additions & 60 deletions

‎src/main/java/org/openrewrite/java/testing/cleanup/RemoveTestPrefix.java‎

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -162,16 +162,15 @@ public J.MethodDeclaration visitMethodDeclaration(J.MethodDeclaration method,
162162
}
163163

164164
// Skip when calling a similarly named method
165-
AtomicBoolean skip = new AtomicBoolean(false);
166-
new JavaIsoVisitor<AtomicBoolean>() {
165+
AtomicBoolean skip = new JavaIsoVisitor<AtomicBoolean>() {
167166
@Override
168-
public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, AtomicBoolean atomicBoolean) {
167+
public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, AtomicBoolean found) {
169168
if (method.getName().getSimpleName().equals(newMethodName) && method.getSelect() == null) {
170-
skip.set(true);
169+
found.set(true);
171170
}
172-
return super.visitMethodInvocation(method, atomicBoolean);
171+
return super.visitMethodInvocation(method, found);
173172
}
174-
}.visit(m, skip, getCursor().getParentTreeCursor());
173+
}.reduce(m, new AtomicBoolean(), getCursor().getParentTreeCursor());
175174
if (skip.get()) {
176175
return m;
177176
}

‎src/main/java/org/openrewrite/java/testing/cleanup/TestsShouldIncludeAssertions.java‎

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -159,19 +159,16 @@ private boolean methodIsDisabled(J.MethodDeclaration methodDeclaration) {
159159
}
160160

161161
private boolean methodHasAssertion(J.Block body) {
162-
AtomicBoolean hasAssertion = new AtomicBoolean(Boolean.FALSE);
163-
JavaIsoVisitor<AtomicBoolean> findAssertionVisitor = new JavaIsoVisitor<AtomicBoolean>() {
162+
return new JavaIsoVisitor<AtomicBoolean>() {
164163
@Override
165-
public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, AtomicBoolean atomicBoolean) {
166-
J.MethodInvocation mi = super.visitMethodInvocation(method, atomicBoolean);
164+
public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, AtomicBoolean hasAssertion) {
165+
J.MethodInvocation mi = super.visitMethodInvocation(method, hasAssertion);
167166
if (isAssertion(mi) || isAssumption(mi)) {
168-
atomicBoolean.set(Boolean.TRUE);
167+
hasAssertion.set(true);
169168
}
170169
return mi;
171170
}
172-
};
173-
findAssertionVisitor.visit(body, hasAssertion);
174-
return hasAssertion.get();
171+
}.reduce(body, new AtomicBoolean()).get();
175172
}
176173

177174
private boolean methodInvocationInBodyContainsAssertion() {

‎src/main/java/org/openrewrite/java/testing/junit5/AddMissingNested.java‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.openrewrite.Preconditions;
2323
import org.openrewrite.Recipe;
2424
import org.openrewrite.TreeVisitor;
25+
import org.openrewrite.internal.ListUtils;
2526
import org.openrewrite.java.AnnotationMatcher;
2627
import org.openrewrite.java.JavaIsoVisitor;
2728
import org.openrewrite.java.JavaParser;
@@ -114,7 +115,7 @@ public J.ClassDeclaration visitClassDeclaration(J.ClassDeclaration classDecl, Ex
114115
.imports(NESTED)
115116
.build()
116117
.apply(updateCursor(cd), cd.getCoordinates().addAnnotation(Comparator.comparing(J.Annotation::getSimpleName)));
117-
cd.getModifiers().removeIf(modifier -> modifier.getType() == J.Modifier.Type.Static);
118+
cd = cd.withModifiers(ListUtils.filter(cd.getModifiers(), modifier -> modifier.getType() != J.Modifier.Type.Static));
118119
return maybeAutoFormat(classDecl, cd, cd.getName(), ctx, getCursor().getParentTreeCursor());
119120
}
120121

‎src/main/java/org/openrewrite/java/testing/junit5/ExpectedSystemExitToCatchSystemExit.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ public J.VariableDeclarations visitVariableDeclarations(J.VariableDeclarations f
108108
return null;
109109
}
110110
if (todos.containsKey(variable)) {
111-
return withTodo(vd, todos.get(variable));
111+
return withTodo(updateCursor(vd), todos.get(variable));
112112
}
113113
return vd;
114114
}

‎src/main/java/org/openrewrite/java/testing/junit5/SystemRules.java‎

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
import org.openrewrite.java.JavaIsoVisitor;
2323
import org.openrewrite.java.marker.JavaSourceSet;
2424
import org.openrewrite.java.tree.*;
25-
import org.openrewrite.marker.Markers;
25+
import org.openrewrite.trait.Comments;
2626

2727
import java.util.ArrayList;
2828
import java.util.List;
@@ -133,17 +133,11 @@ static boolean isStatement(Cursor invocation) {
133133
return invocation.getParentTreeCursor().getValue() instanceof J.Block;
134134
}
135135

136-
static J.VariableDeclarations withTodo(J.VariableDeclarations field, String todo) {
137-
Space prefix = field.getPrefix();
138-
for (Comment comment : prefix.getComments()) {
139-
if (comment instanceof TextComment && todo.equals(((TextComment) comment).getText())) {
140-
return field;
141-
}
142-
}
136+
static J.VariableDeclarations withTodo(Cursor field, String todo) {
137+
Space prefix = field.<J.VariableDeclarations>getValue().getPrefix();
143138
String whitespace = prefix.getComments().isEmpty() ? prefix.getWhitespace() :
144139
prefix.getComments().get(prefix.getComments().size() - 1).getSuffix();
145140
String indent = whitespace.substring(Math.max(0, whitespace.lastIndexOf('\n')));
146-
return field.withPrefix(prefix.withComments(ListUtils.concat(prefix.getComments(),
147-
new TextComment(false, todo, indent, Markers.EMPTY))));
141+
return Comments.of(field).comment(todo, Comments.Placement.BEFORE, indent);
148142
}
149143
}

‎src/main/java/org/openrewrite/java/testing/junit5/SystemRulesToSystemStubs.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ public J visitVariableDeclarations(J.VariableDeclarations multiVariable, Executi
177177
return vd;
178178
}
179179
if (todos.containsKey(variable)) {
180-
return withTodo(vd, todos.get(variable));
180+
return withTodo(updateCursor(vd), todos.get(variable));
181181
}
182182
Migration migration = migrations.get(variable);
183183
if (migration == null) {

‎src/main/java/org/openrewrite/java/testing/junit5/UpdateTestAnnotation.java‎

Lines changed: 4 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,8 @@
3232

3333
import java.util.Comparator;
3434
import java.util.List;
35-
import java.util.Objects;
3635
import java.util.Set;
3736

38-
import static java.util.Collections.emptyList;
39-
4037
public class UpdateTestAnnotation extends Recipe {
4138

4239
@Getter
@@ -185,34 +182,15 @@ private J.MethodDeclaration removeSpecificThrowsClause(J.MethodDeclaration metho
185182
return method;
186183
}
187184

188-
// Extract the exception type from the expected exception expression
189185
// This should always be a `J.FieldAccess` of the form MyException.class
190-
JavaType expectedType = null;
191-
if (expectedException instanceof J.FieldAccess) {
192-
J.FieldAccess fa = (J.FieldAccess) expectedException;
193-
expectedType = fa.getTarget().getType();
186+
if (!(expectedException instanceof J.FieldAccess)) {
187+
return method;
194188
}
195-
189+
JavaType expectedType = ((J.FieldAccess) expectedException).getTarget().getType();
196190
if (expectedType == null) {
197191
return method;
198192
}
199-
200-
// Filter out only the exception that matches the expected exception
201-
final JavaType finalExpectedType = expectedType;
202-
List<NameTree> filteredThrows = ListUtils.map(thrown, throwClause -> {
203-
JavaType throwType = throwClause.getType();
204-
if (throwType != null && TypeUtils.isAssignableTo(finalExpectedType, throwType)) {
205-
return null;
206-
}
207-
return throwClause;
208-
});
209-
210-
// If all throws were removed, return empty list, otherwise keep the remaining ones
211-
if (filteredThrows.isEmpty() || filteredThrows.stream().allMatch(Objects::isNull)) {
212-
return method.withThrows(emptyList());
213-
}
214-
215-
return method.withThrows(filteredThrows);
193+
return method.withThrows(ListUtils.filter(thrown, throwClause -> throwClause.getType() == null || !TypeUtils.isAssignableTo(expectedType, throwClause.getType())));
216194
}
217195

218196
private static class ChangeTestAnnotation extends JavaIsoVisitor<ExecutionContext> {

‎src/main/java/org/openrewrite/java/testing/mockito/PowerMockitoDoStubbingToMockito.java‎

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
import org.openrewrite.java.MethodMatcher;
2727
import org.openrewrite.java.search.UsesMethod;
2828
import org.openrewrite.java.tree.*;
29-
import org.openrewrite.marker.Markers;
29+
import org.openrewrite.trait.Comments;
3030

3131
import java.util.List;
3232

@@ -86,12 +86,7 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Execu
8686
firstArg.getType(), targetMethodName, extraArgs.size());
8787
if (targetMethod != null && targetMethod.getFlags().contains(Private)) {
8888
String comment = " PowerMock private method stubbing is not supported by Mockito. Refactor the private method to package-private or extract to a collaborator.";
89-
if (mi.getPrefix().getComments().stream().noneMatch(c -> c.printComment(getCursor()).endsWith(comment.trim()))) {
90-
return mi.withPrefix(mi.getPrefix().withComments(ListUtils.concat(
91-
mi.getPrefix().getComments(),
92-
new TextComment(false, comment, "\n" + mi.getPrefix().getIndent(), Markers.EMPTY))));
93-
}
94-
return mi;
89+
return Comments.of(updateCursor(mi)).comment(comment, Comments.Placement.BEFORE, "\n" + mi.getPrefix().getIndent());
9590
}
9691

9792
// Rewrite: doX().when(instance, "method", args...) → doX().when(instance).method(args...)

0 commit comments

Comments
 (0)