diff --git a/src/java/org/apache/commons/lang/ArrayUtils.java b/src/java/org/apache/commons/lang/ArrayUtils.java index e7571f6ea..c9116da8a 100644 --- a/src/java/org/apache/commons/lang/ArrayUtils.java +++ b/src/java/org/apache/commons/lang/ArrayUtils.java @@ -4413,5 +4413,27 @@ public class ArrayUtils { return result; } - + + /** + *

Converts an array of Object in to an Array of Class objects.

+ * + *

This method returns null for a null input array.

+ * + * @param array an Object array + * @return a Class array, null if null array input + */ + public static Class[] toClass(Object[] array) + { + if (array == null) { + return null; + } else if (array.length == 0) { + return EMPTY_CLASS_ARRAY; + } + Class[] classes = new Class[array.length]; + for (int i = 0; i < array.length; i++) { + classes[i] = array[i].getClass(); + } + return classes; + } + } diff --git a/src/test/org/apache/commons/lang/ArrayUtilsTest.java b/src/test/org/apache/commons/lang/ArrayUtilsTest.java index a7a25973b..2073761b7 100644 --- a/src/test/org/apache/commons/lang/ArrayUtilsTest.java +++ b/src/test/org/apache/commons/lang/ArrayUtilsTest.java @@ -2420,6 +2420,24 @@ public class ArrayUtilsTest extends TestCase { new double[] { Double.MIN_VALUE, Double.MAX_VALUE, 9999999 }))); } + public void testToClass_object() { + assertEquals(null, ArrayUtils.toClass(null)); + + assertSame( + ArrayUtils.EMPTY_CLASS_ARRAY, + ArrayUtils.toClass(new Class[0])); + + Object[] array = new Object[3]; + array[0] = new String("Test"); + array[1] = new Integer(1); + array[2] = new Double(99); + + Class[] results = ArrayUtils.toClass(array); + assertEquals("String", results[0].getSimpleName()); + assertEquals("Integer", results[1].getSimpleName()); + assertEquals("Double", results[2].getSimpleName()); + } + //----------------------------------------------------------------------- /** * Test for {@link ArrayUtils#isEmpty(java.lang.Object[])}.