1
2
3
4
5 package mockit.internal.expectations.argumentMatching;
6
7 import edu.umd.cs.findbugs.annotations.NonNull;
8 import edu.umd.cs.findbugs.annotations.Nullable;
9
10 import java.lang.reflect.Method;
11
12 import mockit.Delegate;
13 import mockit.internal.reflection.MethodReflection;
14
15 public final class ReflectiveMatcher implements ArgumentMatcher<ReflectiveMatcher> {
16 @NonNull
17 private final Delegate<?> delegate;
18 @Nullable
19 private Method handlerMethod;
20 @Nullable
21 private Object matchedValue;
22
23 public ReflectiveMatcher(@NonNull Delegate<?> delegate) {
24 this.delegate = delegate;
25 }
26
27 @Override
28 public boolean same(@NonNull ReflectiveMatcher other) {
29 return delegate == other.delegate;
30 }
31
32 @Override
33 public boolean matches(@Nullable Object argValue) {
34 if (handlerMethod == null) {
35 handlerMethod = MethodReflection.findNonPrivateHandlerMethod(delegate);
36 }
37
38 matchedValue = argValue;
39 Boolean result = MethodReflection.invoke(delegate, handlerMethod, argValue);
40
41 return result == null || result;
42 }
43
44 @Override
45 public void writeMismatchPhrase(@NonNull ArgumentMismatch argumentMismatch) {
46 if (handlerMethod != null) {
47 argumentMismatch.append(handlerMethod.getName()).append('(');
48 argumentMismatch.appendFormatted(matchedValue);
49 argumentMismatch.append(") (should return true, was false)");
50 } else {
51 argumentMismatch.append('?');
52 }
53 }
54 }