Skip to content

Commit 9992dca

Browse files
committed
ConversionUtils: avoid NPEs in canCast methods
The following calls now return true: * canCast((Class) null, Foo.class) -- previously threw NPE * canCast((Object) null, Foo.class) -- already did The following calls now return false: * canCast((Class) null, null) -- previously threw NPE * canCast((Object) null, null) -- previously threw NPE * canCast(new Foo(), null) -- previously threw NPE * canCast(Foo.class, null) -- previously threw NPE This commit also adds tests to ensure these calls continue to work.
1 parent b509f5c commit 9992dca

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

src/main/java/org/scijava/util/ConversionUtils.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,18 +100,25 @@ public static <T> T cast(final Object src, final Class<T> dest) {
100100
* Checks whether objects of the given class can be cast to the specified
101101
* type.
102102
*
103+
* @return true If the destination class is assignable from the source one, or
104+
* if the source class is null and destination class is non-null.
103105
* @see #cast(Object, Class)
104106
*/
105107
public static boolean canCast(final Class<?> src, final Class<?> dest) {
106-
return dest.isAssignableFrom(src);
108+
if (dest == null) return false;
109+
return src == null || dest.isAssignableFrom(src);
107110
}
108111

109112
/**
110113
* Checks whether the given object can be cast to the specified type.
111114
*
115+
* @return true If the destination class is assignable from the source
116+
* object's class, or if the source object is null and destionation
117+
* class is non-null.
112118
* @see #cast(Object, Class)
113119
*/
114120
public static boolean canCast(final Object src, final Class<?> dest) {
121+
if (dest == null) return false;
115122
return src == null || canCast(src.getClass(), dest);
116123
}
117124

src/test/java/org/scijava/util/ConversionUtilsTest.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,18 @@ public void testCanCast() {
7878
assertFalse(ConversionUtils.canCast(int.class, Number.class));
7979

8080
// casting from null always works
81+
final Class<?> nullClass = null;
82+
assertTrue(ConversionUtils.canCast(nullClass, Object.class));
83+
assertTrue(ConversionUtils.canCast(nullClass, int[].class));
8184
final Object nullObject = null;
8285
assertTrue(ConversionUtils.canCast(nullObject, Object.class));
8386
assertTrue(ConversionUtils.canCast(nullObject, int[].class));
87+
88+
// casting to null is not allowed
89+
assertFalse(ConversionUtils.canCast(nullClass, null));
90+
assertFalse(ConversionUtils.canCast(Object.class, null));
91+
assertFalse(ConversionUtils.canCast(nullObject, null));
92+
assertFalse(ConversionUtils.canCast(new Object(), null));
8493
}
8594

8695
/** Tests {@link ConversionUtils#cast(Object, Class)}. */

0 commit comments

Comments
 (0)