10
10
*
11
11
* @param <T>
12
12
* @author Neil Dunn
13
+ * @author Nat Pryce
13
14
*/
14
15
public abstract class TypeSafeDiagnosingMatcher <T > extends BaseMatcher <T > {
15
16
private final Class <?> expectedType ;
@@ -31,28 +32,31 @@ public final boolean matches(Object item) {
31
32
&& matchesSafely ((T ) item , new Description .NullDescription ());
32
33
}
33
34
34
- @ SuppressWarnings ("unchecked" )
35
35
@ Override
36
+ @ SuppressWarnings ("unchecked" )
36
37
public final void describeMismatch (Object item , Description mismatchDescription ) {
37
38
matchesSafely ((T ) item , mismatchDescription );
38
39
}
39
-
40
- private static Class <?> findExpectedType (Class <?> fromClass ) {
40
+
41
+ private Class <?> findExpectedType (Class <?> fromClass ) {
41
42
for (Class <?> c = fromClass ; c != Object .class ; c = c .getSuperclass ()) {
42
43
for (Method method : c .getDeclaredMethods ()) {
43
- if (isMatchesSafelyMethod (method )) {
44
- return method . getParameterTypes ()[ 0 ] ;
44
+ if (canObtainExpectedTypeFrom (method )) {
45
+ return obtainExpectedTypeFrom ( method ) ;
45
46
}
46
47
}
47
48
}
48
-
49
+
49
50
throw new Error ("Cannot determine correct type for matchesSafely() method." );
50
51
}
51
-
52
- private static boolean isMatchesSafelyMethod (Method method ) {
52
+
53
+ protected boolean canObtainExpectedTypeFrom (Method method ) {
53
54
return method .getName ().equals ("matchesSafely" )
54
55
&& method .getParameterTypes ().length == 2
55
56
&& !method .isSynthetic ();
56
57
}
57
-
58
+
59
+ protected Class <?> obtainExpectedTypeFrom (Method method ) {
60
+ return method .getParameterTypes ()[0 ];
61
+ }
58
62
}
0 commit comments