Skip to content

Commit 969cf38

Browse files
author
nat.pryce
committed
TypeSafeMatcher no longer breaks backward compatability.
1 parent 7a28ef8 commit 969cf38

File tree

2 files changed

+30
-16
lines changed

2 files changed

+30
-16
lines changed

hamcrest-core/src/main/java/org/hamcrest/TypeSafeMatcher.java

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,25 +7,20 @@
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
*/
1113
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+
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
}

hamcrest-unit-test/src/main/java/org/hamcrest/TypeSafeMatcherTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public void testCanDetermineMatcherTypeFromProtectedMatchesSafelyMethod() {
2828

2929
public void testDescribesMismatches() {
3030
assertMismatchDescription("was null", null);
31-
assertMismatchDescription("was <3>", new Integer(3));
31+
assertMismatchDescription("was a java.lang.Integer (<3>)", new Integer(3));
3232
assertMismatchDescription("The mismatch", "a string");
3333
}
3434

0 commit comments

Comments
 (0)