From 8c7546be279b0d82696d5a4954aeaf3820b18ac5 Mon Sep 17 00:00:00 2001 From: Matthew Jason Benson Date: Tue, 6 May 2008 20:26:08 +0000 Subject: [PATCH] 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 --- .../org/apache/commons/lang/ClassUtils.java | 84 +++++- .../apache/commons/lang/ClassUtilsTest.java | 278 ++++++++++++------ 2 files changed, 275 insertions(+), 87 deletions(-) diff --git a/src/java/org/apache/commons/lang/ClassUtils.java b/src/java/org/apache/commons/lang/ClassUtils.java index a690c1542..bb58914e9 100644 --- a/src/java/org/apache/commons/lang/ClassUtils.java +++ b/src/java/org/apache/commons/lang/ClassUtils.java @@ -398,6 +398,42 @@ public class ClassUtils { * @return true if assignment possible */ public static boolean isAssignable(Class[] classArray, Class[] toClassArray) { + return isAssignable(classArray, toClassArray, false); + } + + /** + *

Checks if an array of Classes can be assigned to another array of Classes.

+ * + *

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).

+ * + *

Unlike the {@link Class#isAssignableFrom(java.lang.Class)} method, this + * method takes into account widenings of primitive classes and + * nulls.

+ * + *

Primitive widenings allow an int to be assigned to a long, + * float or double. This method returns the correct + * result for these cases.

+ * + *

Null may be assigned to any reference type. This method will + * return true if null is passed in and the toClass is + * non-primitive.

+ * + *

Specifically, this method tests whether the type represented by the + * specified Class parameter can be converted to the type + * represented by this Class object via an identity conversion + * widening primitive or widening reference conversion. See + * The Java Language Specification, + * sections 5.1.1, 5.1.2 and 5.1.4 for details.

+ * + * @param classArray the array of Classes to check, may be null + * @param toClassArray the array of Classes to try to assign into, may be null + * @param autoboxing whether to use implicit autoboxing/unboxing between primitives and wrappers + * @return true if assignment possible + */ + public static boolean isAssignable(Class[] classArray, Class[] toClassArray, boolean autoboxing) { if (ArrayUtils.isSameLength(classArray, toClassArray) == false) { return false; } @@ -408,7 +444,7 @@ public class ClassUtils { toClassArray = ArrayUtils.EMPTY_CLASS_ARRAY; } 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; } } @@ -442,6 +478,37 @@ public class ClassUtils { * @return true if assignment possible */ public static boolean isAssignable(Class cls, Class toClass) { + return isAssignable(cls, toClass, false); + } + + /** + *

Checks if one Class can be assigned to a variable of + * another Class.

+ * + *

Unlike the {@link Class#isAssignableFrom(java.lang.Class)} method, + * this method takes into account widenings of primitive classes and + * nulls.

+ * + *

Primitive widenings allow an int to be assigned to a long, float or + * double. This method returns the correct result for these cases.

+ * + *

Null may be assigned to any reference type. This method + * will return true if null is passed in and the + * toClass is non-primitive.

+ * + *

Specifically, this method tests whether the type represented by the + * specified Class parameter can be converted to the type + * represented by this Class object via an identity conversion + * widening primitive or widening reference conversion. See + * The Java Language Specification, + * sections 5.1.1, 5.1.2 and 5.1.4 for details.

+ * + * @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 true if assignment possible + */ + public static boolean isAssignable(Class cls, Class toClass, boolean autoboxing) { if (toClass == null) { return false; } @@ -449,6 +516,21 @@ public class ClassUtils { if (cls == null) { 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)) { return true; } diff --git a/src/test/org/apache/commons/lang/ClassUtilsTest.java b/src/test/org/apache/commons/lang/ClassUtilsTest.java index b8e1fe4e2..6592395a3 100644 --- a/src/test/org/apache/commons/lang/ClassUtilsTest.java +++ b/src/test/org/apache/commons/lang/ClassUtilsTest.java @@ -222,114 +222,220 @@ public class ClassUtilsTest extends TestCase { Class[] array1s = new Class[] {String.class}; Class[] array0 = new Class[] {}; - assertEquals(false, ClassUtils.isAssignable(array1, array2)); - assertEquals(false, ClassUtils.isAssignable(null, array2)); - assertEquals(true, ClassUtils.isAssignable(null, array0)); - assertEquals(true, ClassUtils.isAssignable(array0, array0)); - assertEquals(true, ClassUtils.isAssignable(array0, null)); - assertEquals(true, ClassUtils.isAssignable((Class[]) null, (Class[]) null)); + assertFalse(ClassUtils.isAssignable(array1, array2)); + assertFalse(ClassUtils.isAssignable(null, array2)); + assertTrue(ClassUtils.isAssignable(null, array0)); + assertTrue(ClassUtils.isAssignable(array0, array0)); + assertTrue(ClassUtils.isAssignable(array0, null)); + assertTrue(ClassUtils.isAssignable((Class[]) null, (Class[]) null)); - assertEquals(false, ClassUtils.isAssignable(array1, array1s)); - assertEquals(true, ClassUtils.isAssignable(array1s, array1s)); - assertEquals(true, ClassUtils.isAssignable(array1s, array1)); + assertFalse(ClassUtils.isAssignable(array1, array1s)); + assertTrue(ClassUtils.isAssignable(array1s, array1s)); + assertTrue(ClassUtils.isAssignable(array1s, array1)); } public void test_isAssignable() throws Exception { - assertEquals(false, ClassUtils.isAssignable((Class) null, null)); - assertEquals(false, ClassUtils.isAssignable(String.class, null)); + assertFalse(ClassUtils.isAssignable((Class) null, null)); + assertFalse(ClassUtils.isAssignable(String.class, null)); - assertEquals(true, ClassUtils.isAssignable(null, Object.class)); - assertEquals(true, ClassUtils.isAssignable(null, Integer.class)); - assertEquals(false, ClassUtils.isAssignable(null, Integer.TYPE)); - assertEquals(true, ClassUtils.isAssignable(String.class, Object.class)); - assertEquals(true, ClassUtils.isAssignable(String.class, String.class)); - assertEquals(false, ClassUtils.isAssignable(Object.class, String.class)); - assertEquals(false, ClassUtils.isAssignable(Integer.TYPE, Integer.class)); - assertEquals(false, ClassUtils.isAssignable(Integer.class, Integer.TYPE)); - assertEquals(true, ClassUtils.isAssignable(Integer.TYPE, Integer.TYPE)); - assertEquals(true, ClassUtils.isAssignable(Integer.class, Integer.class)); + assertTrue(ClassUtils.isAssignable(null, Object.class)); + assertTrue(ClassUtils.isAssignable(null, Integer.class)); + assertFalse(ClassUtils.isAssignable(null, Integer.TYPE)); + assertTrue(ClassUtils.isAssignable(String.class, Object.class)); + assertTrue(ClassUtils.isAssignable(String.class, String.class)); + assertFalse(ClassUtils.isAssignable(Object.class, String.class)); + assertFalse(ClassUtils.isAssignable(Integer.TYPE, Integer.class)); + assertFalse(ClassUtils.isAssignable(Integer.class, Integer.TYPE)); + assertTrue(ClassUtils.isAssignable(Integer.TYPE, Integer.TYPE)); + 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 { // test byte conversions - assertEquals("byte -> char", false, ClassUtils.isAssignable(Byte.TYPE, Character.TYPE)); - assertEquals("byte -> byte", true, ClassUtils.isAssignable(Byte.TYPE, Byte.TYPE)); - assertEquals("byte -> short", true, ClassUtils.isAssignable(Byte.TYPE, Short.TYPE)); - assertEquals("byte -> int", true, ClassUtils.isAssignable(Byte.TYPE, Integer.TYPE)); - assertEquals("byte -> long", true, ClassUtils.isAssignable(Byte.TYPE, Long.TYPE)); - assertEquals("byte -> float", true, ClassUtils.isAssignable(Byte.TYPE, Float.TYPE)); - assertEquals("byte -> double", true, ClassUtils.isAssignable(Byte.TYPE, Double.TYPE)); - assertEquals("byte -> boolean", false, ClassUtils.isAssignable(Byte.TYPE, Boolean.TYPE)); + assertFalse("byte -> char", ClassUtils.isAssignable(Byte.TYPE, Character.TYPE)); + assertTrue("byte -> byte", ClassUtils.isAssignable(Byte.TYPE, Byte.TYPE)); + assertTrue("byte -> short", ClassUtils.isAssignable(Byte.TYPE, Short.TYPE)); + assertTrue("byte -> int", ClassUtils.isAssignable(Byte.TYPE, Integer.TYPE)); + assertTrue("byte -> long", ClassUtils.isAssignable(Byte.TYPE, Long.TYPE)); + assertTrue("byte -> float", ClassUtils.isAssignable(Byte.TYPE, Float.TYPE)); + assertTrue("byte -> double", ClassUtils.isAssignable(Byte.TYPE, Double.TYPE)); + assertFalse("byte -> boolean", ClassUtils.isAssignable(Byte.TYPE, Boolean.TYPE)); // test short conversions - assertEquals("short -> char", false, ClassUtils.isAssignable(Short.TYPE, Character.TYPE)); - assertEquals("short -> byte", false, ClassUtils.isAssignable(Short.TYPE, Byte.TYPE)); - assertEquals("short -> short", true, ClassUtils.isAssignable(Short.TYPE, Short.TYPE)); - assertEquals("short -> int", true, ClassUtils.isAssignable(Short.TYPE, Integer.TYPE)); - assertEquals("short -> long", true, ClassUtils.isAssignable(Short.TYPE, Long.TYPE)); - assertEquals("short -> float", true, ClassUtils.isAssignable(Short.TYPE, Float.TYPE)); - assertEquals("short -> double", true, ClassUtils.isAssignable(Short.TYPE, Double.TYPE)); - assertEquals("short -> boolean", false, ClassUtils.isAssignable(Short.TYPE, Boolean.TYPE)); + assertFalse("short -> char", ClassUtils.isAssignable(Short.TYPE, Character.TYPE)); + assertFalse("short -> byte", ClassUtils.isAssignable(Short.TYPE, Byte.TYPE)); + assertTrue("short -> short", ClassUtils.isAssignable(Short.TYPE, Short.TYPE)); + assertTrue("short -> int", ClassUtils.isAssignable(Short.TYPE, Integer.TYPE)); + assertTrue("short -> long", ClassUtils.isAssignable(Short.TYPE, Long.TYPE)); + assertTrue("short -> float", ClassUtils.isAssignable(Short.TYPE, Float.TYPE)); + assertTrue("short -> double", ClassUtils.isAssignable(Short.TYPE, Double.TYPE)); + assertFalse("short -> boolean", ClassUtils.isAssignable(Short.TYPE, Boolean.TYPE)); // test char conversions - assertEquals("char -> char", true, ClassUtils.isAssignable(Character.TYPE, Character.TYPE)); - assertEquals("char -> byte", false, ClassUtils.isAssignable(Character.TYPE, Byte.TYPE)); - assertEquals("char -> short", false, ClassUtils.isAssignable(Character.TYPE, Short.TYPE)); - assertEquals("char -> int", true, ClassUtils.isAssignable(Character.TYPE, Integer.TYPE)); - assertEquals("char -> long", true, ClassUtils.isAssignable(Character.TYPE, Long.TYPE)); - assertEquals("char -> float", true, ClassUtils.isAssignable(Character.TYPE, Float.TYPE)); - assertEquals("char -> double", true, ClassUtils.isAssignable(Character.TYPE, Double.TYPE)); - assertEquals("char -> boolean", false, ClassUtils.isAssignable(Character.TYPE, Boolean.TYPE)); + assertTrue("char -> char", ClassUtils.isAssignable(Character.TYPE, Character.TYPE)); + assertFalse("char -> byte", ClassUtils.isAssignable(Character.TYPE, Byte.TYPE)); + assertFalse("char -> short", ClassUtils.isAssignable(Character.TYPE, Short.TYPE)); + assertTrue("char -> int", ClassUtils.isAssignable(Character.TYPE, Integer.TYPE)); + assertTrue("char -> long", ClassUtils.isAssignable(Character.TYPE, Long.TYPE)); + assertTrue("char -> float", ClassUtils.isAssignable(Character.TYPE, Float.TYPE)); + assertTrue("char -> double", ClassUtils.isAssignable(Character.TYPE, Double.TYPE)); + assertFalse("char -> boolean", ClassUtils.isAssignable(Character.TYPE, Boolean.TYPE)); // test int conversions - assertEquals("int -> char", false, ClassUtils.isAssignable(Integer.TYPE, Character.TYPE)); - assertEquals("int -> byte", false, ClassUtils.isAssignable(Integer.TYPE, Byte.TYPE)); - assertEquals("int -> short", false, ClassUtils.isAssignable(Integer.TYPE, Short.TYPE)); - assertEquals("int -> int", true, ClassUtils.isAssignable(Integer.TYPE, Integer.TYPE)); - assertEquals("int -> long", true, ClassUtils.isAssignable(Integer.TYPE, Long.TYPE)); - assertEquals("int -> float", true, ClassUtils.isAssignable(Integer.TYPE, Float.TYPE)); - assertEquals("int -> double", true, ClassUtils.isAssignable(Integer.TYPE, Double.TYPE)); - assertEquals("int -> boolean", false, ClassUtils.isAssignable(Integer.TYPE, Boolean.TYPE)); + assertFalse("int -> char", ClassUtils.isAssignable(Integer.TYPE, Character.TYPE)); + assertFalse("int -> byte", ClassUtils.isAssignable(Integer.TYPE, Byte.TYPE)); + assertFalse("int -> short", ClassUtils.isAssignable(Integer.TYPE, Short.TYPE)); + assertTrue("int -> int", ClassUtils.isAssignable(Integer.TYPE, Integer.TYPE)); + assertTrue("int -> long", ClassUtils.isAssignable(Integer.TYPE, Long.TYPE)); + assertTrue("int -> float", ClassUtils.isAssignable(Integer.TYPE, Float.TYPE)); + assertTrue("int -> double", ClassUtils.isAssignable(Integer.TYPE, Double.TYPE)); + assertFalse("int -> boolean", ClassUtils.isAssignable(Integer.TYPE, Boolean.TYPE)); // test long conversions - assertEquals("long -> char", false, ClassUtils.isAssignable(Long.TYPE, Character.TYPE)); - assertEquals("long -> byte", false, ClassUtils.isAssignable(Long.TYPE, Byte.TYPE)); - assertEquals("long -> short", false, ClassUtils.isAssignable(Long.TYPE, Short.TYPE)); - assertEquals("long -> int", false, ClassUtils.isAssignable(Long.TYPE, Integer.TYPE)); - assertEquals("long -> long", true, ClassUtils.isAssignable(Long.TYPE, Long.TYPE)); - assertEquals("long -> float", true, ClassUtils.isAssignable(Long.TYPE, Float.TYPE)); - assertEquals("long -> double", true, ClassUtils.isAssignable(Long.TYPE, Double.TYPE)); - assertEquals("long -> boolean", false, ClassUtils.isAssignable(Long.TYPE, Boolean.TYPE)); + assertFalse("long -> char", ClassUtils.isAssignable(Long.TYPE, Character.TYPE)); + assertFalse("long -> byte", ClassUtils.isAssignable(Long.TYPE, Byte.TYPE)); + assertFalse("long -> short", ClassUtils.isAssignable(Long.TYPE, Short.TYPE)); + assertFalse("long -> int", ClassUtils.isAssignable(Long.TYPE, Integer.TYPE)); + assertTrue("long -> long", ClassUtils.isAssignable(Long.TYPE, Long.TYPE)); + assertTrue("long -> float", ClassUtils.isAssignable(Long.TYPE, Float.TYPE)); + assertTrue("long -> double", ClassUtils.isAssignable(Long.TYPE, Double.TYPE)); + assertFalse("long -> boolean", ClassUtils.isAssignable(Long.TYPE, Boolean.TYPE)); // test float conversions - assertEquals("float -> char", false, ClassUtils.isAssignable(Float.TYPE, Character.TYPE)); - assertEquals("float -> byte", false, ClassUtils.isAssignable(Float.TYPE, Byte.TYPE)); - assertEquals("float -> short", false, ClassUtils.isAssignable(Float.TYPE, Short.TYPE)); - assertEquals("float -> int", false, ClassUtils.isAssignable(Float.TYPE, Integer.TYPE)); - assertEquals("float -> long", false, ClassUtils.isAssignable(Float.TYPE, Long.TYPE)); - assertEquals("float -> float", true, ClassUtils.isAssignable(Float.TYPE, Float.TYPE)); - assertEquals("float -> double", true, ClassUtils.isAssignable(Float.TYPE, Double.TYPE)); - assertEquals("float -> boolean", false, ClassUtils.isAssignable(Float.TYPE, Boolean.TYPE)); + assertFalse("float -> char", ClassUtils.isAssignable(Float.TYPE, Character.TYPE)); + assertFalse("float -> byte", ClassUtils.isAssignable(Float.TYPE, Byte.TYPE)); + assertFalse("float -> short", ClassUtils.isAssignable(Float.TYPE, Short.TYPE)); + assertFalse("float -> int", ClassUtils.isAssignable(Float.TYPE, Integer.TYPE)); + assertFalse("float -> long", ClassUtils.isAssignable(Float.TYPE, Long.TYPE)); + assertTrue("float -> float", ClassUtils.isAssignable(Float.TYPE, Float.TYPE)); + assertTrue("float -> double", ClassUtils.isAssignable(Float.TYPE, Double.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 - assertEquals("double -> char", false, ClassUtils.isAssignable(Double.TYPE, Character.TYPE)); - assertEquals("double -> byte", false, ClassUtils.isAssignable(Double.TYPE, Byte.TYPE)); - assertEquals("double -> short", false, ClassUtils.isAssignable(Double.TYPE, Short.TYPE)); - assertEquals("double -> int", false, ClassUtils.isAssignable(Double.TYPE, Integer.TYPE)); - assertEquals("double -> long", false, ClassUtils.isAssignable(Double.TYPE, Long.TYPE)); - assertEquals("double -> float", false, ClassUtils.isAssignable(Double.TYPE, Float.TYPE)); - assertEquals("double -> double", true, ClassUtils.isAssignable(Double.TYPE, Double.TYPE)); - assertEquals("double -> boolean", false, ClassUtils.isAssignable(Double.TYPE, Boolean.TYPE)); + assertFalse("float -> char", ClassUtils.isAssignable(Float.class, Character.TYPE, true)); + assertFalse("float -> byte", ClassUtils.isAssignable(Float.class, Byte.TYPE, true)); + assertFalse("float -> short", ClassUtils.isAssignable(Float.class, Short.TYPE, true)); + assertFalse("float -> int", ClassUtils.isAssignable(Float.class, Integer.TYPE, true)); + assertFalse("float -> long", ClassUtils.isAssignable(Float.class, Long.TYPE, true)); + assertTrue("float -> float", ClassUtils.isAssignable(Float.class, Float.TYPE, true)); + assertTrue("float -> double", ClassUtils.isAssignable(Float.class, Double.TYPE, true)); + assertFalse("float -> boolean", ClassUtils.isAssignable(Float.class, Boolean.TYPE, true)); - // test float conversions - assertEquals("boolean -> char", false, ClassUtils.isAssignable(Boolean.TYPE, Character.TYPE)); - assertEquals("boolean -> byte", false, ClassUtils.isAssignable(Boolean.TYPE, Byte.TYPE)); - assertEquals("boolean -> short", false, ClassUtils.isAssignable(Boolean.TYPE, Short.TYPE)); - assertEquals("boolean -> int", false, ClassUtils.isAssignable(Boolean.TYPE, Integer.TYPE)); - assertEquals("boolean -> long", false, ClassUtils.isAssignable(Boolean.TYPE, Long.TYPE)); - assertEquals("boolean -> float", false, ClassUtils.isAssignable(Boolean.TYPE, Float.TYPE)); - assertEquals("boolean -> double", false, ClassUtils.isAssignable(Boolean.TYPE, Double.TYPE)); - assertEquals("boolean -> boolean", true, ClassUtils.isAssignable(Boolean.TYPE, Boolean.TYPE)); + // test double conversions + assertFalse("double -> char", ClassUtils.isAssignable(Double.class, Character.TYPE, true)); + assertFalse("double -> byte", ClassUtils.isAssignable(Double.class, Byte.TYPE, true)); + assertFalse("double -> short", ClassUtils.isAssignable(Double.class, Short.TYPE, true)); + assertFalse("double -> int", ClassUtils.isAssignable(Double.class, Integer.TYPE, true)); + assertFalse("double -> long", ClassUtils.isAssignable(Double.class, Long.TYPE, true)); + assertFalse("double -> float", ClassUtils.isAssignable(Double.class, Float.TYPE, true)); + assertTrue("double -> double", ClassUtils.isAssignable(Double.class, Double.TYPE, true)); + 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() {