From 220e829b704c7dbdc57b0f5aad4aa73717e59e11 Mon Sep 17 00:00:00 2001 From: Henri Yandell Date: Tue, 13 Nov 2007 02:38:48 +0000 Subject: [PATCH] Applying Dave Meikle's patch to LANG-333; adding a toClass(Object[]);Class[] method to ArrayUtils git-svn-id: https://svn.apache.org/repos/asf/commons/proper/lang/trunk@594396 13f79535-47bb-0310-9956-ffa450edef68 --- .../org/apache/commons/lang/ArrayUtils.java | 24 ++++++++++++++++++- .../apache/commons/lang/ArrayUtilsTest.java | 18 ++++++++++++++ 2 files changed, 41 insertions(+), 1 deletion(-) 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[])}.