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