Skip to content

Commit 9afa622

Browse files
authored
Merge pull request #256 from scijava/modify-cancast
Modify cancast
2 parents 1f60f5a + b72870d commit 9afa622

File tree

3 files changed

+60
-13
lines changed

3 files changed

+60
-13
lines changed

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

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -97,26 +97,46 @@ public static <T> T cast(final Object src, final Class<T> dest) {
9797
}
9898

9999
/**
100-
* Checks whether objects of the given class can be cast to the specified
101-
* type.
102-
*
100+
* Checks whether objects of the given class can be assigned to the specified
101+
* type. Unlike {@link Class#isAssignableFrom(Class)}, this method considers
102+
* auto-unboxing.
103+
*
103104
* @return true If the destination class is assignable from the source one, or
104105
* if the source class is null and destination class is non-null.
105-
* @see #cast(Object, Class)
106106
*/
107-
public static boolean canCast(final Class<?> src, final Class<?> dest) {
108-
if (dest == null) return false;
109-
return src == null || dest.isAssignableFrom(src);
107+
public static boolean canAssign(final Class<?> src, final Class<?> dest) {
108+
return canCast(src, dest);
110109
}
111110

112111
/**
113-
* Checks whether the given object can be cast to the specified type.
114-
*
112+
* Checks whether the given object can be assigned to the specified type.
113+
* Unlike {@link Class#isAssignableFrom(Class)}, this method considers
114+
* auto-unboxing.
115+
*
115116
* @return true If the destination class is assignable from the source
116117
* object's class, or if the source object is null and destionation
117118
* class is non-null.
118-
* @see #cast(Object, Class)
119119
*/
120+
public static boolean canAssign(final Object src, final Class<?> dest) {
121+
return canCast(src, dest);
122+
}
123+
124+
/**
125+
* @deprecated use {@link #canAssign(Class, Class)} instead
126+
*/
127+
@Deprecated
128+
public static boolean canCast(final Class<?> src, final Class<?> dest) {
129+
if (dest == null) return false;
130+
if (src == null) return true;
131+
final Class<?> saneSrc = getNonprimitiveType(src);
132+
final Class<?> saneDest = getNonprimitiveType(dest);
133+
return saneDest.isAssignableFrom(saneSrc);
134+
}
135+
136+
/**
137+
* @deprecated use {@link #canAssign(Object, Class)} instead
138+
*/
139+
@Deprecated
120140
public static boolean canCast(final Object src, final Class<?> dest) {
121141
if (dest == null) return false;
122142
return src == null || canCast(src.getClass(), dest);

src/test/java/org/scijava/convert/ConvertServiceTest.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -498,6 +498,22 @@ class Struct {
498498
setFieldValue(struct, "singleValue", new int[] { 4, 8, 2 });
499499
}
500500

501+
/**
502+
* Test behavior when setting a single element field with a constructor that
503+
* accepts a primitive type (int).
504+
*/
505+
@Test
506+
public void testLegitimateSingletonInt() {
507+
class Struct {
508+
509+
private IntWrapper intWrapper;
510+
}
511+
final Struct struct = new Struct();
512+
513+
setFieldValue(struct, "intWrapper", Integer.valueOf(12));
514+
assertNotNull(struct.intWrapper);
515+
}
516+
501517
/**
502518
* Test behavior when setting a single element field with a constructor that
503519
* accepts a primitive array.
@@ -628,6 +644,18 @@ private static interface INumberList extends List<Number> {
628644
// NB: No implementation needed.
629645
}
630646

647+
/**
648+
* Dummy class with an int constructor to ensure that when converting using
649+
* constructors, auto-unboxing should work as expected.
650+
*/
651+
public static class IntWrapper {
652+
653+
@SuppressWarnings("unused")
654+
public IntWrapper(final int gonnaWrapThisInt) {
655+
// nothing to do
656+
}
657+
}
658+
631659
/**
632660
* Dummy class with an array constructor to ensure that the logic to
633661
* recursively convert arrays doesn't consume the array improperly when it

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,8 @@ public void testCanCast() {
7373
assertFalse(ConversionUtils.canCast(double.class, float.class));
7474
assertFalse(ConversionUtils.canCast(float.class, double.class));
7575

76-
// boxing is not reported to work
77-
// TODO: Consider changing this behavior.
78-
assertFalse(ConversionUtils.canCast(int.class, Number.class));
76+
// check casting with boxing
77+
assertTrue(ConversionUtils.canCast(int.class, Number.class));
7978

8079
// casting from null always works
8180
final Class<?> nullClass = null;

0 commit comments

Comments
 (0)