commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mben...@apache.org
Subject svn commit: r905766 - in /commons/proper/lang/trunk/src: main/java/org/apache/commons/lang3/ClassUtils.java test/java/org/apache/commons/lang3/ClassUtilsTest.java
Date Tue, 02 Feb 2010 20:07:28 GMT
Author: mbenson
Date: Tue Feb  2 20:07:27 2010
New Revision: 905766

URL: http://svn.apache.org/viewvc?rev=905766&view=rev
Log:
default autoboxing for Class[] depending on RT Java version + more tests

Modified:
    commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/ClassUtils.java
    commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/ClassUtilsTest.java

Modified: commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/ClassUtils.java
URL: http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/ClassUtils.java?rev=905766&r1=905765&r2=905766&view=diff
==============================================================================
--- commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/ClassUtils.java (original)
+++ commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/ClassUtils.java Tue Feb
 2 20:07:27 2010
@@ -426,13 +426,17 @@
      * <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>
      *
+     * <p><strong>Since Lang 3.0,</strong> this method will default behavior
for
+     * calculating assignability between primitive and wrapper types <em>corresponding
+     * to the running Java version</em>; i.e. autoboxing will be the default
+     * behavior in VMs running Java versions >= 1.5.</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>
      * @return <code>true</code> if assignment possible
      */
