11package org .hamcrest ;
22
3- import java . lang . reflect . Method ;
3+ import org . hamcrest . internal . ReflectiveTypeFinder ;
44
55/**
66 * Convenient base class for Matchers that require a non-null value of a specific type.
99 * @author Joe Walnes
1010 */
1111public abstract class TypeSafeMatcher <T > extends BaseMatcher <T > {
12- private Class <?> expectedType ;
12+ private static final ReflectiveTypeFinder TYPE_FINDER = new ReflectiveTypeFinder ("matchesSafely" , 1 , 0 );
13+ final private Class <?> expectedType ;
1314
1415 /**
15- * Subclasses should implement this . The item will already have been checked for
16+ * Subclasses should implement these . The item will already have been checked for
1617 * the specific type and will never be null.
1718 */
18- public abstract boolean matchesSafely (T item );
19-
20- protected TypeSafeMatcher () {
21- expectedType = findExpectedType (getClass ());
22- }
23-
24- private static Class <?> findExpectedType (Class <?> fromClass ) {
25- for (Class <?> c = fromClass ; c != Object .class ; c = c .getSuperclass ()) {
26- for (Method method : c .getDeclaredMethods ()) {
27- if (isMatchesSafelyMethod (method )) {
28- return method .getParameterTypes ()[0 ];
29- }
30- }
31- }
32-
33- throw new Error ("Cannot determine correct type for matchesSafely() method." );
34- }
19+ protected abstract boolean matchesSafely (T item );
20+ protected abstract void describeMismatchSafely (T item , Description mismatchDescription );
3521
36- private static boolean isMatchesSafelyMethod (Method method ) {
37- return method .getName ().equals ("matchesSafely" )
38- && method .getParameterTypes ().length == 1
39- && !method .isSynthetic ();
22+ /**
23+ * The default constructor for simple sub types
24+ */
25+ protected TypeSafeMatcher () {
26+ this (TYPE_FINDER );
4027 }
4128
42- protected TypeSafeMatcher (Class <T > expectedType ) {
29+
30+ /**
31+ * Use this constructor if the subclass that implements <code>matchesSafely</code>
32+ * is <em>not</em> the class that binds <T> to a type.
33+ * @param expectedType The expectedType of the actual value.
34+ */
35+ protected TypeSafeMatcher (Class <?> expectedType ) {
4336 this .expectedType = expectedType ;
4437 }
4538
39+
40+ /**
41+ * Use this constructor if the subclass that implements <code>matchesSafely</code>
42+ * is <em>not</em> the class that binds <T> to a type.
43+ * @param typeFinder A type finder to extract the type
44+ */
45+ protected TypeSafeMatcher (ReflectiveTypeFinder typeFinder ) {
46+ this .expectedType = typeFinder .findExpectedType (getClass ());
47+ }
48+
4649 /**
47- * Method made final to prevent accidental override.
50+ * Methods made final to prevent accidental override.
4851 * If you need to override this, there's no point on extending TypeSafeMatcher.
4952 * Instead, extend the {@link BaseMatcher}.
5053 */
@@ -54,4 +57,14 @@ public final boolean matches(Object item) {
5457 && expectedType .isInstance (item )
5558 && matchesSafely ((T ) item );
5659 }
60+
61+ @ SuppressWarnings ("unchecked" )
62+ @ Override
63+ 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+ }
69+ }
5770}
0 commit comments