diff --git a/src/java/org/apache/commons/lang/ClassUtils.java b/src/java/org/apache/commons/lang/ClassUtils.java index 160a750bd..43503b5f1 100644 --- a/src/java/org/apache/commons/lang/ClassUtils.java +++ b/src/java/org/apache/commons/lang/ClassUtils.java @@ -32,7 +32,7 @@ import java.util.Map; * @author Gary Gregory * @author Norm Deane * @since 2.0 - * @version $Id: ClassUtils.java,v 1.28 2004/02/24 06:01:28 ggregory Exp $ + * @version $Id: ClassUtils.java,v 1.29 2004/06/27 04:42:54 bayard Exp $ */ public class ClassUtils { @@ -511,6 +511,31 @@ public class ClassUtils { } return convertedClass; } + + /** + *

Converts the specified array of primitive Class objects to an array of + * its corresponding wrapper Class objects.

+ * + * @param classes the class array to convert, may be null or empty + * @return an array which contains for each given class, the wrapper class or + * the original class if class is not a primitive. null if null input. + * Empty array if an empty array passed in. + */ + public static Class[] primitivesToWrappers(Class[] classes) { + if (classes == null) { + return null; + } + + if (classes.length == 0) { + return ArrayUtils.EMPTY_CLASS_ARRAY; + } + + Class[] convertedClasses = new Class[classes.length]; + for (int i=0; i < classes.length; i++) { + convertedClasses[i] = primitiveToWrapper( classes[i] ); + } + return convertedClasses; + } // Inner class // ---------------------------------------------------------------------- diff --git a/src/test/org/apache/commons/lang/ClassUtilsTest.java b/src/test/org/apache/commons/lang/ClassUtilsTest.java index 290b24359..80500a686 100644 --- a/src/test/org/apache/commons/lang/ClassUtilsTest.java +++ b/src/test/org/apache/commons/lang/ClassUtilsTest.java @@ -33,7 +33,7 @@ import junit.textui.TestRunner; * * @author Stephen Colebourne * @author Gary D. Gregory - * @version $Id: ClassUtilsTest.java,v 1.11 2004/02/18 23:22:29 ggregory Exp $ + * @version $Id: ClassUtilsTest.java,v 1.12 2004/06/27 04:42:54 bayard Exp $ */ public class ClassUtilsTest extends TestCase { @@ -375,6 +375,38 @@ public class ClassUtilsTest extends TestCase { assertNull("null -> null", ClassUtils.primitiveToWrapper(null)); } + + public void testPrimitivesToWrappers() { + // test null + assertNull("null -> null", + ClassUtils.primitivesToWrappers(null)); + // test empty array + assertEquals("empty -> empty", + ArrayUtils.EMPTY_CLASS_ARRAY, ClassUtils.primitivesToWrappers(ArrayUtils.EMPTY_CLASS_ARRAY)); + + // test an array of various classes + final Class[] primitives = new Class[] { + Boolean.TYPE, Byte.TYPE, Character.TYPE, Short.TYPE, + Integer.TYPE, Long.TYPE, Double.TYPE, Float.TYPE, + String.class, ClassUtils.class + }; + Class[] wrappers= ClassUtils.primitivesToWrappers(primitives); + + for (int i=0; i < primitives.length; i++) { + // test each returned wrapper + Class primitive = primitives[i]; + Class expectedWrapper = ClassUtils.primitiveToWrapper(primitive); + + assertEquals(primitive + " -> " + expectedWrapper, expectedWrapper, wrappers[i]); + } + + // test an array of no primitive classes + final Class[] noPrimitives = new Class[] { + String.class, ClassUtils.class + }; + // This used to return the exact same array, but no longer does. + assertNotSame("unmodified", noPrimitives, ClassUtils.primitivesToWrappers(noPrimitives)); + } public void testClassComparator() { Comparator comparator = ClassUtils.CLASS_NAME_COMPARATOR;