-    //TODO when we bump the major version we should default autoboxing to true on platforms
>= 1.5
     public static boolean isAssignable(Class<?>[] classArray, Class<?>[] toClassArray)
{
-        return isAssignable(classArray, toClassArray, false);
+        return isAssignable(classArray, toClassArray, SystemUtils.isJavaVersionAtLeast(1.5f));
     }
 
     /**

Modified: commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/ClassUtilsTest.java
URL: http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/ClassUtilsTest.java?rev=905766&r1=905765&r2=905766&view=diff
==============================================================================
--- commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/ClassUtilsTest.java (original)
+++ commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/ClassUtilsTest.java Tue
Feb  2 20:07:27 2010
@@ -28,8 +28,6 @@
 
 import junit.framework.TestCase;
 
-
-
 /**
  * Unit tests {@link org.apache.commons.lang3.ClassUtils}.
  *
@@ -251,6 +249,8 @@
         Class<?>[] array1 = new Class[] {Object.class};
         Class<?>[] array1s = new Class[] {String.class};
         Class<?>[] array0 = new Class[] {};
+        Class<?>[] arrayPrimitives = { Integer.TYPE, Boolean.TYPE };
+        Class<?>[] arrayWrappers = { Integer.class, Boolean.class };
 
         assertFalse(ClassUtils.isAssignable(array1, array2));
         assertFalse(ClassUtils.isAssignable(null, array2));
@@ -262,6 +262,69 @@
         assertFalse(ClassUtils.isAssignable(array1, array1s));
         assertTrue(ClassUtils.isAssignable(array1s, array1s));
         assertTrue(ClassUtils.isAssignable(array1s, array1));
+
+        boolean autoboxing = SystemUtils.isJavaVersionAtLeast(1.5f);
+
+        assertEquals(autoboxing, ClassUtils.isAssignable(arrayPrimitives, arrayWrappers));
+        assertEquals(autoboxing, ClassUtils.isAssignable(arrayWrappers, arrayPrimitives));
+        assertFalse(ClassUtils.isAssignable(arrayPrimitives, array1));
+        assertFalse(ClassUtils.isAssignable(arrayWrappers, array1));
+        assertEquals(autoboxing, ClassUtils.isAssignable(arrayPrimitives, array2));
+        assertTrue(ClassUtils.isAssignable(arrayWrappers, array2));
+    }
+
+    public void test_isAssignable_ClassArray_ClassArray_Autoboxing() throws Exception {
+        Class<?>[] array2 = new Class[] {Object.class, Object.class};
+        Class<?>[] array1 = new Class[] {Object.class};
+        Class<?>[] array1s = new Class[] {String.class};
+        Class<?>[] array0 = new Class[] {};
+        Class<?>[] arrayPrimitives = { Integer.TYPE, Boolean.TYPE };
+        Class<?>[] arrayWrappers = { Integer.class, Boolean.class };
+
+        assertFalse(ClassUtils.isAssignable(array1, array2, true));
+        assertFalse(ClassUtils.isAssignable(null, array2, true));
+        assertTrue(ClassUtils.isAssignable(null, array0, true));
+        assertTrue(ClassUtils.isAssignable(array0, array0, true));
+        assertTrue(ClassUtils.isAssignable(array0, null, true));
+        assertTrue(ClassUtils.isAssignable((Class[]) null, (Class[]) null, true));
+
+        assertFalse(ClassUtils.isAssignable(array1, array1s, true));
+        assertTrue(ClassUtils.isAssignable(array1s, array1s, true));
+        assertTrue(ClassUtils.isAssignable(array1s, array1, true));
+
+        assertTrue(ClassUtils.isAssignable(arrayPrimitives, arrayWrappers, true));
+        assertTrue(ClassUtils.isAssignable(arrayWrappers, arrayPrimitives, true));
+        assertFalse(ClassUtils.isAssignable(arrayPrimitives, array1, true));
+        assertFalse(ClassUtils.isAssignable(arrayWrappers, array1, true));
+        assertTrue(ClassUtils.isAssignable(arrayPrimitives, array2, true));
+        assertTrue(ClassUtils.isAssignable(arrayWrappers, array2, true));
+    }
+
+    public void test_isAssignable_ClassArray_ClassArray_NoAutoboxing() throws Exception {
+        Class<?>[] array2 = new Class[] {Object.class, Object.class};
+        Class<?>[] array1 = new Class[] {Object.class};
+        Class<?>[] array1s = new Class[] {String.class};
+        Class<?>[] array0 = new Class[] {};
+        Class<?>[] arrayPrimitives = { Integer.TYPE, Boolean.TYPE };
+        Class<?>[] arrayWrappers = { Integer.class, Boolean.class };
+
+        assertFalse(ClassUtils.isAssignable(array1, array2, false));
+        assertFalse(ClassUtils.isAssignable(null, array2, false));
+        assertTrue(ClassUtils.isAssignable(null, array0, false));
+        assertTrue(ClassUtils.isAssignable(array0, array0, false));
+        assertTrue(ClassUtils.isAssignable(array0, null, false));
+        assertTrue(ClassUtils.isAssignable((Class[]) null, (Class[]) null, false));
+
+        assertFalse(ClassUtils.isAssignable(array1, array1s, false));
+        assertTrue(ClassUtils.isAssignable(array1s, array1s, false));
+        assertTrue(ClassUtils.isAssignable(array1s, array1, false));
+
+        assertFalse(ClassUtils.isAssignable(arrayPrimitives, arrayWrappers, false));
+        assertFalse(ClassUtils.isAssignable(arrayWrappers, arrayPrimitives, false));
+        assertFalse(ClassUtils.isAssignable(arrayPrimitives, array1, false));
+        assertFalse(ClassUtils.isAssignable(arrayWrappers, array1, false));
+        assertTrue(ClassUtils.isAssignable(arrayWrappers, array2, false));
+        assertFalse(ClassUtils.isAssignable(arrayPrimitives, array2, false));
     }
 
     public void test_isAssignable() throws Exception {
@@ -278,11 +341,15 @@
         boolean autoboxing = SystemUtils.isJavaVersionAtLeast(1.5f);
 
         assertEquals(autoboxing, ClassUtils.isAssignable(Integer.TYPE, Integer.class));
+        assertEquals(autoboxing, ClassUtils.isAssignable(Integer.TYPE, Object.class));
         assertEquals(autoboxing, ClassUtils.isAssignable(Integer.class, Integer.TYPE));
+        assertEquals(autoboxing, ClassUtils.isAssignable(Integer.class, Object.class));
         assertTrue(ClassUtils.isAssignable(Integer.TYPE, Integer.TYPE));
         assertTrue(ClassUtils.isAssignable(Integer.class, Integer.class));
         assertEquals(autoboxing, ClassUtils.isAssignable(Boolean.TYPE, Boolean.class));
+        assertEquals(autoboxing, ClassUtils.isAssignable(Boolean.TYPE, Object.class));
         assertEquals(autoboxing, ClassUtils.isAssignable(Boolean.class, Boolean.TYPE));
+        assertEquals(autoboxing, ClassUtils.isAssignable(Boolean.class, Object.class));
         assertTrue(ClassUtils.isAssignable(Boolean.TYPE, Boolean.TYPE));
         assertTrue(ClassUtils.isAssignable(Boolean.class, Boolean.class));
     }
@@ -298,15 +365,41 @@
         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.TYPE, Object.class, true));
         assertTrue(ClassUtils.isAssignable(Integer.class, Integer.TYPE, true));
+        assertTrue(ClassUtils.isAssignable(Integer.class, Object.class, 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.class, Object.class, true));
         assertTrue(ClassUtils.isAssignable(Boolean.TYPE, Boolean.TYPE, true));
         assertTrue(ClassUtils.isAssignable(Boolean.class, Boolean.class, true));
     }
 
+    public void test_isAssignable_NoAutoboxing() throws Exception {
+        assertFalse(ClassUtils.isAssignable((Class<?>) null, null, false));
+        assertFalse(ClassUtils.isAssignable(String.class, null, false));
+
+        assertTrue(ClassUtils.isAssignable(null, Object.class, false));
+        assertTrue(ClassUtils.isAssignable(null, Integer.class, false));
+        assertFalse(ClassUtils.isAssignable(null, Integer.TYPE, false));
+        assertTrue(ClassUtils.isAssignable(String.class, Object.class, false));
+        assertTrue(ClassUtils.isAssignable(String.class, String.class, false));
+        assertFalse(ClassUtils.isAssignable(Object.class, String.class, false));
+        assertFalse(ClassUtils.isAssignable(Integer.TYPE, Integer.class, false));
+        assertFalse(ClassUtils.isAssignable(Integer.TYPE, Object.class, false));
+        assertFalse(ClassUtils.isAssignable(Integer.class, Integer.TYPE, false));
+        assertTrue(ClassUtils.isAssignable(Integer.TYPE, Integer.TYPE, false));
+        assertTrue(ClassUtils.isAssignable(Integer.class, Integer.class, false));
+        assertFalse(ClassUtils.isAssignable(Boolean.TYPE, Boolean.class, false));
+        assertFalse(ClassUtils.isAssignable(Boolean.TYPE, Object.class, false));
+        assertFalse(ClassUtils.isAssignable(Boolean.class, Boolean.TYPE, false));
+        assertTrue(ClassUtils.isAssignable(Boolean.class, Object.class, false));
+        assertTrue(ClassUtils.isAssignable(Boolean.TYPE, Boolean.TYPE, false));
+        assertTrue(ClassUtils.isAssignable(Boolean.class, Boolean.class, false));
+    }
+
     public void test_isAssignable_Widening() throws Exception {
         // test byte conversions
         assertFalse("byte -> char", ClassUtils.isAssignable(Byte.TYPE, Character.TYPE));
@@ -389,6 +482,90 @@
         assertTrue("boolean -> boolean", ClassUtils.isAssignable(Boolean.TYPE, Boolean.TYPE));
     }
 
+    public void test_isAssignable_DefaultUnboxing_Widening() throws Exception {
+        boolean autoboxing = SystemUtils.isJavaVersionAtLeast(1.5f);
+
+        // test byte conversions
+        assertFalse("byte -> char", ClassUtils.isAssignable(Byte.class, Character.TYPE));
+        assertEquals("byte -> byte", autoboxing, ClassUtils.isAssignable(Byte.class, Byte.TYPE));
+        assertEquals("byte -> short", autoboxing, ClassUtils.isAssignable(Byte.class,
Short.TYPE));
+        assertEquals("byte -> int", autoboxing, ClassUtils.isAssignable(Byte.class, Integer.TYPE));
+        assertEquals("byte -> long", autoboxing, ClassUtils.isAssignable(Byte.class, Long.TYPE));
+        assertEquals("byte -> float", autoboxing, ClassUtils.isAssignable(Byte.class,
Float.TYPE));
+        assertEquals("byte -> double", autoboxing, ClassUtils.isAssignable(Byte.class,
Double.TYPE));
+        assertFalse("byte -> boolean", ClassUtils.isAssignable(Byte.class, Boolean.TYPE));
+
+        // test short conversions
+        assertFalse("short -> char", ClassUtils.isAssignable(Short.class, Character.TYPE));
+        assertFalse("short -> byte", ClassUtils.isAssignable(Short.class, Byte.TYPE));
+        assertEquals("short -> short", autoboxing, ClassUtils.isAssignable(Short.class,
Short.TYPE));
+        assertEquals("short -> int", autoboxing, ClassUtils.isAssignable(Short.class,
Integer.TYPE));
+        assertEquals("short -> long", autoboxing, ClassUtils.isAssignable(Short.class,
Long.TYPE));
+        assertEquals("short -> float", autoboxing, ClassUtils.isAssignable(Short.class,
Float.TYPE));
+        assertEquals("short -> double", autoboxing, ClassUtils.isAssignable(Short.class,
Double.TYPE));
+        assertFalse("short -> boolean", ClassUtils.isAssignable(Short.class, Boolean.TYPE));
+
+        // test char conversions
+        assertEquals("char -> char", autoboxing, ClassUtils.isAssignable(Character.class,
Character.TYPE));
+        assertFalse("char -> byte", ClassUtils.isAssignable(Character.class, Byte.TYPE));
+        assertFalse("char -> short", ClassUtils.isAssignable(Character.class, Short.TYPE));
+        assertEquals("char -> int", autoboxing, ClassUtils.isAssignable(Character.class,
Integer.TYPE));
+        assertEquals("char -> long", autoboxing, ClassUtils.isAssignable(Character.class,
Long.TYPE));
+        assertEquals("char -> float", autoboxing, ClassUtils.isAssignable(Character.class,
Float.TYPE));
+        assertEquals("char -> double", autoboxing, ClassUtils.isAssignable(Character.class,
Double.TYPE));
+        assertFalse("char -> boolean", ClassUtils.isAssignable(Character.class, Boolean.TYPE));
+
+        // test int conversions
+        assertFalse("int -> char", ClassUtils.isAssignable(Integer.class, Character.TYPE));
+        assertFalse("int -> byte", ClassUtils.isAssignable(Integer.class, Byte.TYPE));
+        assertFalse("int -> short", ClassUtils.isAssignable(Integer.class, Short.TYPE));
+        assertEquals("int -> int", autoboxing, ClassUtils.isAssignable(Integer.class,
Integer.TYPE));
+        assertEquals("int -> long", autoboxing, ClassUtils.isAssignable(Integer.class,
Long.TYPE));
+        assertEquals("int -> float", autoboxing, ClassUtils.isAssignable(Integer.class,
Float.TYPE));
+        assertEquals("int -> double", autoboxing, ClassUtils.isAssignable(Integer.class,
Double.TYPE));
+        assertFalse("int -> boolean", ClassUtils.isAssignable(Integer.class, Boolean.TYPE));
+
+        // test long conversions
+        assertFalse("long -> char", ClassUtils.isAssignable(Long.class, Character.TYPE));
+        assertFalse("long -> byte", ClassUtils.isAssignable(Long.class, Byte.TYPE));
+        assertFalse("long -> short", ClassUtils.isAssignable(Long.class, Short.TYPE));
+        assertFalse("long -> int", ClassUtils.isAssignable(Long.class, Integer.TYPE));
+        assertEquals("long -> long", autoboxing, ClassUtils.isAssignable(Long.class, Long.TYPE));
+        assertEquals("long -> float", autoboxing, ClassUtils.isAssignable(Long.class,
Float.TYPE));
+        assertEquals("long -> double", autoboxing, ClassUtils.isAssignable(Long.class,
Double.TYPE));
+        assertFalse("long -> boolean", ClassUtils.isAssignable(Long.class, Boolean.TYPE));
+
+        // test float conversions
+        assertFalse("float -> char", ClassUtils.isAssignable(Float.class, Character.TYPE));
+        assertFalse("float -> byte", ClassUtils.isAssignable(Float.class, Byte.TYPE));
+        assertFalse("float -> short", ClassUtils.isAssignable(Float.class, Short.TYPE));
+        assertFalse("float -> int", ClassUtils.isAssignable(Float.class, Integer.TYPE));
+        assertFalse("float -> long", ClassUtils.isAssignable(Float.class, Long.TYPE));
+        assertEquals("float -> float", autoboxing, ClassUtils.isAssignable(Float.class,
Float.TYPE));
+        assertEquals("float -> double", autoboxing, ClassUtils.isAssignable(Float.class,
Double.TYPE));
+        assertFalse("float -> boolean", ClassUtils.isAssignable(Float.class, Boolean.TYPE));
+
+        // test double conversions
+        assertFalse("double -> char", ClassUtils.isAssignable(Double.class, Character.TYPE));
+        assertFalse("double -> byte", ClassUtils.isAssignable(Double.class, Byte.TYPE));
+        assertFalse("double -> short", ClassUtils.isAssignable(Double.class, Short.TYPE));
+        assertFalse("double -> int", ClassUtils.isAssignable(Double.class, Integer.TYPE));
+        assertFalse("double -> long", ClassUtils.isAssignable(Double.class, Long.TYPE));
+        assertFalse("double -> float", ClassUtils.isAssignable(Double.class, Float.TYPE));
+        assertEquals("double -> double", autoboxing, ClassUtils.isAssignable(Double.class,
Double.TYPE));
+        assertFalse("double -> boolean", ClassUtils.isAssignable(Double.class, Boolean.TYPE));
+
+        // test boolean conversions
+        assertFalse("boolean -> char", ClassUtils.isAssignable(Boolean.class, Character.TYPE));
+        assertFalse("boolean -> byte", ClassUtils.isAssignable(Boolean.class, Byte.TYPE));
+        assertFalse("boolean -> short", ClassUtils.isAssignable(Boolean.class, Short.TYPE));
+        assertFalse("boolean -> int", ClassUtils.isAssignable(Boolean.class, Integer.TYPE));
+        assertFalse("boolean -> long", ClassUtils.isAssignable(Boolean.class, Long.TYPE));
+        assertFalse("boolean -> float", ClassUtils.isAssignable(Boolean.class, Float.TYPE));
+        assertFalse("boolean -> double", ClassUtils.isAssignable(Boolean.class, Double.TYPE));
+        assertEquals("boolean -> boolean", autoboxing, ClassUtils.isAssignable(Boolean.class,
Boolean.TYPE));
+    }
+
     public void test_isAssignable_Unboxing_Widening() throws Exception {
         // test byte conversions
         assertFalse("byte -> char", ClassUtils.isAssignable(Byte.class, Character.TYPE,
true));



Mime
View raw message