7
7
* This simply implements the null check, checks the type and then casts.
8
8
*
9
9
* @author Joe Walnes
10
+ * @author Steve Freeman
11
+ * @author Nat Pryce
10
12
*/
11
13
public abstract class TypeSafeMatcher <T > extends BaseMatcher <T > {
12
- private static final ReflectiveTypeFinder TYPE_FINDER = new ReflectiveTypeFinder ("matchesSafely" , 1 , 0 );
14
+ private static final ReflectiveTypeFinder TYPE_FINDER = new ReflectiveTypeFinder ("matchesSafely" , 1 , 0 );
15
+
13
16
final private Class <?> expectedType ;
14
17
15
- /**
16
- * Subclasses should implement these. The item will already have been checked for
17
- * the specific type and will never be null.
18
- */
19
- protected abstract boolean matchesSafely (T item );
20
- protected abstract void describeMismatchSafely (T item , Description mismatchDescription );
21
-
22
18
/**
23
19
* The default constructor for simple sub types
24
20
*/
25
21
protected TypeSafeMatcher () {
26
22
this (TYPE_FINDER );
27
23
}
28
-
29
24
30
25
/**
31
26
* Use this constructor if the subclass that implements <code>matchesSafely</code>
@@ -35,7 +30,6 @@ protected TypeSafeMatcher() {
35
30
protected TypeSafeMatcher (Class <?> expectedType ) {
36
31
this .expectedType = expectedType ;
37
32
}
38
-
39
33
40
34
/**
41
35
* Use this constructor if the subclass that implements <code>matchesSafely</code>
@@ -46,6 +40,20 @@ protected TypeSafeMatcher(ReflectiveTypeFinder typeFinder) {
46
40
this .expectedType = typeFinder .findExpectedType (getClass ());
47
41
}
48
42
43
+ /**
44
+ * Subclasses should implement this. The item will already have been checked for
45
+ * the specific type and will never be null.
46
+ */
47
+ protected abstract boolean matchesSafely (T item );
48
+
49
+ /**
50
+ * Subclasses should override this. The item will already have been checked for
51
+ * the specific type and will never be null.
52
+ */
53
+ protected void describeMismatchSafely (T item , Description mismatchDescription ) {
54
+ super .describeMismatch (item , mismatchDescription );
55
+ }
56
+
49
57
/**
50
58
* Methods made final to prevent accidental override.
51
59
* If you need to override this, there's no point on extending TypeSafeMatcher.
@@ -61,10 +69,16 @@ public final boolean matches(Object item) {
61
69
@ SuppressWarnings ("unchecked" )
62
70
@ Override
63
71
final public void describeMismatch (Object item , Description description ) {
64
- if (item == null || ! expectedType .isInstance (item )) {
65
- super .describeMismatch (item , description );
66
- } else {
67
- describeMismatchSafely ((T )item , description );
68
- }
72
+ if (item == null ) {
73
+ super .describeMismatch (item , description );
74
+ } else if (! expectedType .isInstance (item )) {
75
+ description .appendText ("was a " )
76
+ .appendText (item .getClass ().getName ())
77
+ .appendText (" (" )
78
+ .appendValue (item )
79
+ .appendText (")" );
80
+ } else {
81
+ describeMismatchSafely ((T )item , description );
82
+ }
69
83
}
70
84
}
0 commit comments