Skip to content

Commit 10384e5

Browse files
OpenRewrite recipe best practices
Use this link to re-run the recipe: https://app.moderne.io/recipes/org.openrewrite.recipes.rewrite.OpenRewriteRecipeBestPractices?organizationId=QUxML01vZGVybmUvTW9kZXJuZSArIE9wZW5SZXdyaXRl Co-authored-by: Moderne <team@moderne.io>
1 parent ddd9959 commit 10384e5

16 files changed

Lines changed: 85 additions & 24 deletions

src/main/java/org/openrewrite/java/testing/assertj/AdoptAssertJDurationAssertions.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ private J.MethodInvocation simplifyTimeUnits(J.MethodInvocation m, ExecutionCont
166166
List<Object> unitInfo = getUnitInfo(m.getSimpleName(), Math.toIntExact(argValue));
167167
String methodName = (String) unitInfo.get(0);
168168
int methodArg = (int) unitInfo.get(1);
169-
if (!(m.getSimpleName().equals(methodName))) {
169+
if (!m.getSimpleName().equals(methodName)) {
170170
// update method invocation with new name and arg
171171
String template = String.format("#{any()}.%s(%d)", methodName, methodArg);
172172
return applyTemplate(ctx, m, template, m.getSelect());

src/main/java/org/openrewrite/java/testing/dbrider/ExecutionListenerToDbRiderAnnotation.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ public boolean shouldAddDbRiderAnnotation() {
198198
private boolean canTestExecutionListenerBeRemoved() {
199199
if (listener == null && listeners != null && listeners.getInitializer() != null &&
200200
listeners.getInitializer().stream().allMatch(listener -> isTypeReference(listener, DBRIDER_TEST_EXECUTION_LISTENER))) {
201-
return (getMigratedInheritListeners() == null && getMigratedMergeMode() != null);
201+
return getMigratedInheritListeners() == null && getMigratedMergeMode() != null;
202202
}
203203
return false;
204204
}

src/main/java/org/openrewrite/java/testing/hamcrest/HamcrestMatcherToJUnit5.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Execu
145145

146146
//we do not handle nested matchers
147147
if (!(matcherInvocation.getArguments().get(0) instanceof J.Empty)) {
148-
if ((matcherInvocation.getArguments().get(0).getType()).toString().startsWith("org.hamcrest")) {
148+
if (matcherInvocation.getArguments().get(0).getType().toString().startsWith("org.hamcrest")) {
149149
return mi;
150150
}
151151
}

src/main/java/org/openrewrite/java/testing/jmockit/JMockitMockUpToMockito.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,7 @@ private String getAnswerBody(J.MethodDeclaration md) {
340340
new JavaIsoVisitor<Set<String>>() {
341341
@Override
342342
public J.VariableDeclarations.NamedVariable visitVariable(J.VariableDeclarations.NamedVariable variable, Set<String> ctx) {
343-
Cursor scope = getCursor().dropParentUntil((is) -> is instanceof J.ClassDeclaration || is instanceof J.Block || is instanceof J.MethodDeclaration || is instanceof J.ForLoop || is instanceof J.ForEachLoop || is instanceof J.ForLoop.Control || is instanceof J.ForEachLoop.Control || is instanceof J.Case || is instanceof J.Try || is instanceof J.Try.Resource || is instanceof J.Try.Catch || is instanceof J.MultiCatch || is instanceof J.Lambda || is instanceof JavaSourceFile);
343+
Cursor scope = getCursor().dropParentUntil(is -> is instanceof J.ClassDeclaration || is instanceof J.Block || is instanceof J.MethodDeclaration || is instanceof J.ForLoop || is instanceof J.ForEachLoop || is instanceof J.ForLoop.Control || is instanceof J.ForEachLoop.Control || is instanceof J.Case || is instanceof J.Try || is instanceof J.Try.Resource || is instanceof J.Try.Catch || is instanceof J.MultiCatch || is instanceof J.Lambda || is instanceof JavaSourceFile);
344344
if (!VariableReferences.findRhsReferences(scope.getValue(), variable.getName()).isEmpty()) {
345345
ctx.add(variable.getSimpleName());
346346
}

src/main/java/org/openrewrite/java/testing/junit5/AssertTrueInstanceofToAssertInstanceOf.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@
3232
import org.openrewrite.java.tree.TypedTree;
3333
import org.openrewrite.marker.Markers;
3434

35-
import java.util.Collections;
35+
import static java.util.Collections.emptyList;
36+
import static java.util.Collections.singletonList;
3637

3738
public class AssertTrueInstanceofToAssertInstanceOf extends Recipe {
3839
@Getter
@@ -105,7 +106,7 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Execu
105106
.build();
106107

107108
maybeAddImport("org.junit.jupiter.api.Assertions", "assertInstanceOf");
108-
TypedTree rawClazz = clazz instanceof J.ParameterizedType ? (TypedTree) ((J.ParameterizedType) clazz).getClazz() : clazz;
109+
TypedTree rawClazz = clazz instanceof J.ParameterizedType ? ((J.ParameterizedType) clazz).getClazz() : clazz;
109110
Expression classLiteral = toClassLiteral(rawClazz);
110111
return reason != null ?
111112
template.apply(getCursor(), mi.getCoordinates().replace(), classLiteral, expression, reason) :
@@ -116,9 +117,9 @@ private Expression toClassLiteral(TypedTree clazz) {
116117
JavaType clazzType = clazz.getType();
117118
JavaType.Parameterized classType = new JavaType.Parameterized(null,
118119
JavaType.ShallowClass.build("java.lang.Class"),
119-
Collections.singletonList(clazzType != null ? clazzType : JavaType.Unknown.getInstance()));
120+
singletonList(clazzType != null ? clazzType : JavaType.Unknown.getInstance()));
120121
J.Identifier classKeyword = new J.Identifier(Tree.randomId(), Space.EMPTY, Markers.EMPTY,
121-
Collections.emptyList(), "class", classType, null);
122+
emptyList(), "class", classType, null);
122123
Expression target = ((Expression) clazz).withPrefix(Space.EMPTY);
123124
return new J.FieldAccess(Tree.randomId(), Space.EMPTY, Markers.EMPTY,
124125
target, JLeftPadded.build(classKeyword), classType);

src/main/java/org/openrewrite/java/testing/junit5/EnvironmentVariables.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,18 +95,18 @@ private static class EnvironmentVariablesVisitor extends JavaVisitor<ExecutionCo
9595
return variableDecls;
9696
}
9797
J.VariableDeclarations vd = (J.VariableDeclarations) new Annotated.Matcher("@org.junit.*Rule").asVisitor(a ->
98-
(new JavaIsoVisitor<ExecutionContext>() {
98+
new JavaIsoVisitor<ExecutionContext>() {
9999
@Override
100100
public J.Annotation visitAnnotation(J.Annotation annotation, ExecutionContext ctx) {
101101
return systemStubsTemplate(ctx).apply(updateCursor(annotation), annotation.getCoordinates().replace());
102102
}
103-
}).visit(a.getTree(), ctx, a.getCursor().getParentOrThrow()))
103+
}.visit(a.getTree(), ctx, a.getCursor().getParentOrThrow()))
104104
.visit(variableDecls, ctx, getCursor().getParentOrThrow());
105105

106106
if (variableDecls != vd) {
107107
// put message to first enclosing ClassDeclaration, to inform that we have an env var rule.
108108
getCursor()
109-
.dropParentUntil(c -> c instanceof J.ClassDeclaration)
109+
.dropParentUntil(J.ClassDeclaration.class::isInstance)
110110
.putMessage(HAS_ENV_VAR_RULE, true);
111111
}
112112

@@ -123,7 +123,7 @@ public J.Annotation visitAnnotation(J.Annotation annotation, ExecutionContext ct
123123
int argCount = argCount(m);
124124
J j =
125125
getEnvVarClearTemplate(ctx, argCount)
126-
.apply(updateCursor(m), m.getCoordinates().replace(), (Object[]) getArgs(m, argCount));
126+
.apply(updateCursor(m), m.getCoordinates().replace(), getArgs(m, argCount));
127127

128128
if (getCursor().getParentTreeCursor().getValue() instanceof J.Block &&
129129
!(j instanceof Statement)) {

src/main/java/org/openrewrite/java/testing/junit5/JUnitParamsRunnerToParameterized.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ private boolean doTestParamsHaveCustomConverter(J.@Nullable MethodDeclaration me
176176
return false;
177177
}
178178
return method.getParameters().stream()
179-
.filter(param -> param instanceof J.VariableDeclarations)
179+
.filter(J.VariableDeclarations.class::isInstance)
180180
.map(J.VariableDeclarations.class::cast)
181181
.anyMatch(v -> v.getLeadingAnnotations().stream().anyMatch(CONVERTER_MATCHER::matches));
182182
}
@@ -370,7 +370,7 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Execu
370370
method.getMethodType() != null &&
371371
methodNames.contains(enclosingMethod.getSimpleName()) &&
372372
method.getMethodType().getDeclaringType() == classType) {
373-
Cursor classCursor = getCursor().dropParentUntil(j -> j instanceof J.ClassDeclaration);
373+
Cursor classCursor = getCursor().dropParentUntil(J.ClassDeclaration.class::isInstance);
374374
classCursor.computeMessageIfAbsent(REFERENCED_METHODS, k -> new ArrayList<>()).add(method.getSimpleName());
375375
}
376376
return super.visitMethodInvocation(method, ctx);

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131

3232
import java.util.Comparator;
3333
import java.util.List;
34+
import java.util.Objects;
3435
import java.util.Set;
3536

3637
import static java.util.Collections.emptyList;
@@ -196,7 +197,7 @@ private J.MethodDeclaration removeSpecificThrowsClause(J.MethodDeclaration metho
196197
});
197198

198199
// If all throws were removed, return empty list, otherwise keep the remaining ones
199-
if (filteredThrows.isEmpty() || filteredThrows.stream().allMatch(t -> t == null)) {
200+
if (filteredThrows.isEmpty() || filteredThrows.stream().allMatch(Objects::isNull)) {
200201
return method.withThrows(emptyList());
201202
}
202203

src/main/java/org/openrewrite/java/testing/mockito/CloseUnclosedStaticMocks.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,7 @@ private String tearDownMethodName(J.ClassDeclaration cd) {
364364

365365
private boolean isVarDeclared(J.ClassDeclaration cd, String varName) {
366366
return cd.getBody().getStatements().stream()
367-
.filter(s -> s instanceof J.VariableDeclarations)
367+
.filter(J.VariableDeclarations.class::isInstance)
368368
.map(J.VariableDeclarations.class::cast)
369369
.flatMap(vd -> vd.getVariables().stream())
370370
.anyMatch(var -> var.getSimpleName().equals(varName));

src/main/java/org/openrewrite/java/testing/mockito/MockitoUtils.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public static J.ClassDeclaration maybeAddMethodWithAnnotation(
6060
.build()
6161
.apply(
6262
new Cursor(visitor.getCursor().getParentOrThrow(), classDecl),
63-
(firstTestMethod != null) ?
63+
firstTestMethod != null ?
6464
firstTestMethod.getCoordinates().before() :
6565
classDecl.getBody().getCoordinates().lastStatement()
6666
);

0 commit comments

Comments
 (0)