|
24 | 24 | import org.openrewrite.java.AnnotationMatcher; |
25 | 25 | import org.openrewrite.java.JavaIsoVisitor; |
26 | 26 | import org.openrewrite.java.MethodMatcher; |
| 27 | +import org.openrewrite.java.search.SemanticallyEqual; |
27 | 28 | import org.openrewrite.java.search.UsesMethod; |
28 | 29 | import org.openrewrite.java.search.UsesType; |
29 | 30 | import org.openrewrite.java.service.AnnotationService; |
| 31 | +import org.openrewrite.java.tree.Expression; |
30 | 32 | import org.openrewrite.java.tree.J; |
31 | 33 |
|
| 34 | +import java.util.Arrays; |
| 35 | +import java.util.HashSet; |
| 36 | +import java.util.List; |
| 37 | +import java.util.Set; |
| 38 | + |
32 | 39 | public class RemoveInitMocksIfRunnersSpecified extends Recipe { |
33 | 40 |
|
34 | 41 | @Getter |
35 | | - final String displayName = "Remove `MockitoAnnotations.initMocks(this)` if specified JUnit runners"; |
| 42 | + final String displayName = "Remove `MockitoAnnotations.initMocks(this)` and `openMocks(this)` if JUnit runners specified"; |
36 | 43 |
|
37 | 44 | @Getter |
38 | | - final String description = "Remove `MockitoAnnotations.initMocks(this)` if specified class-level JUnit runners `@RunWith(MockitoJUnitRunner.class)` or `@ExtendWith(MockitoExtension.class)`."; |
| 45 | + final String description = "Remove `MockitoAnnotations.initMocks(this)` and `MockitoAnnotations.openMocks(this)` if class-level " + |
| 46 | + "JUnit runners `@RunWith(MockitoJUnitRunner.class)` or `@ExtendWith(MockitoExtension.class)` are specified. " + |
| 47 | + "These manual initialization calls are redundant when using Mockito's JUnit integration."; |
39 | 48 |
|
40 | 49 | private static final String MOCKITO_EXTENSION = "org.mockito.junit.jupiter.MockitoExtension"; |
41 | 50 | private static final String MOCKITO_JUNIT_RUNNER = "org.mockito.junit.MockitoJUnitRunner"; |
42 | 51 | private static final AnnotationMatcher MOCKITO_EXTENSION_MATCHER = new AnnotationMatcher("@org.junit.jupiter.api.extension.ExtendWith(" + MOCKITO_EXTENSION + ".class)"); |
43 | 52 | private static final AnnotationMatcher MOCKITO_JUNIT_MATCHER = new AnnotationMatcher("@org.junit.runner.RunWith(" + MOCKITO_JUNIT_RUNNER + ".class)"); |
44 | 53 | private static final MethodMatcher INIT_MOCKS_MATCHER = new MethodMatcher("org.mockito.MockitoAnnotations initMocks(..)", false); |
| 54 | + private static final MethodMatcher OPEN_MOCKS_MATCHER = new MethodMatcher("org.mockito.MockitoAnnotations openMocks(..)", false); |
| 55 | + private static final MethodMatcher CLOSEABLE_MATCHER = new MethodMatcher("java.lang.AutoCloseable close()", false); |
| 56 | + private static List<AnnotationMatcher> BEFORE_AND_AFTER_MATCHERS = Arrays.asList( |
| 57 | + new AnnotationMatcher("@org.junit.jupiter.api.BeforeAll"), |
| 58 | + new AnnotationMatcher("@org.junit.jupiter.api.BeforeEach"), |
| 59 | + new AnnotationMatcher("@org.junit.BeforeClass"), |
| 60 | + new AnnotationMatcher("@org.junit.Before"), |
| 61 | + new AnnotationMatcher("@org.junit.jupiter.api.AfterAll"), |
| 62 | + new AnnotationMatcher("@org.junit.jupiter.api.AfterEach"), |
| 63 | + new AnnotationMatcher("@org.junit.AfterClass"), |
| 64 | + new AnnotationMatcher("@org.junit.After") |
| 65 | + ); |
45 | 66 |
|
46 | 67 | @Override |
47 | 68 | public TreeVisitor<?, ExecutionContext> getVisitor() { |
48 | 69 | return Preconditions.check( |
49 | 70 | Preconditions.and( |
50 | | - new UsesMethod<>(INIT_MOCKS_MATCHER), |
| 71 | + Preconditions.or( |
| 72 | + new UsesMethod<>(INIT_MOCKS_MATCHER), |
| 73 | + new UsesMethod<>(OPEN_MOCKS_MATCHER) |
| 74 | + ), |
51 | 75 | Preconditions.or( |
52 | 76 | new UsesType<>(MOCKITO_EXTENSION, false), |
53 | 77 | new UsesType<>(MOCKITO_JUNIT_RUNNER, false) |
54 | 78 | ) |
55 | 79 | ), |
56 | 80 | new JavaIsoVisitor<ExecutionContext>() { |
57 | | - |
58 | 81 | @Override |
59 | | - public J.@Nullable MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) { |
60 | | - J.MethodInvocation mi = super.visitMethodInvocation(method, ctx); |
61 | | - if (INIT_MOCKS_MATCHER.matches(mi)) { |
62 | | - maybeRemoveImport("org.mockito.MockitoAnnotations"); |
63 | | - return null; |
64 | | - } |
65 | | - return mi; |
| 82 | + public J.ClassDeclaration visitClassDeclaration(J.ClassDeclaration classDecl, ExecutionContext ctx) { |
| 83 | + J.ClassDeclaration cd = super.visitClassDeclaration(classDecl, ctx); |
| 84 | + |
| 85 | + Set<Expression> closeables = new JavaIsoVisitor<Set<Expression>>() { |
| 86 | + @Override |
| 87 | + public J.Assignment visitAssignment(J.Assignment assignment, Set<Expression> exprSet) { |
| 88 | + J.Assignment as = super.visitAssignment(assignment, exprSet); |
| 89 | + |
| 90 | + if (isMockitoOpenMocksCall(assignment.getAssignment())) { |
| 91 | + exprSet.add(assignment.getVariable()); |
| 92 | + } |
| 93 | + return as; |
| 94 | + } |
| 95 | + }.reduce(cd, new HashSet<>()); |
| 96 | + |
| 97 | + J.ClassDeclaration modifiedCd = (J.ClassDeclaration) new JavaIsoVisitor<ExecutionContext>() { |
| 98 | + |
| 99 | + @Override |
| 100 | + public J.ClassDeclaration visitClassDeclaration(J.ClassDeclaration classDecl, ExecutionContext ctx) { |
| 101 | + if (service(AnnotationService.class).matches(getCursor(), MOCKITO_EXTENSION_MATCHER) || |
| 102 | + service(AnnotationService.class).matches(getCursor(), MOCKITO_JUNIT_MATCHER)) { |
| 103 | + return super.visitClassDeclaration(classDecl, ctx); |
| 104 | + } |
| 105 | + return classDecl; |
| 106 | + } |
| 107 | + |
| 108 | + @Override |
| 109 | + public J.@Nullable Assignment visitAssignment(J.Assignment assignment, ExecutionContext ctx) { |
| 110 | + J.Assignment a = super.visitAssignment(assignment, ctx); |
| 111 | + // Remove assignments where RHS is initMocks/openMocks |
| 112 | + if (isMockitoInitMocksCall(assignment.getAssignment()) || isMockitoOpenMocksCall(assignment.getAssignment())) { |
| 113 | + maybeRemoveImport("org.mockito.MockitoAnnotations"); |
| 114 | + return null; |
| 115 | + } |
| 116 | + return a; |
| 117 | + } |
| 118 | + |
| 119 | + @Override |
| 120 | + public J.@Nullable MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) { |
| 121 | + J.MethodInvocation mi = super.visitMethodInvocation(method, ctx); |
| 122 | + if (OPEN_MOCKS_MATCHER.matches(mi) || INIT_MOCKS_MATCHER.matches(mi)) { |
| 123 | + return null; |
| 124 | + } |
| 125 | + if (CLOSEABLE_MATCHER.matches(mi) && mi.getSelect() != null && closeables.stream().anyMatch(it -> SemanticallyEqual.areEqual(it, mi.getSelect()))) { |
| 126 | + return null; |
| 127 | + } |
| 128 | + return mi; |
| 129 | + } |
| 130 | + |
| 131 | + @Override |
| 132 | + public J.@Nullable VariableDeclarations visitVariableDeclarations(J.VariableDeclarations multiVariable, ExecutionContext ctx) { |
| 133 | + J.VariableDeclarations vd = super.visitVariableDeclarations(multiVariable, ctx); |
| 134 | + // Remove field declarations for fields that store openMocks result |
| 135 | + for (J.VariableDeclarations.NamedVariable variable : vd.getVariables()) { |
| 136 | + if (closeables.stream().anyMatch(it -> SemanticallyEqual.areEqual(it, variable.getDeclarator()))) { |
| 137 | + return null; |
| 138 | + } |
| 139 | + } |
| 140 | + return vd; |
| 141 | + } |
| 142 | + |
| 143 | + @Override |
| 144 | + public J.@Nullable MethodDeclaration visitMethodDeclaration(J.MethodDeclaration method, ExecutionContext ctx) { |
| 145 | + J.MethodDeclaration md = super.visitMethodDeclaration(method, ctx); |
| 146 | + if (md != method && md.getBody() != null && md.getBody().getStatements().isEmpty()) { |
| 147 | + // Only remove empty Before and After methods |
| 148 | + if (BEFORE_AND_AFTER_MATCHERS.stream().anyMatch(matcher -> |
| 149 | + service(AnnotationService.class).matches(getCursor(), matcher))) { |
| 150 | + return null; |
| 151 | + } |
| 152 | + } |
| 153 | + return md; |
| 154 | + } |
| 155 | + |
| 156 | + }.visitNonNull(cd, ctx, getCursor().getParentOrThrow()); |
| 157 | + |
| 158 | + maybeRemoveImport("org.mockito.MockitoAnnotations"); |
| 159 | + maybeRemoveImport("org.junit.jupiter.api.BeforeAll"); |
| 160 | + maybeRemoveImport("org.junit.jupiter.api.BeforeEach"); |
| 161 | + maybeRemoveImport("org.junit.jupiter.api.AfterAll"); |
| 162 | + maybeRemoveImport("org.junit.jupiter.api.AfterEach"); |
| 163 | + maybeRemoveImport("org.junit.BeforeClass"); |
| 164 | + maybeRemoveImport("org.junit.Before"); |
| 165 | + maybeRemoveImport("org.junit.AfterClass"); |
| 166 | + maybeRemoveImport("org.junit.After"); |
| 167 | + |
| 168 | + return modifiedCd; |
66 | 169 | } |
67 | 170 |
|
68 | | - @Override |
69 | | - public J.@Nullable MethodDeclaration visitMethodDeclaration(J.MethodDeclaration method, ExecutionContext ctx) { |
70 | | - J.MethodDeclaration md = super.visitMethodDeclaration(method, ctx); |
71 | | - if (md != method && md.getBody() != null && md.getBody().getStatements().isEmpty()) { |
72 | | - maybeRemoveImport("org.junit.jupiter.api.BeforeEach"); |
73 | | - maybeRemoveImport("org.junit.Before"); |
74 | | - return null; |
75 | | - } |
76 | | - return md; |
| 171 | + private boolean isMockitoOpenMocksCall(Expression expr) { |
| 172 | + return expr instanceof J.MethodInvocation && OPEN_MOCKS_MATCHER.matches((J.MethodInvocation)expr); |
77 | 173 | } |
78 | 174 |
|
79 | | - @Override |
80 | | - public J.ClassDeclaration visitClassDeclaration(J.ClassDeclaration cd, ExecutionContext ctx) { |
81 | | - if (service(AnnotationService.class).matches(updateCursor(cd), MOCKITO_EXTENSION_MATCHER) || |
82 | | - service(AnnotationService.class).matches(updateCursor(cd), MOCKITO_JUNIT_MATCHER)) { |
83 | | - return super.visitClassDeclaration(cd, ctx); |
84 | | - } |
85 | | - return cd; |
| 175 | + private boolean isMockitoInitMocksCall(Expression expr) { |
| 176 | + return expr instanceof J.MethodInvocation && INIT_MOCKS_MATCHER.matches((J.MethodInvocation)expr); |
86 | 177 | } |
87 | 178 | } |
88 | 179 | ); |
|
0 commit comments