3838
3939public class AssertionsArgumentOrder extends Recipe {
4040
41- private static final MethodMatcher [] jupiterAssertionMatchers = new MethodMatcher []{
41+ private static final MethodMatcher [] JUPITER_ASSERTION_MATCHERS = new MethodMatcher []{
4242 new MethodMatcher ("org.junit.jupiter.api.Assertions assertArrayEquals(..)" ),
4343 new MethodMatcher ("org.junit.jupiter.api.Assertions assertEquals(..)" ),
4444 new MethodMatcher ("org.junit.jupiter.api.Assertions assertNotEquals(..)" ),
4545 new MethodMatcher ("org.junit.jupiter.api.Assertions assertSame(..)" ),
4646 new MethodMatcher ("org.junit.jupiter.api.Assertions assertNotSame(..)" )
4747 };
4848
49- private static final MethodMatcher [] junitAssertMatchers = new MethodMatcher []{
49+ private static final MethodMatcher [] JUNIT_ASSERT_MATCHERS = new MethodMatcher []{
5050 new MethodMatcher ("org.junit.Assert assertEquals(..)" ),
5151 new MethodMatcher ("org.junit.Assert assertEquals(..)" ),
5252 new MethodMatcher ("org.junit.Assert assertArrayEquals(..)" ),
@@ -55,17 +55,17 @@ public class AssertionsArgumentOrder extends Recipe {
5555 new MethodMatcher ("org.junit.Assert assert*Null(String, Object)" )
5656 };
5757
58- private static final MethodMatcher [] junitAssertWithMessageMatchers = new MethodMatcher []{
58+ private static final MethodMatcher [] JUNIT_ASSERT_WITH_MESSAGE_MATCHERS = new MethodMatcher []{
5959 new MethodMatcher ("org.junit.Assert assertEquals(String, ..)" ),
6060 new MethodMatcher ("org.junit.Assert assertArrayEquals(String, ..)" )
6161 };
62- private static final MethodMatcher jupiterAssertIterableEqualsMatcher = new MethodMatcher ("org.junit.jupiter.api.Assertions assertIterableEquals(..)" );
62+ private static final MethodMatcher JUPITER_ASSERT_ITERABLE_EQUALS_MATCHER = new MethodMatcher ("org.junit.jupiter.api.Assertions assertIterableEquals(..)" );
6363
6464 // `assertNull("message", result())` should be `assertNull(result(), "message")`
65- private static final MethodMatcher jupiterAssertNullMatcher = new MethodMatcher ("org.junit.jupiter.api.Assertions assert*Null(Object, String)" );
65+ private static final MethodMatcher JUPITER_ASSERT_NULL_MATCHER = new MethodMatcher ("org.junit.jupiter.api.Assertions assert*Null(Object, String)" );
6666
6767 // Moving a null literal into the expected position short circuits the equality check, rather than calling equals
68- private static final MethodMatcher [] equalityMatchers = new MethodMatcher []{
68+ private static final MethodMatcher [] EQUALITY_MATCHERS = new MethodMatcher []{
6969 new MethodMatcher ("org.junit.jupiter.api.Assertions assertEquals(..)" ),
7070 new MethodMatcher ("org.junit.jupiter.api.Assertions assertNotEquals(..)" ),
7171 new MethodMatcher ("org.junit.Assert assertEquals(..)" ),
@@ -74,24 +74,31 @@ public class AssertionsArgumentOrder extends Recipe {
7474 new MethodMatcher ("org.testng.Assert assertNotEquals(..)" )
7575 };
7676
77- private static final MethodMatcher [] testNgMatcher = new MethodMatcher []{
77+ private static final MethodMatcher [] TESTNG_MATCHERS = new MethodMatcher []{
7878 new MethodMatcher ("org.testng.Assert assertSame(..)" ),
7979 new MethodMatcher ("org.testng.Assert assertNotSame(..)" ),
8080 new MethodMatcher ("org.testng.Assert assertEquals(..)" ),
8181 new MethodMatcher ("org.testng.Assert assertNotEquals(..)" )
8282 };
8383
84- private static final TreeVisitor <?, ExecutionContext > precondition ;
84+ private static final MethodMatcher [] NEW_LIST_MATCHERS = new MethodMatcher []{
85+ new MethodMatcher ("java.util.List of(..)" ),
86+ new MethodMatcher ("java.util.Collections singleton(..)" ),
87+ new MethodMatcher ("java.util.Collections empty()" ),
88+ new MethodMatcher ("java.util.Arrays asList(..)" ),
89+ };
90+
91+ private static final TreeVisitor <?, ExecutionContext > PRECONDITION ;
8592
8693 static {
87- List <MethodMatcher > matchers = new ArrayList <>(Arrays .asList (jupiterAssertionMatchers ));
88- matchers .addAll (Arrays .asList (junitAssertMatchers ));
89- matchers .addAll (Arrays .asList (junitAssertWithMessageMatchers ));
90- matchers .add (jupiterAssertIterableEqualsMatcher );
91- matchers .add (jupiterAssertNullMatcher );
92- matchers .addAll (Arrays .asList (testNgMatcher ));
94+ List <MethodMatcher > matchers = new ArrayList <>(Arrays .asList (JUPITER_ASSERTION_MATCHERS ));
95+ matchers .addAll (Arrays .asList (JUNIT_ASSERT_MATCHERS ));
96+ matchers .addAll (Arrays .asList (JUNIT_ASSERT_WITH_MESSAGE_MATCHERS ));
97+ matchers .add (JUPITER_ASSERT_ITERABLE_EQUALS_MATCHER );
98+ matchers .add (JUPITER_ASSERT_NULL_MATCHER );
99+ matchers .addAll (Arrays .asList (TESTNG_MATCHERS ));
93100 //noinspection unchecked
94- precondition = Preconditions .or (matchers .stream ().map (UsesMethod ::new ).toArray (TreeVisitor []::new ));
101+ PRECONDITION = Preconditions .or (matchers .stream ().map (UsesMethod ::new ).toArray (TreeVisitor []::new ));
95102 }
96103
97104 @ Getter
@@ -105,17 +112,10 @@ public class AssertionsArgumentOrder extends Recipe {
105112
106113 @ Override
107114 public TreeVisitor <?, ExecutionContext > getVisitor () {
108- return Preconditions .check (precondition , new AssertionsArgumentOrderVisitor ());
115+ return Preconditions .check (PRECONDITION , new AssertionsArgumentOrderVisitor ());
109116 }
110117
111118 private static class AssertionsArgumentOrderVisitor extends JavaIsoVisitor <ExecutionContext > {
112- private final MethodMatcher [] newListMatchers = new MethodMatcher []{
113- new MethodMatcher ("java.util.List of(..)" ),
114- new MethodMatcher ("java.util.Collections singleton(..)" ),
115- new MethodMatcher ("java.util.Collections empty()" ),
116- new MethodMatcher ("java.util.Arrays asList(..)" ),
117- };
118-
119119 @ Override
120120 public J .MethodInvocation visitMethodInvocation (J .MethodInvocation method , ExecutionContext ctx ) {
121121 J .MethodInvocation mi = super .visitMethodInvocation (method , ctx );
@@ -154,7 +154,7 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Execu
154154 }
155155
156156 private boolean isEqualityAssertion (J .MethodInvocation mi ) {
157- for (MethodMatcher equalityMatcher : equalityMatchers ) {
157+ for (MethodMatcher equalityMatcher : EQUALITY_MATCHERS ) {
158158 if (equalityMatcher .matches (mi )) {
159159 return true ;
160160 }
@@ -171,7 +171,7 @@ private boolean isNullLiteral(Expression expression) {
171171 }
172172
173173 private boolean isCorrectOrder (Expression expected , Expression actual , J .MethodInvocation mi ) {
174- if (jupiterAssertNullMatcher .matches (mi )) {
174+ if (JUPITER_ASSERT_NULL_MATCHER .matches (mi )) {
175175 return isConstant (actual , mi ) || !isConstant (expected , mi );
176176 }
177177 return isConstant (expected , mi ) || !isConstant (actual , mi );
@@ -197,8 +197,8 @@ private boolean isConstant(Expression expression, J.MethodInvocation mi) {
197197 return var .hasFlags (Flag .Static , Flag .Final );
198198 }
199199
200- if (jupiterAssertIterableEqualsMatcher .matches (mi )) {
201- for (MethodMatcher iterableMatcher : newListMatchers ) {
200+ if (JUPITER_ASSERT_ITERABLE_EQUALS_MATCHER .matches (mi )) {
201+ for (MethodMatcher iterableMatcher : NEW_LIST_MATCHERS ) {
202202 if (iterableMatcher .matches (expression )) {
203203 return true ;
204204 }
@@ -208,16 +208,16 @@ private boolean isConstant(Expression expression, J.MethodInvocation mi) {
208208 }
209209
210210 private boolean isJupiterAssertion (J .MethodInvocation mi ) {
211- for (MethodMatcher assertionMethodMatcher : jupiterAssertionMatchers ) {
211+ for (MethodMatcher assertionMethodMatcher : JUPITER_ASSERTION_MATCHERS ) {
212212 if (assertionMethodMatcher .matches (mi )) {
213213 return true ;
214214 }
215215 }
216- return jupiterAssertIterableEqualsMatcher .matches (mi ) || jupiterAssertNullMatcher .matches (mi );
216+ return JUPITER_ASSERT_ITERABLE_EQUALS_MATCHER .matches (mi ) || JUPITER_ASSERT_NULL_MATCHER .matches (mi );
217217 }
218218
219219 private boolean isTestNgAssertion (J .MethodInvocation mi ) {
220- for (MethodMatcher actExpMatcher : testNgMatcher ) {
220+ for (MethodMatcher actExpMatcher : TESTNG_MATCHERS ) {
221221 if (actExpMatcher .matches (mi )) {
222222 return true ;
223223 }
@@ -226,7 +226,7 @@ private boolean isTestNgAssertion(J.MethodInvocation mi) {
226226 }
227227
228228 private boolean isJunitAssertion (J .MethodInvocation mi ) {
229- for (MethodMatcher assertionMethodMatcher : junitAssertMatchers ) {
229+ for (MethodMatcher assertionMethodMatcher : JUNIT_ASSERT_MATCHERS ) {
230230 if (assertionMethodMatcher .matches (mi )) {
231231 return true ;
232232 }
@@ -235,7 +235,7 @@ private boolean isJunitAssertion(J.MethodInvocation mi) {
235235 }
236236
237237 private boolean isJunitAssertEqualsWithMessage (J .MethodInvocation mi ) {
238- for (MethodMatcher actExpMatcher : junitAssertWithMessageMatchers ) {
238+ for (MethodMatcher actExpMatcher : JUNIT_ASSERT_WITH_MESSAGE_MATCHERS ) {
239239 if (actExpMatcher .matches (mi )) {
240240 return true ;
241241 }
0 commit comments