aboutsummaryrefslogtreecommitdiff
path: root/hamcrest-core/src/main/java/org/hamcrest/core/IsInstanceOf.java
diff options
context:
space:
mode:
Diffstat (limited to 'hamcrest-core/src/main/java/org/hamcrest/core/IsInstanceOf.java')
-rw-r--r--hamcrest-core/src/main/java/org/hamcrest/core/IsInstanceOf.java81
1 files changed, 64 insertions, 17 deletions
diff --git a/hamcrest-core/src/main/java/org/hamcrest/core/IsInstanceOf.java b/hamcrest-core/src/main/java/org/hamcrest/core/IsInstanceOf.java
index df20824..5a508c9 100644
--- a/hamcrest-core/src/main/java/org/hamcrest/core/IsInstanceOf.java
+++ b/hamcrest-core/src/main/java/org/hamcrest/core/IsInstanceOf.java
@@ -1,44 +1,91 @@
-/* Copyright (c) 2000-2006 hamcrest.org
- */
package org.hamcrest.core;
import org.hamcrest.Description;
+import org.hamcrest.DiagnosingMatcher;
import org.hamcrest.Matcher;
-import org.hamcrest.Factory;
-import org.hamcrest.BaseMatcher;
/**
* Tests whether the value is an instance of a class.
+ * Classes of basic types will be converted to the relevant "Object" classes
*/
-public class IsInstanceOf extends BaseMatcher<Object> {
- private final Class<?> theClass;
+public class IsInstanceOf extends DiagnosingMatcher<Object> {
+ private final Class<?> expectedClass;
+ private final Class<?> matchableClass;
/**
* Creates a new instance of IsInstanceOf
*
- * @param theClass The predicate evaluates to true for instances of this class
+ * @param expectedClass The predicate evaluates to true for instances of this class
* or one of its subclasses.
*/
- public IsInstanceOf(Class<?> theClass) {
- this.theClass = theClass;
+ public IsInstanceOf(Class<?> expectedClass) {
+ this.expectedClass = expectedClass;
+ this.matchableClass = matchableClass(expectedClass);
+ }
+
+ private static Class<?> matchableClass(Class<?> expectedClass) {
+ if (boolean.class.equals(expectedClass)) return Boolean.class;
+ if (byte.class.equals(expectedClass)) return Byte.class;
+ if (char.class.equals(expectedClass)) return Character.class;
+ if (double.class.equals(expectedClass)) return Double.class;
+ if (float.class.equals(expectedClass)) return Float.class;
+ if (int.class.equals(expectedClass)) return Integer.class;
+ if (long.class.equals(expectedClass)) return Long.class;
+ if (short.class.equals(expectedClass)) return Short.class;
+ return expectedClass;
}
- public boolean matches(Object item) {
- return theClass.isInstance(item);
+ @Override
+ protected boolean matches(Object item, Description mismatch) {
+ if (null == item) {
+ mismatch.appendText("null");
+ return false;
+ }
+
+ if (!matchableClass.isInstance(item)) {
+ mismatch.appendValue(item).appendText(" is a " + item.getClass().getName());
+ return false;
+ }
+
+ return true;
}
+ @Override
public void describeTo(Description description) {
- description.appendText("an instance of ")
- .appendText(theClass.getName());
+ description.appendText("an instance of ").appendText(expectedClass.getName());
}
/**
- * Is the value an instance of a particular type?
+ * Creates a matcher that matches when the examined object is an instance of the specified <code>type</code>,
+ * as determined by calling the {@link java.lang.Class#isInstance(Object)} method on that type, passing the
+ * the examined object.
+ *
+ * <p>The created matcher assumes no relationship between specified type and the examined object.</p>
+ * For example:
+ * <pre>assertThat(new Canoe(), instanceOf(Paddlable.class));</pre>
+ *
+ */
+ @SuppressWarnings("unchecked")
+ public static <T> Matcher<T> instanceOf(Class<?> type) {
+ return (Matcher<T>) new IsInstanceOf(type);
+ }
+
+ /**
+ * Creates a matcher that matches when the examined object is an instance of the specified <code>type</code>,
+ * as determined by calling the {@link java.lang.Class#isInstance(Object)} method on that type, passing the
+ * the examined object.
+ *
+ * <p>The created matcher forces a relationship between specified type and the examined object, and should be
+ * used when it is necessary to make generics conform, for example in the JMock clause
+ * <code>with(any(Thing.class))</code></p>
+ * For example:
+ * <pre>assertThat(new Canoe(), instanceOf(Canoe.class));</pre>
+ *
*/
- @Factory
- public static Matcher<Object> instanceOf(Class<?> type) {
- return new IsInstanceOf(type);
+ @SuppressWarnings("unchecked")
+ public static <T> Matcher<T> any(Class<T> type) {
+ return (Matcher<T>) new IsInstanceOf(type);
}
}