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
This commit is contained in:
Henri Yandell 2007-11-13 02:38:48 +00:00
parent 62a9fa6811
commit 220e829b70
2 changed files with 41 additions and 1 deletions

View File

@ -4414,4 +4414,26 @@ public class ArrayUtils {
return result;
}
/**
* <p>Converts an array of <code>Object</code> in to an Array of <code>Class</code> objects.</p>
*
* <p>This method returns <code>null</code> for a <code>null</code> input array.</p>
*
* @param array an <code>Object</code> array
* @return a <code>Class</code> array, <code>null</code> 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;
}
}

View File

@ -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[])}.