merge ClassUtils isAssignable w/ autoboxing from LANG_POST_2_4 branch

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/lang/trunk@653907 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Matthew Jason Benson 2008-05-06 20:26:08 +00:00
parent 290731e746
commit 8c7546be27
2 changed files with 275 additions and 87 deletions

View File

@ -398,6 +398,42 @@ public class ClassUtils {
* @return <code>true</code> if assignment possible * @return <code>true</code> if assignment possible
*/ */
public static boolean isAssignable(Class[] classArray, Class[] toClassArray) { public static boolean isAssignable(Class[] classArray, Class[] toClassArray) {
return isAssignable(classArray, toClassArray, false);
}
/**
* <p>Checks if an array of Classes can be assigned to another array of Classes.</p>
*
* <p>This method calls {@link #isAssignable(Class, Class) isAssignable} for each
* Class pair in the input arrays. It can be used to check if a set of arguments
* (the first parameter) are suitably compatible with a set of method parameter types
* (the second parameter).</p>
*
* <p>Unlike the {@link Class#isAssignableFrom(java.lang.Class)} method, this
* method takes into account widenings of primitive classes and
* <code>null</code>s.</p>
*
* <p>Primitive widenings allow an int to be assigned to a <code>long</code>,
* <code>float</code> or <code>double</code>. This method returns the correct
* result for these cases.</p>
*
* <p><code>Null</code> may be assigned to any reference type. This method will
* return <code>true</code> if <code>null</code> is passed in and the toClass is
* non-primitive.</p>
*
* <p>Specifically, this method tests whether the type represented by the
* specified <code>Class</code> parameter can be converted to the type
* represented by this <code>Class</code> object via an identity conversion
* widening primitive or widening reference conversion. See
* <em><a href="http://java.sun.com/docs/books/jls/">The Java Language Specification</a></em>,
* sections 5.1.1, 5.1.2 and 5.1.4 for details.</p>
*
* @param classArray the array of Classes to check, may be <code>null</code>
* @param toClassArray the array of Classes to try to assign into, may be <code>null</code>
* @param autoboxing whether to use implicit autoboxing/unboxing between primitives and wrappers
* @return <code>true</code> if assignment possible
*/
public static boolean isAssignable(Class[] classArray, Class[] toClassArray, boolean autoboxing) {
if (ArrayUtils.isSameLength(classArray, toClassArray) == false) { if (ArrayUtils.isSameLength(classArray, toClassArray) == false) {
return false; return false;
} }
@ -408,7 +444,7 @@ public class ClassUtils {
toClassArray = ArrayUtils.EMPTY_CLASS_ARRAY; toClassArray = ArrayUtils.EMPTY_CLASS_ARRAY;
} }
for (int i = 0; i < classArray.length; i++) { for (int i = 0; i < classArray.length; i++) {
if (isAssignable(classArray[i], toClassArray[i]) == false) { if (isAssignable(classArray[i], toClassArray[i], autoboxing) == false) {
return false; return false;
} }
} }
@ -442,6 +478,37 @@ public class ClassUtils {
* @return <code>true</code> if assignment possible * @return <code>true</code> if assignment possible
*/ */
public static boolean isAssignable(Class cls, Class toClass) { public static boolean isAssignable(Class cls, Class toClass) {
return isAssignable(cls, toClass, false);
}
/**
* <p>Checks if one <code>Class</code> can be assigned to a variable of
* another <code>Class</code>.</p>
*
* <p>Unlike the {@link Class#isAssignableFrom(java.lang.Class)} method,
* this method takes into account widenings of primitive classes and
* <code>null</code>s.</p>
*
* <p>Primitive widenings allow an int to be assigned to a long, float or
* double. This method returns the correct result for these cases.</p>
*
* <p><code>Null</code> may be assigned to any reference type. This method
* will return <code>true</code> if <code>null</code> is passed in and the
* toClass is non-primitive.</p>
*
* <p>Specifically, this method tests whether the type represented by the
* specified <code>Class</code> parameter can be converted to the type
* represented by this <code>Class</code> object via an identity conversion
* widening primitive or widening reference conversion. See
* <em><a href="http://java.sun.com/docs/books/jls/">The Java Language Specification</a></em>,
* sections 5.1.1, 5.1.2 and 5.1.4 for details.</p>
*
* @param cls the Class to check, may be null
* @param toClass the Class to try to assign into, returns false if null
* @param autoboxing whether to use implicit autoboxing/unboxing between primitives and wrappers
* @return <code>true</code> if assignment possible
*/
public static boolean isAssignable(Class cls, Class toClass, boolean autoboxing) {
if (toClass == null) { if (toClass == null) {
return false; return false;
} }
@ -449,6 +516,21 @@ public class ClassUtils {
if (cls == null) { if (cls == null) {
return !(toClass.isPrimitive()); return !(toClass.isPrimitive());
} }
//autoboxing:
if (autoboxing) {
if (cls.isPrimitive() && !toClass.isPrimitive()) {
cls = primitiveToWrapper(cls);
if (cls == null) {
return false;
}
}
if (toClass.isPrimitive() && !cls.isPrimitive()) {
cls = wrapperToPrimitive(cls);
if (cls == null) {
return false;
}
}
}
if (cls.equals(toClass)) { if (cls.equals(toClass)) {
return true; return true;
} }

View File

@ -222,114 +222,220 @@ public class ClassUtilsTest extends TestCase {
Class[] array1s = new Class[] {String.class}; Class[] array1s = new Class[] {String.class};
Class[] array0 = new Class[] {}; Class[] array0 = new Class[] {};
assertEquals(false, ClassUtils.isAssignable(array1, array2)); assertFalse(ClassUtils.isAssignable(array1, array2));
assertEquals(false, ClassUtils.isAssignable(null, array2)); assertFalse(ClassUtils.isAssignable(null, array2));
assertEquals(true, ClassUtils.isAssignable(null, array0)); assertTrue(ClassUtils.isAssignable(null, array0));
assertEquals(true, ClassUtils.isAssignable(array0, array0)); assertTrue(ClassUtils.isAssignable(array0, array0));
assertEquals(true, ClassUtils.isAssignable(array0, null)); assertTrue(ClassUtils.isAssignable(array0, null));
assertEquals(true, ClassUtils.isAssignable((Class[]) null, (Class[]) null)); assertTrue(ClassUtils.isAssignable((Class[]) null, (Class[]) null));
assertEquals(false, ClassUtils.isAssignable(array1, array1s)); assertFalse(ClassUtils.isAssignable(array1, array1s));
assertEquals(true, ClassUtils.isAssignable(array1s, array1s)); assertTrue(ClassUtils.isAssignable(array1s, array1s));
assertEquals(true, ClassUtils.isAssignable(array1s, array1)); assertTrue(ClassUtils.isAssignable(array1s, array1));
} }
public void test_isAssignable() throws Exception { public void test_isAssignable() throws Exception {
assertEquals(false, ClassUtils.isAssignable((Class) null, null)); assertFalse(ClassUtils.isAssignable((Class) null, null));
assertEquals(false, ClassUtils.isAssignable(String.class, null)); assertFalse(ClassUtils.isAssignable(String.class, null));
assertEquals(true, ClassUtils.isAssignable(null, Object.class)); assertTrue(ClassUtils.isAssignable(null, Object.class));
assertEquals(true, ClassUtils.isAssignable(null, Integer.class)); assertTrue(ClassUtils.isAssignable(null, Integer.class));
assertEquals(false, ClassUtils.isAssignable(null, Integer.TYPE)); assertFalse(ClassUtils.isAssignable(null, Integer.TYPE));
assertEquals(true, ClassUtils.isAssignable(String.class, Object.class)); assertTrue(ClassUtils.isAssignable(String.class, Object.class));
assertEquals(true, ClassUtils.isAssignable(String.class, String.class)); assertTrue(ClassUtils.isAssignable(String.class, String.class));
assertEquals(false, ClassUtils.isAssignable(Object.class, String.class)); assertFalse(ClassUtils.isAssignable(Object.class, String.class));
assertEquals(false, ClassUtils.isAssignable(Integer.TYPE, Integer.class)); assertFalse(ClassUtils.isAssignable(Integer.TYPE, Integer.class));
assertEquals(false, ClassUtils.isAssignable(Integer.class, Integer.TYPE)); assertFalse(ClassUtils.isAssignable(Integer.class, Integer.TYPE));
assertEquals(true, ClassUtils.isAssignable(Integer.TYPE, Integer.TYPE)); assertTrue(ClassUtils.isAssignable(Integer.TYPE, Integer.TYPE));
assertEquals(true, ClassUtils.isAssignable(Integer.class, Integer.class)); assertTrue(ClassUtils.isAssignable(Integer.class, Integer.class));
assertFalse(ClassUtils.isAssignable(Boolean.TYPE, Boolean.class));
assertFalse(ClassUtils.isAssignable(Boolean.class, Boolean.TYPE));
assertTrue(ClassUtils.isAssignable(Boolean.TYPE, Boolean.TYPE));
assertTrue(ClassUtils.isAssignable(Boolean.class, Boolean.class));
} }
public void test_isAssignable_Autoboxing() throws Exception {
assertFalse(ClassUtils.isAssignable((Class) null, null, true));
assertFalse(ClassUtils.isAssignable(String.class, null, true));
assertTrue(ClassUtils.isAssignable(null, Object.class, true));
assertTrue(ClassUtils.isAssignable(null, Integer.class, true));
assertFalse(ClassUtils.isAssignable(null, Integer.TYPE, true));
assertTrue(ClassUtils.isAssignable(String.class, Object.class, true));
assertTrue(ClassUtils.isAssignable(String.class, String.class, true));
assertFalse(ClassUtils.isAssignable(Object.class, String.class, true));
assertTrue(ClassUtils.isAssignable(Integer.TYPE, Integer.class, true));
assertTrue(ClassUtils.isAssignable(Integer.class, Integer.TYPE, true));
assertTrue(ClassUtils.isAssignable(Integer.TYPE, Integer.TYPE, true));
assertTrue(ClassUtils.isAssignable(Integer.class, Integer.class, true));
assertTrue(ClassUtils.isAssignable(Boolean.TYPE, Boolean.class, true));
assertTrue(ClassUtils.isAssignable(Boolean.class, Boolean.TYPE, true));
assertTrue(ClassUtils.isAssignable(Boolean.TYPE, Boolean.TYPE, true));
assertTrue(ClassUtils.isAssignable(Boolean.class, Boolean.class, true));
}
public void test_isAssignable_Widening() throws Exception { public void test_isAssignable_Widening() throws Exception {
// test byte conversions // test byte conversions
assertEquals("byte -> char", false, ClassUtils.isAssignable(Byte.TYPE, Character.TYPE)); assertFalse("byte -> char", ClassUtils.isAssignable(Byte.TYPE, Character.TYPE));
assertEquals("byte -> byte", true, ClassUtils.isAssignable(Byte.TYPE, Byte.TYPE)); assertTrue("byte -> byte", ClassUtils.isAssignable(Byte.TYPE, Byte.TYPE));
assertEquals("byte -> short", true, ClassUtils.isAssignable(Byte.TYPE, Short.TYPE)); assertTrue("byte -> short", ClassUtils.isAssignable(Byte.TYPE, Short.TYPE));
assertEquals("byte -> int", true, ClassUtils.isAssignable(Byte.TYPE, Integer.TYPE)); assertTrue("byte -> int", ClassUtils.isAssignable(Byte.TYPE, Integer.TYPE));
assertEquals("byte -> long", true, ClassUtils.isAssignable(Byte.TYPE, Long.TYPE)); assertTrue("byte -> long", ClassUtils.isAssignable(Byte.TYPE, Long.TYPE));
assertEquals("byte -> float", true, ClassUtils.isAssignable(Byte.TYPE, Float.TYPE)); assertTrue("byte -> float", ClassUtils.isAssignable(Byte.TYPE, Float.TYPE));
assertEquals("byte -> double", true, ClassUtils.isAssignable(Byte.TYPE, Double.TYPE)); assertTrue("byte -> double", ClassUtils.isAssignable(Byte.TYPE, Double.TYPE));
assertEquals("byte -> boolean", false, ClassUtils.isAssignable(Byte.TYPE, Boolean.TYPE)); assertFalse("byte -> boolean", ClassUtils.isAssignable(Byte.TYPE, Boolean.TYPE));
// test short conversions // test short conversions
assertEquals("short -> char", false, ClassUtils.isAssignable(Short.TYPE, Character.TYPE)); assertFalse("short -> char", ClassUtils.isAssignable(Short.TYPE, Character.TYPE));
assertEquals("short -> byte", false, ClassUtils.isAssignable(Short.TYPE, Byte.TYPE)); assertFalse("short -> byte", ClassUtils.isAssignable(Short.TYPE, Byte.TYPE));
assertEquals("short -> short", true, ClassUtils.isAssignable(Short.TYPE, Short.TYPE)); assertTrue("short -> short", ClassUtils.isAssignable(Short.TYPE, Short.TYPE));
assertEquals("short -> int", true, ClassUtils.isAssignable(Short.TYPE, Integer.TYPE)); assertTrue("short -> int", ClassUtils.isAssignable(Short.TYPE, Integer.TYPE));
assertEquals("short -> long", true, ClassUtils.isAssignable(Short.TYPE, Long.TYPE)); assertTrue("short -> long", ClassUtils.isAssignable(Short.TYPE, Long.TYPE));
assertEquals("short -> float", true, ClassUtils.isAssignable(Short.TYPE, Float.TYPE)); assertTrue("short -> float", ClassUtils.isAssignable(Short.TYPE, Float.TYPE));
assertEquals("short -> double", true, ClassUtils.isAssignable(Short.TYPE, Double.TYPE)); assertTrue("short -> double", ClassUtils.isAssignable(Short.TYPE, Double.TYPE));
assertEquals("short -> boolean", false, ClassUtils.isAssignable(Short.TYPE, Boolean.TYPE)); assertFalse("short -> boolean", ClassUtils.isAssignable(Short.TYPE, Boolean.TYPE));
// test char conversions // test char conversions
assertEquals("char -> char", true, ClassUtils.isAssignable(Character.TYPE, Character.TYPE)); assertTrue("char -> char", ClassUtils.isAssignable(Character.TYPE, Character.TYPE));
assertEquals("char -> byte", false, ClassUtils.isAssignable(Character.TYPE, Byte.TYPE)); assertFalse("char -> byte", ClassUtils.isAssignable(Character.TYPE, Byte.TYPE));
assertEquals("char -> short", false, ClassUtils.isAssignable(Character.TYPE, Short.TYPE)); assertFalse("char -> short", ClassUtils.isAssignable(Character.TYPE, Short.TYPE));
assertEquals("char -> int", true, ClassUtils.isAssignable(Character.TYPE, Integer.TYPE)); assertTrue("char -> int", ClassUtils.isAssignable(Character.TYPE, Integer.TYPE));
assertEquals("char -> long", true, ClassUtils.isAssignable(Character.TYPE, Long.TYPE)); assertTrue("char -> long", ClassUtils.isAssignable(Character.TYPE, Long.TYPE));
assertEquals("char -> float", true, ClassUtils.isAssignable(Character.TYPE, Float.TYPE)); assertTrue("char -> float", ClassUtils.isAssignable(Character.TYPE, Float.TYPE));
assertEquals("char -> double", true, ClassUtils.isAssignable(Character.TYPE, Double.TYPE)); assertTrue("char -> double", ClassUtils.isAssignable(Character.TYPE, Double.TYPE));
assertEquals("char -> boolean", false, ClassUtils.isAssignable(Character.TYPE, Boolean.TYPE)); assertFalse("char -> boolean", ClassUtils.isAssignable(Character.TYPE, Boolean.TYPE));
// test int conversions // test int conversions
assertEquals("int -> char", false, ClassUtils.isAssignable(Integer.TYPE, Character.TYPE)); assertFalse("int -> char", ClassUtils.isAssignable(Integer.TYPE, Character.TYPE));
assertEquals("int -> byte", false, ClassUtils.isAssignable(Integer.TYPE, Byte.TYPE)); assertFalse("int -> byte", ClassUtils.isAssignable(Integer.TYPE, Byte.TYPE));
assertEquals("int -> short", false, ClassUtils.isAssignable(Integer.TYPE, Short.TYPE)); assertFalse("int -> short", ClassUtils.isAssignable(Integer.TYPE, Short.TYPE));
assertEquals("int -> int", true, ClassUtils.isAssignable(Integer.TYPE, Integer.TYPE)); assertTrue("int -> int", ClassUtils.isAssignable(Integer.TYPE, Integer.TYPE));
assertEquals("int -> long", true, ClassUtils.isAssignable(Integer.TYPE, Long.TYPE)); assertTrue("int -> long", ClassUtils.isAssignable(Integer.TYPE, Long.TYPE));
assertEquals("int -> float", true, ClassUtils.isAssignable(Integer.TYPE, Float.TYPE)); assertTrue("int -> float", ClassUtils.isAssignable(Integer.TYPE, Float.TYPE));
assertEquals("int -> double", true, ClassUtils.isAssignable(Integer.TYPE, Double.TYPE)); assertTrue("int -> double", ClassUtils.isAssignable(Integer.TYPE, Double.TYPE));
assertEquals("int -> boolean", false, ClassUtils.isAssignable(Integer.TYPE, Boolean.TYPE)); assertFalse("int -> boolean", ClassUtils.isAssignable(Integer.TYPE, Boolean.TYPE));
// test long conversions // test long conversions
assertEquals("long -> char", false, ClassUtils.isAssignable(Long.TYPE, Character.TYPE)); assertFalse("long -> char", ClassUtils.isAssignable(Long.TYPE, Character.TYPE));
assertEquals("long -> byte", false, ClassUtils.isAssignable(Long.TYPE, Byte.TYPE)); assertFalse("long -> byte", ClassUtils.isAssignable(Long.TYPE, Byte.TYPE));
assertEquals("long -> short", false, ClassUtils.isAssignable(Long.TYPE, Short.TYPE)); assertFalse("long -> short", ClassUtils.isAssignable(Long.TYPE, Short.TYPE));
assertEquals("long -> int", false, ClassUtils.isAssignable(Long.TYPE, Integer.TYPE)); assertFalse("long -> int", ClassUtils.isAssignable(Long.TYPE, Integer.TYPE));
assertEquals("long -> long", true, ClassUtils.isAssignable(Long.TYPE, Long.TYPE)); assertTrue("long -> long", ClassUtils.isAssignable(Long.TYPE, Long.TYPE));
assertEquals("long -> float", true, ClassUtils.isAssignable(Long.TYPE, Float.TYPE)); assertTrue("long -> float", ClassUtils.isAssignable(Long.TYPE, Float.TYPE));
assertEquals("long -> double", true, ClassUtils.isAssignable(Long.TYPE, Double.TYPE)); assertTrue("long -> double", ClassUtils.isAssignable(Long.TYPE, Double.TYPE));
assertEquals("long -> boolean", false, ClassUtils.isAssignable(Long.TYPE, Boolean.TYPE)); assertFalse("long -> boolean", ClassUtils.isAssignable(Long.TYPE, Boolean.TYPE));
// test float conversions // test float conversions
assertEquals("float -> char", false, ClassUtils.isAssignable(Float.TYPE, Character.TYPE)); assertFalse("float -> char", ClassUtils.isAssignable(Float.TYPE, Character.TYPE));
assertEquals("float -> byte", false, ClassUtils.isAssignable(Float.TYPE, Byte.TYPE)); assertFalse("float -> byte", ClassUtils.isAssignable(Float.TYPE, Byte.TYPE));
assertEquals("float -> short", false, ClassUtils.isAssignable(Float.TYPE, Short.TYPE)); assertFalse("float -> short", ClassUtils.isAssignable(Float.TYPE, Short.TYPE));
assertEquals("float -> int", false, ClassUtils.isAssignable(Float.TYPE, Integer.TYPE)); assertFalse("float -> int", ClassUtils.isAssignable(Float.TYPE, Integer.TYPE));
assertEquals("float -> long", false, ClassUtils.isAssignable(Float.TYPE, Long.TYPE)); assertFalse("float -> long", ClassUtils.isAssignable(Float.TYPE, Long.TYPE));
assertEquals("float -> float", true, ClassUtils.isAssignable(Float.TYPE, Float.TYPE)); assertTrue("float -> float", ClassUtils.isAssignable(Float.TYPE, Float.TYPE));
assertEquals("float -> double", true, ClassUtils.isAssignable(Float.TYPE, Double.TYPE)); assertTrue("float -> double", ClassUtils.isAssignable(Float.TYPE, Double.TYPE));
assertEquals("float -> boolean", false, ClassUtils.isAssignable(Float.TYPE, Boolean.TYPE)); assertFalse("float -> boolean", ClassUtils.isAssignable(Float.TYPE, Boolean.TYPE));
// test double conversions
assertFalse("double -> char", ClassUtils.isAssignable(Double.TYPE, Character.TYPE));
assertFalse("double -> byte", ClassUtils.isAssignable(Double.TYPE, Byte.TYPE));
assertFalse("double -> short", ClassUtils.isAssignable(Double.TYPE, Short.TYPE));
assertFalse("double -> int", ClassUtils.isAssignable(Double.TYPE, Integer.TYPE));
assertFalse("double -> long", ClassUtils.isAssignable(Double.TYPE, Long.TYPE));
assertFalse("double -> float", ClassUtils.isAssignable(Double.TYPE, Float.TYPE));
assertTrue("double -> double", ClassUtils.isAssignable(Double.TYPE, Double.TYPE));
assertFalse("double -> boolean", ClassUtils.isAssignable(Double.TYPE, Boolean.TYPE));
// test boolean conversions
assertFalse("boolean -> char", ClassUtils.isAssignable(Boolean.TYPE, Character.TYPE));
assertFalse("boolean -> byte", ClassUtils.isAssignable(Boolean.TYPE, Byte.TYPE));
assertFalse("boolean -> short", ClassUtils.isAssignable(Boolean.TYPE, Short.TYPE));
assertFalse("boolean -> int", ClassUtils.isAssignable(Boolean.TYPE, Integer.TYPE));
assertFalse("boolean -> long", ClassUtils.isAssignable(Boolean.TYPE, Long.TYPE));
assertFalse("boolean -> float", ClassUtils.isAssignable(Boolean.TYPE, Float.TYPE));
assertFalse("boolean -> double", ClassUtils.isAssignable(Boolean.TYPE, Double.TYPE));
assertTrue("boolean -> boolean", ClassUtils.isAssignable(Boolean.TYPE, Boolean.TYPE));
}
public void test_isAssignable_Unboxing_Widening() throws Exception {
// test byte conversions
assertFalse("byte -> char", ClassUtils.isAssignable(Byte.class, Character.TYPE, true));
assertTrue("byte -> byte", ClassUtils.isAssignable(Byte.class, Byte.TYPE, true));
assertTrue("byte -> short", ClassUtils.isAssignable(Byte.class, Short.TYPE, true));
assertTrue("byte -> int", ClassUtils.isAssignable(Byte.class, Integer.TYPE, true));
assertTrue("byte -> long", ClassUtils.isAssignable(Byte.class, Long.TYPE, true));
assertTrue("byte -> float", ClassUtils.isAssignable(Byte.class, Float.TYPE, true));
assertTrue("byte -> double", ClassUtils.isAssignable(Byte.class, Double.TYPE, true));
assertFalse("byte -> boolean", ClassUtils.isAssignable(Byte.class, Boolean.TYPE, true));
// test short conversions
assertFalse("short -> char", ClassUtils.isAssignable(Short.class, Character.TYPE, true));
assertFalse("short -> byte", ClassUtils.isAssignable(Short.class, Byte.TYPE, true));
assertTrue("short -> short", ClassUtils.isAssignable(Short.class, Short.TYPE, true));
assertTrue("short -> int", ClassUtils.isAssignable(Short.class, Integer.TYPE, true));
assertTrue("short -> long", ClassUtils.isAssignable(Short.class, Long.TYPE, true));
assertTrue("short -> float", ClassUtils.isAssignable(Short.class, Float.TYPE, true));
assertTrue("short -> double", ClassUtils.isAssignable(Short.class, Double.TYPE, true));
assertFalse("short -> boolean", ClassUtils.isAssignable(Short.class, Boolean.TYPE, true));
// test char conversions
assertTrue("char -> char", ClassUtils.isAssignable(Character.class, Character.TYPE, true));
assertFalse("char -> byte", ClassUtils.isAssignable(Character.class, Byte.TYPE, true));
assertFalse("char -> short", ClassUtils.isAssignable(Character.class, Short.TYPE, true));
assertTrue("char -> int", ClassUtils.isAssignable(Character.class, Integer.TYPE, true));
assertTrue("char -> long", ClassUtils.isAssignable(Character.class, Long.TYPE, true));
assertTrue("char -> float", ClassUtils.isAssignable(Character.class, Float.TYPE, true));
assertTrue("char -> double", ClassUtils.isAssignable(Character.class, Double.TYPE, true));
assertFalse("char -> boolean", ClassUtils.isAssignable(Character.class, Boolean.TYPE, true));
// test int conversions
assertFalse("int -> char", ClassUtils.isAssignable(Integer.class, Character.TYPE, true));
assertFalse("int -> byte", ClassUtils.isAssignable(Integer.class, Byte.TYPE, true));
assertFalse("int -> short", ClassUtils.isAssignable(Integer.class, Short.TYPE, true));
assertTrue("int -> int", ClassUtils.isAssignable(Integer.class, Integer.TYPE, true));
assertTrue("int -> long", ClassUtils.isAssignable(Integer.class, Long.TYPE, true));
assertTrue("int -> float", ClassUtils.isAssignable(Integer.class, Float.TYPE, true));
assertTrue("int -> double", ClassUtils.isAssignable(Integer.class, Double.TYPE, true));
assertFalse("int -> boolean", ClassUtils.isAssignable(Integer.class, Boolean.TYPE, true));
// test long conversions
assertFalse("long -> char", ClassUtils.isAssignable(Long.class, Character.TYPE, true));
assertFalse("long -> byte", ClassUtils.isAssignable(Long.class, Byte.TYPE, true));
assertFalse("long -> short", ClassUtils.isAssignable(Long.class, Short.TYPE, true));
assertFalse("long -> int", ClassUtils.isAssignable(Long.class, Integer.TYPE, true));
assertTrue("long -> long", ClassUtils.isAssignable(Long.class, Long.TYPE, true));
assertTrue("long -> float", ClassUtils.isAssignable(Long.class, Float.TYPE, true));
assertTrue("long -> double", ClassUtils.isAssignable(Long.class, Double.TYPE, true));
assertFalse("long -> boolean", ClassUtils.isAssignable(Long.class, Boolean.TYPE, true));
// test float conversions // test float conversions
assertEquals("double -> char", false, ClassUtils.isAssignable(Double.TYPE, Character.TYPE)); assertFalse("float -> char", ClassUtils.isAssignable(Float.class, Character.TYPE, true));
assertEquals("double -> byte", false, ClassUtils.isAssignable(Double.TYPE, Byte.TYPE)); assertFalse("float -> byte", ClassUtils.isAssignable(Float.class, Byte.TYPE, true));
assertEquals("double -> short", false, ClassUtils.isAssignable(Double.TYPE, Short.TYPE)); assertFalse("float -> short", ClassUtils.isAssignable(Float.class, Short.TYPE, true));
assertEquals("double -> int", false, ClassUtils.isAssignable(Double.TYPE, Integer.TYPE)); assertFalse("float -> int", ClassUtils.isAssignable(Float.class, Integer.TYPE, true));
assertEquals("double -> long", false, ClassUtils.isAssignable(Double.TYPE, Long.TYPE)); assertFalse("float -> long", ClassUtils.isAssignable(Float.class, Long.TYPE, true));
assertEquals("double -> float", false, ClassUtils.isAssignable(Double.TYPE, Float.TYPE)); assertTrue("float -> float", ClassUtils.isAssignable(Float.class, Float.TYPE, true));
assertEquals("double -> double", true, ClassUtils.isAssignable(Double.TYPE, Double.TYPE)); assertTrue("float -> double", ClassUtils.isAssignable(Float.class, Double.TYPE, true));
assertEquals("double -> boolean", false, ClassUtils.isAssignable(Double.TYPE, Boolean.TYPE)); assertFalse("float -> boolean", ClassUtils.isAssignable(Float.class, Boolean.TYPE, true));
// test float conversions // test double conversions
assertEquals("boolean -> char", false, ClassUtils.isAssignable(Boolean.TYPE, Character.TYPE)); assertFalse("double -> char", ClassUtils.isAssignable(Double.class, Character.TYPE, true));
assertEquals("boolean -> byte", false, ClassUtils.isAssignable(Boolean.TYPE, Byte.TYPE)); assertFalse("double -> byte", ClassUtils.isAssignable(Double.class, Byte.TYPE, true));
assertEquals("boolean -> short", false, ClassUtils.isAssignable(Boolean.TYPE, Short.TYPE)); assertFalse("double -> short", ClassUtils.isAssignable(Double.class, Short.TYPE, true));
assertEquals("boolean -> int", false, ClassUtils.isAssignable(Boolean.TYPE, Integer.TYPE)); assertFalse("double -> int", ClassUtils.isAssignable(Double.class, Integer.TYPE, true));
assertEquals("boolean -> long", false, ClassUtils.isAssignable(Boolean.TYPE, Long.TYPE)); assertFalse("double -> long", ClassUtils.isAssignable(Double.class, Long.TYPE, true));
assertEquals("boolean -> float", false, ClassUtils.isAssignable(Boolean.TYPE, Float.TYPE)); assertFalse("double -> float", ClassUtils.isAssignable(Double.class, Float.TYPE, true));
assertEquals("boolean -> double", false, ClassUtils.isAssignable(Boolean.TYPE, Double.TYPE)); assertTrue("double -> double", ClassUtils.isAssignable(Double.class, Double.TYPE, true));
assertEquals("boolean -> boolean", true, ClassUtils.isAssignable(Boolean.TYPE, Boolean.TYPE)); assertFalse("double -> boolean", ClassUtils.isAssignable(Double.class, Boolean.TYPE, true));
// test boolean conversions
assertFalse("boolean -> char", ClassUtils.isAssignable(Boolean.class, Character.TYPE, true));
assertFalse("boolean -> byte", ClassUtils.isAssignable(Boolean.class, Byte.TYPE, true));
assertFalse("boolean -> short", ClassUtils.isAssignable(Boolean.class, Short.TYPE, true));
assertFalse("boolean -> int", ClassUtils.isAssignable(Boolean.class, Integer.TYPE, true));
assertFalse("boolean -> long", ClassUtils.isAssignable(Boolean.class, Long.TYPE, true));
assertFalse("boolean -> float", ClassUtils.isAssignable(Boolean.class, Float.TYPE, true));
assertFalse("boolean -> double", ClassUtils.isAssignable(Boolean.class, Double.TYPE, true));
assertTrue("boolean -> boolean", ClassUtils.isAssignable(Boolean.class, Boolean.TYPE, true));
} }
public void testPrimitiveToWrapper() { public void testPrimitiveToWrapper() {