77 * This simply implements the null check, checks the type and then casts.
88 *
99 * @author Joe Walnes
10+ * @author Steve Freeman
11+ * @author Nat Pryce
1012 */
1113public 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+
1316 final private Class <?> expectedType ;
1417
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-
2218 /**
2319 * The default constructor for simple sub types
2420 */
2521 protected TypeSafeMatcher () {
2622 this (TYPE_FINDER );
2723 }
28-
2924
3025 /**
3126 * Use this constructor if the subclass that implements <code>matchesSafely</code>
@@ -35,7 +30,6 @@ protected TypeSafeMatcher() {
3530 protected TypeSafeMatcher (Class <?> expectedType ) {
3631 this .expectedType = expectedType ;
3732 }
38-
3933
4034 /**
4135 * Use this constructor if the subclass that implements <code>matchesSafely</code>
@@ -46,6 +40,20 @@ protected TypeSafeMatcher(ReflectiveTypeFinder typeFinder) {
4640 this .expectedType = typeFinder .findExpectedType (getClass ());
4741 }
4842
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+
4957 /**
5058 * Methods made final to prevent accidental override.
5159 * If you need to override this, there's no point on extending TypeSafeMatcher.
@@ -61,10 +69,16 @@ public final boolean matches(Object item) {
6169 @ SuppressWarnings ("unchecked" )
6270 @ Override
6371 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+ }
6983 }
7084}
0 commit comments