Skip to content

Commit 8f4af18

Browse files
Simplify MatcherApplicationStrategy (#2803)
Prep work for #2796 The class is overly complex, with the precomputed `matchingType` adding to value.
1 parent 0ce902a commit 8f4af18

File tree

2 files changed

+35
-56
lines changed

2 files changed

+35
-56
lines changed

src/main/java/org/mockito/internal/invocation/MatcherApplicationStrategy.java

+29-51
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,6 @@
44
*/
55
package org.mockito.internal.invocation;
66

7-
import static org.mockito.internal.invocation.MatcherApplicationStrategy.MatcherApplicationType.ERROR_UNSUPPORTED_NUMBER_OF_MATCHERS;
8-
import static org.mockito.internal.invocation.MatcherApplicationStrategy.MatcherApplicationType.MATCH_EACH_VARARGS_WITH_LAST_MATCHER;
9-
import static org.mockito.internal.invocation.MatcherApplicationStrategy.MatcherApplicationType.ONE_MATCHER_PER_ARGUMENT;
10-
117
import java.util.ArrayList;
128
import java.util.List;
139

@@ -20,22 +16,12 @@
2016
public class MatcherApplicationStrategy {
2117

2218
private final Invocation invocation;
23-
private final List<ArgumentMatcher<?>> matchers;
24-
private final MatcherApplicationType matchingType;
19+
private final List<? extends ArgumentMatcher<?>> matchers;
2520

2621
private MatcherApplicationStrategy(
27-
Invocation invocation,
28-
List<ArgumentMatcher<?>> matchers,
29-
MatcherApplicationType matchingType) {
22+
Invocation invocation, List<? extends ArgumentMatcher<?>> matchers) {
3023
this.invocation = invocation;
31-
if (matchingType == MATCH_EACH_VARARGS_WITH_LAST_MATCHER) {
32-
int times = varargLength(invocation);
33-
this.matchers = appendLastMatcherNTimes(matchers, times);
34-
} else {
35-
this.matchers = matchers;
36-
}
37-
38-
this.matchingType = matchingType;
24+
this.matchers = matchers;
3925
}
4026

4127
/**
@@ -51,10 +37,8 @@ private MatcherApplicationStrategy(
5137
* @return never <code>null</code>
5238
*/
5339
public static MatcherApplicationStrategy getMatcherApplicationStrategyFor(
54-
Invocation invocation, List<ArgumentMatcher<?>> matchers) {
55-
56-
MatcherApplicationType type = getMatcherApplicationType(invocation, matchers);
57-
return new MatcherApplicationStrategy(invocation, matchers, type);
40+
Invocation invocation, List<? extends ArgumentMatcher<?>> matchers) {
41+
return new MatcherApplicationStrategy(invocation, matchers);
5842
}
5943

6044
/**
@@ -74,11 +58,28 @@ public static MatcherApplicationStrategy getMatcherApplicationStrategyFor(
7458
* </ul>
7559
*/
7660
public boolean forEachMatcherAndArgument(ArgumentMatcherAction action) {
77-
if (matchingType == ERROR_UNSUPPORTED_NUMBER_OF_MATCHERS) {
78-
return false;
61+
if (invocation.getArguments().length == matchers.size()) {
62+
return argsMatch(invocation.getArguments(), matchers, action);
7963
}
8064

81-
Object[] arguments = invocation.getArguments();
65+
final boolean isVararg =
66+
invocation.getMethod().isVarArgs()
67+
&& invocation.getRawArguments().length == matchers.size()
68+
&& isLastMatcherVarargMatcher(matchers);
69+
70+
if (isVararg) {
71+
int times = varargLength(invocation);
72+
final List<? extends ArgumentMatcher<?>> matchers = appendLastMatcherNTimes(times);
73+
return argsMatch(invocation.getArguments(), matchers, action);
74+
}
75+
76+
return false;
77+
}
78+
79+
private boolean argsMatch(
80+
Object[] arguments,
81+
List<? extends ArgumentMatcher<?>> matchers,
82+
ArgumentMatcherAction action) {
8283
for (int i = 0; i < arguments.length; i++) {
8384
ArgumentMatcher<?> matcher = matchers.get(i);
8485
Object argument = arguments[i];
@@ -90,33 +91,16 @@ public boolean forEachMatcherAndArgument(ArgumentMatcherAction action) {
9091
return true;
9192
}
9293

93-
private static MatcherApplicationType getMatcherApplicationType(
94-
Invocation invocation, List<ArgumentMatcher<?>> matchers) {
95-
final int rawArguments = invocation.getRawArguments().length;
96-
final int expandedArguments = invocation.getArguments().length;
97-
final int matcherCount = matchers.size();
98-
99-
if (expandedArguments == matcherCount) {
100-
return ONE_MATCHER_PER_ARGUMENT;
101-
}
102-
103-
if (rawArguments == matcherCount && isLastMatcherVarargMatcher(matchers)) {
104-
return MATCH_EACH_VARARGS_WITH_LAST_MATCHER;
105-
}
106-
107-
return ERROR_UNSUPPORTED_NUMBER_OF_MATCHERS;
108-
}
109-
110-
private static boolean isLastMatcherVarargMatcher(final List<ArgumentMatcher<?>> matchers) {
94+
private static boolean isLastMatcherVarargMatcher(List<? extends ArgumentMatcher<?>> matchers) {
11195
ArgumentMatcher<?> argumentMatcher = lastMatcher(matchers);
11296
if (argumentMatcher instanceof HamcrestArgumentMatcher<?>) {
11397
return ((HamcrestArgumentMatcher<?>) argumentMatcher).isVarargMatcher();
11498
}
11599
return argumentMatcher instanceof VarargMatcher;
116100
}
117101

118-
private static List<ArgumentMatcher<?>> appendLastMatcherNTimes(
119-
List<ArgumentMatcher<?>> matchers, int timesToAppendLastMatcher) {
102+
private List<? extends ArgumentMatcher<?>> appendLastMatcherNTimes(
103+
int timesToAppendLastMatcher) {
120104
ArgumentMatcher<?> lastMatcher = lastMatcher(matchers);
121105

122106
List<ArgumentMatcher<?>> expandedMatchers = new ArrayList<ArgumentMatcher<?>>(matchers);
@@ -132,13 +116,7 @@ private static int varargLength(Invocation invocation) {
132116
return expandedArgumentCount - rawArgumentCount;
133117
}
134118

135-
private static ArgumentMatcher<?> lastMatcher(List<ArgumentMatcher<?>> matchers) {
119+
private static ArgumentMatcher<?> lastMatcher(List<? extends ArgumentMatcher<?>> matchers) {
136120
return matchers.get(matchers.size() - 1);
137121
}
138-
139-
enum MatcherApplicationType {
140-
ONE_MATCHER_PER_ARGUMENT,
141-
MATCH_EACH_VARARGS_WITH_LAST_MATCHER,
142-
ERROR_UNSUPPORTED_NUMBER_OF_MATCHERS;
143-
}
144122
}

src/test/java/org/mockito/internal/invocation/MatcherApplicationStrategyTest.java

+6-5
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public class MatcherApplicationStrategyTest extends TestBase {
3535

3636
@Mock IMethods mock;
3737
private Invocation invocation;
38-
private List matchers;
38+
private List<? extends ArgumentMatcher<?>> matchers;
3939

4040
private RecordingAction recordAction;
4141

@@ -213,7 +213,8 @@ public void shouldMatchAnyEvenIfMatcherIsDecorated() {
213213
public void shouldMatchAnyEvenIfMatcherIsWrappedInHamcrestMatcher() {
214214
// given
215215
invocation = varargs("1", "2");
216-
HamcrestArgumentMatcher argumentMatcher = new HamcrestArgumentMatcher(new IntMatcher());
216+
HamcrestArgumentMatcher<Integer> argumentMatcher =
217+
new HamcrestArgumentMatcher<>(new IntMatcher());
217218
matchers = asList(argumentMatcher);
218219

219220
// when
@@ -224,7 +225,7 @@ public void shouldMatchAnyEvenIfMatcherIsWrappedInHamcrestMatcher() {
224225
recordAction.assertContainsExactly(argumentMatcher, argumentMatcher);
225226
}
226227

227-
class IntMatcher extends BaseMatcher<Integer> implements VarargMatcher {
228+
private static class IntMatcher extends BaseMatcher<Integer> implements VarargMatcher {
228229
public boolean matches(Object o) {
229230
return true;
230231
}
@@ -242,8 +243,8 @@ private Invocation varargs(String... s) {
242243
return getLastInvocation();
243244
}
244245

245-
private class RecordingAction implements ArgumentMatcherAction {
246-
private List<ArgumentMatcher<?>> matchers = new ArrayList<ArgumentMatcher<?>>();
246+
private static class RecordingAction implements ArgumentMatcherAction {
247+
private final List<ArgumentMatcher<?>> matchers = new ArrayList<ArgumentMatcher<?>>();
247248

248249
@Override
249250
public boolean apply(ArgumentMatcher<?> matcher, Object argument) {

0 commit comments

Comments
 (0)