Skip to content

Commit da09c48

Browse files
author
joe.walnes
committed
Added any(Class<T>), null(Class<T>) and notNull(Class<T>) matchers, which returns
Matcher<T>. Helpful when the compiler struggles with type inference.
1 parent 237b808 commit da09c48

File tree

5 files changed

+47
-0
lines changed

5 files changed

+47
-0
lines changed

CHANGES.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@
2020
* Relaxed isInstanceOf() matcher generic type so it can be used on any kind of
2121
object. e.g. assertThat(someUnknownObject, isInstanceOf(String.class));
2222

23+
* Added any(Class<T>), null(Class<T>) and notNull(Class<T>) matchers, which returns
24+
Matcher<T>. Helpful when the compiler struggles with type inference.
25+
2326
* Modified anyOf() and allOf() to accept mixed-types.
2427

2528
* TypeSafeMatcher.matchesSafely() is now public.

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,12 @@ public static <T> Matcher<T> anything() {
4848
public static <T> Matcher<T> anything(String description) {
4949
return new IsAnything<T>(description);
5050
}
51+
52+
/**
53+
* This matcher always evaluates to true. With type inference.
54+
*/
55+
@Factory
56+
public static <T> Matcher<T> any(@SuppressWarnings("unused")Class<T> type) {
57+
return new IsAnything<T>();
58+
}
5159
}

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,5 +35,21 @@ public static <T> Matcher<T> nullValue() {
3535
public static <T> Matcher<T> notNullValue() {
3636
return not(IsNull.<T>nullValue());
3737
}
38+
39+
/**
40+
* Matches if value is null. With type inference.
41+
*/
42+
@Factory
43+
public static <T> Matcher<T> nullValue(@SuppressWarnings("unused") Class<T> type) {
44+
return nullValue();
45+
}
46+
47+
/**
48+
* Matches if value is not null. With type inference.
49+
*/
50+
@Factory
51+
public static <T> Matcher<T> notNullValue(@SuppressWarnings("unused") Class<T> type) {
52+
return notNullValue();
53+
}
3854
}
3955

hamcrest-unit-test/src/main/java/org/hamcrest/core/IsAnythingTest.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
*/
33
package org.hamcrest.core;
44

5+
import static org.hamcrest.core.IsAnything.any;
56
import org.hamcrest.AbstractMatcherTest;
67
import org.hamcrest.Matcher;
78
import static org.hamcrest.MatcherAssert.assertThat;
@@ -27,4 +28,12 @@ public void testCanOverrideDescription() {
2728
String description = "description";
2829
assertDescription(description, anything(description));
2930
}
31+
32+
public void testSupportsStaticTyping() {
33+
requiresStringMatcher(any(String.class));
34+
}
35+
36+
private void requiresStringMatcher(Matcher<String> arg) {
37+
// no-op
38+
}
3039
}

hamcrest-unit-test/src/main/java/org/hamcrest/core/IsNullTest.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
import static org.hamcrest.MatcherAssert.assertThat;
99
import static org.hamcrest.core.IsNull.nullValue;
1010
import static org.hamcrest.core.IsNull.notNullValue;
11+
import static org.hamcrest.core.IsNull.*;
12+
import static org.hamcrest.core.IsAnything.any;
1113

1214
public class IsNullTest extends AbstractMatcherTest {
1315

@@ -22,4 +24,13 @@ public void testEvaluatesToTrueIfArgumentIsNull() {
2224
assertThat(ANY_NON_NULL_ARGUMENT, notNullValue());
2325
assertThat(null, not(notNullValue()));
2426
}
27+
28+
public void testSupportsStaticTyping() {
29+
requiresStringMatcher(nullValue(String.class));
30+
requiresStringMatcher(notNullValue(String.class));
31+
}
32+
33+
private void requiresStringMatcher(Matcher<String> arg) {
34+
// no-op
35+
}
2536
}

0 commit comments

Comments
 (0)