Skip to content

Commit b1c1191

Browse files
Migrate Traits class usage to constructor calls (#747)
Co-authored-by: Moderne <[email protected]>
1 parent 3dd349c commit b1c1191

File tree

5 files changed

+12
-14
lines changed

5 files changed

+12
-14
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,12 @@
2121
import org.openrewrite.Recipe;
2222
import org.openrewrite.TreeVisitor;
2323
import org.openrewrite.java.*;
24+
import org.openrewrite.java.trait.Annotated;
2425
import org.openrewrite.java.tree.*;
2526

2627
import static java.util.Comparator.comparing;
2728
import static org.openrewrite.java.testing.junit5.Junit4Utils.CLASS_RULE;
2829
import static org.openrewrite.java.testing.junit5.Junit4Utils.RULE;
29-
import static org.openrewrite.java.trait.Traits.annotated;
3030

3131
/**
3232
* A recipe to replace JUnit 4's EnvironmentVariables rule from contrib with the JUnit 5-compatible
@@ -97,7 +97,7 @@ private static class EnvironmentVariablesVisitor extends JavaVisitor<ExecutionCo
9797
if (variableDecls.getType() == null || !TypeUtils.isAssignableTo(ENVIRONMENT_VARIABLES, variableDecls.getType())) {
9898
return variableDecls;
9999
}
100-
J.VariableDeclarations vd = (J.VariableDeclarations) annotated("@org.junit.*Rule").asVisitor(a ->
100+
J.VariableDeclarations vd = (J.VariableDeclarations) new Annotated.Matcher("@org.junit.*Rule").asVisitor(a ->
101101
(new JavaIsoVisitor<ExecutionContext>() {
102102
@Override
103103
public J.Annotation visitAnnotation(J.Annotation annotation, ExecutionContext ctx) {

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
import org.openrewrite.java.search.UsesType;
2424
import org.openrewrite.java.trait.Annotated;
2525
import org.openrewrite.java.trait.Literal;
26-
import org.openrewrite.java.trait.Traits;
2726
import org.openrewrite.java.tree.*;
2827
import org.openrewrite.marker.Markers;
2928

@@ -111,7 +110,7 @@ public J.Annotation visitAnnotation(J.Annotation annotation, ExecutionContext ct
111110
J.Annotation anno = super.visitAnnotation(annotation, ctx);
112111
Cursor classDeclCursor = getCursor().dropParentUntil(J.ClassDeclaration.class::isInstance);
113112
if (PARAMETERS_MATCHER.matches(anno)) {
114-
Annotated annotated = Traits.annotated(PARAMETERS_MATCHER).require(annotation, getCursor().getParentOrThrow());
113+
Annotated annotated = new Annotated.Matcher(PARAMETERS_MATCHER).require(annotation, getCursor().getParentOrThrow());
115114
String annotationArgumentValue = getAnnotationArgumentForInitMethod(annotated, "method", "named");
116115
classDeclCursor.computeMessageIfAbsent(PARAMETERIZED_TESTS, v -> new HashSet<>())
117116
.add(getCursor().firstEnclosing(J.MethodDeclaration.class).getSimpleName());
@@ -135,15 +134,15 @@ public J.Annotation visitAnnotation(J.Annotation annotation, ExecutionContext ct
135134
unsupportedMethods.add(junitParamsDefaultInitMethodName(m.getSimpleName()));
136135
}
137136
} else if (NAMED_PARAMETERS_MATCHER.matches(annotation)) {
138-
Annotated annotated = Traits.annotated(NAMED_PARAMETERS_MATCHER).require(annotation, getCursor().getParentOrThrow());
137+
Annotated annotated = new Annotated.Matcher(NAMED_PARAMETERS_MATCHER).require(annotation, getCursor().getParentOrThrow());
139138
Optional<Literal> value = annotated.getDefaultAttribute("value");
140139
if (value.isPresent()) {
141140
J.MethodDeclaration m = getCursor().dropParentUntil(J.MethodDeclaration.class::isInstance).getValue();
142141
classDeclCursor.computeMessageIfAbsent(INIT_METHOD_REFERENCES, v -> new HashSet<>()).add(m.getSimpleName());
143142
classDeclCursor.computeMessageIfAbsent(INIT_METHODS_MAP, v -> new HashMap<>()).put(value.get().getString(), m.getSimpleName());
144143
}
145144
} else if (TEST_CASE_NAME_MATCHER.matches(anno)) {
146-
Annotated annotated = Traits.annotated(TEST_CASE_NAME_MATCHER).require(annotation, getCursor().getParentOrThrow());
145+
Annotated annotated = new Annotated.Matcher(TEST_CASE_NAME_MATCHER).require(annotation, getCursor().getParentOrThrow());
147146
// test name for ParameterizedTest argument
148147
Optional<Literal> value = annotated.getDefaultAttribute("value");
149148
if (value.isPresent()) {

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import org.openrewrite.TreeVisitor;
2424
import org.openrewrite.java.*;
2525
import org.openrewrite.java.search.UsesType;
26+
import org.openrewrite.java.trait.Annotated;
2627
import org.openrewrite.java.tree.*;
2728
import org.openrewrite.marker.Markers;
2829

@@ -34,7 +35,6 @@
3435

3536
import static java.util.Collections.emptyList;
3637
import static org.openrewrite.Tree.randomId;
37-
import static org.openrewrite.java.trait.Traits.annotated;
3838

3939
public class TemporaryFolderToTempDir extends Recipe {
4040

@@ -89,7 +89,7 @@ public J visitVariableDeclarations(J.VariableDeclarations multiVariable, Executi
8989
return mv;
9090
}
9191
mv = mv.withTypeExpression(toFileIdentifier(mv.getTypeExpression()));
92-
return (J.VariableDeclarations) annotated("@org.junit.*Rule")
92+
return (J.VariableDeclarations) new Annotated.Matcher("@org.junit.*Rule")
9393
.asVisitor(a -> JavaTemplate.builder("@TempDir")
9494
.imports(TEMP_DIR)
9595
.javaParser(JavaParser.fromJavaVersion().classpathFromResources(ctx, "junit-jupiter-api-5"))

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import org.openrewrite.internal.ListUtils;
2222
import org.openrewrite.java.*;
2323
import org.openrewrite.java.search.UsesMethod;
24+
import org.openrewrite.java.trait.Annotated;
2425
import org.openrewrite.java.tree.*;
2526
import org.openrewrite.marker.Markers;
2627

@@ -35,7 +36,6 @@
3536
import static org.openrewrite.Tree.randomId;
3637
import static org.openrewrite.java.VariableNameUtils.GenerationStrategy.INCREMENT_NUMBER;
3738
import static org.openrewrite.java.VariableNameUtils.generateVariableName;
38-
import static org.openrewrite.java.trait.Traits.annotated;
3939

4040
/**
4141
* Ensures that all mockStatic calls are properly closed.
@@ -100,7 +100,7 @@ public J visitTryResource(J.Try.Resource tryResource, ExecutionContext ctx) {
100100
@Override
101101
public J visitMethodDeclaration(J.MethodDeclaration method, ExecutionContext ctx) {
102102
Cursor cursor = getCursor();
103-
annotated("@org.junit.jupiter.api.*").asVisitor(a -> {
103+
new Annotated.Matcher("@org.junit.jupiter.api.*").asVisitor(a -> {
104104
String annotationName = a.getTree().getSimpleName();
105105
if (annotationName.startsWith("Before")) {
106106
cursor.putMessage(MethodType.class.getSimpleName(), MethodType.LIFECYCLE);
@@ -333,7 +333,7 @@ public J.MethodDeclaration visitMethodDeclaration(J.MethodDeclaration methodDecl
333333
return md;
334334
}
335335
AnnotationMatcher annotationMatcher = isStatic ? AFTER_ALL_MATCHER : AFTER_EACH_MATCHER;
336-
boolean matched = annotated(annotationMatcher).<AtomicBoolean>asVisitor((a, found) -> {
336+
boolean matched = new Annotated.Matcher(annotationMatcher).<AtomicBoolean>asVisitor((a, found) -> {
337337
found.set(true);
338338
return a.getTree();
339339
}).reduce(md, new AtomicBoolean()).get();

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import org.openrewrite.java.JavaTemplate;
2525
import org.openrewrite.java.search.UsesType;
2626
import org.openrewrite.java.testing.junit5.RunnerToExtension;
27+
import org.openrewrite.java.trait.Annotated;
2728
import org.openrewrite.java.tree.J;
2829
import org.openrewrite.java.tree.TypeUtils;
2930

@@ -32,8 +33,6 @@
3233
import java.util.Optional;
3334
import java.util.concurrent.atomic.AtomicReference;
3435

35-
import static org.openrewrite.java.trait.Traits.annotated;
36-
3736
public class MockitoJUnitRunnerToExtension extends Recipe {
3837
@Override
3938
public String getDisplayName() {
@@ -57,7 +56,7 @@ public TreeVisitor<?, ExecutionContext> getVisitor() {
5756
public J.ClassDeclaration visitClassDeclaration(J.ClassDeclaration classDecl, ExecutionContext ctx) {
5857
J.ClassDeclaration cd = super.visitClassDeclaration(classDecl, ctx);
5958
AtomicReference<Strictness> strictness = new AtomicReference<>();
60-
annotated(runWith).<AtomicReference<Strictness>>asVisitor((a, s) -> a.getTree().acceptJava(new JavaIsoVisitor<AtomicReference<Strictness>>() {
59+
new Annotated.Matcher(runWith).<AtomicReference<Strictness>>asVisitor((a, s) -> a.getTree().acceptJava(new JavaIsoVisitor<AtomicReference<Strictness>>() {
6160
@Override
6261
public J.FieldAccess visitFieldAccess(J.FieldAccess fieldAccess, AtomicReference<Strictness> strictness) {
6362
for (Strictness strict : Strictness.values()) {

0 commit comments

Comments
 (0)