From d0c47689116024d41ab75bfa0ebda1ad0c60a5b4 Mon Sep 17 00:00:00 2001 From: "Gary D. Gregory" Date: Thu, 11 Oct 2007 01:51:41 +0000 Subject: [PATCH] [LANG-351] Extension to ClassUtils: Obtain the primitive class from a wrapper. Using @since 2.4. git-svn-id: https://svn.apache.org/repos/asf/commons/proper/lang/trunk@583668 13f79535-47bb-0310-9956-ffa450edef68 --- .../org/apache/commons/lang/ClassUtils.java | 65 +++++++++++++++++++ .../apache/commons/lang/ClassUtilsTest.java | 51 +++++++++++++++ 2 files changed, 116 insertions(+) diff --git a/src/java/org/apache/commons/lang/ClassUtils.java b/src/java/org/apache/commons/lang/ClassUtils.java index 97a2fb354..acde8b5b6 100644 --- a/src/java/org/apache/commons/lang/ClassUtils.java +++ b/src/java/org/apache/commons/lang/ClassUtils.java @@ -24,6 +24,7 @@ import java.util.Iterator; import java.util.List; import java.util.Map; + /** *

Operates on classes without using reflection.

* @@ -75,6 +76,20 @@ public class ClassUtils { primitiveWrapperMap.put(Void.TYPE, Void.TYPE); } + /** + * Maps wrapper Classes to their corresponding primitive types. + */ + private static Map wrapperPrimitiveMap = new HashMap(); + static { + for (Iterator it = primitiveWrapperMap.keySet().iterator(); it.hasNext();) { + Class primitiveClass = (Class) it.next(); + Class wrapperClass = (Class) primitiveWrapperMap.get(primitiveClass); + if (!primitiveClass.equals(wrapperClass)) { + wrapperPrimitiveMap.put(wrapperClass, primitiveClass); + } + } + } + /** * Maps a primitive class name to its corresponding abbreviation used in array class names. */ @@ -505,6 +520,56 @@ public class ClassUtils { return convertedClasses; } + /** + *

Converts the specified wrapper class to its corresponding primitive + * class.

+ * + *

This method is the counter part of primitiveToWrapper(). + * If the passed in class is a wrapper class for a primitive type, this + * primitive type will be returned (e.g. Integer.TYPE for + * Integer.class). For other classes, or if the parameter is + * null, the return value is null.

+ * + * @param cls the class to convert, may be null + * @return the corresponding primitive type if cls is a + * wrapper class, null otherwise + * @see #primitiveToWrapper(Class) + * @since 2.4 + */ + public static Class wrapperToPrimitive(Class cls) { + return (Class) wrapperPrimitiveMap.get(cls); + } + + /** + *

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

+ * + *

This method invokes wrapperToPrimitive() for each element + * of the passed in array.

+ * + * @param classes the class array to convert, may be null or empty + * @return an array which contains for each given class, the primitive class or + * null if the original class is not a wrapper class. null if null input. + * Empty array if an empty array passed in. + * @see #wrapperToPrimitive(Class) + * @since 2.4 + */ + public static Class[] wrappersToPrimitives(Class[] classes) { + if (classes == null) { + return null; + } + + if (classes.length == 0) { + return classes; + } + + Class[] convertedClasses = new Class[classes.length]; + for (int i=0; i < classes.length; i++) { + convertedClasses[i] = wrapperToPrimitive( 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 a4e27a2f6..d0dfd7bc0 100644 --- a/src/test/org/apache/commons/lang/ClassUtilsTest.java +++ b/src/test/org/apache/commons/lang/ClassUtilsTest.java @@ -31,6 +31,8 @@ import junit.framework.TestCase; import junit.framework.TestSuite; import junit.textui.TestRunner; + + /** * Unit tests {@link org.apache.commons.lang.ClassUtils}. * @@ -395,6 +397,55 @@ public class ClassUtilsTest extends TestCase { assertNotSame("unmodified", noPrimitives, ClassUtils.primitivesToWrappers(noPrimitives)); } + public void testWrapperToPrimitive() { + // an array with classes to convert + final Class[] primitives = { + Boolean.TYPE, Byte.TYPE, Character.TYPE, Short.TYPE, + Integer.TYPE, Long.TYPE, Float.TYPE, Double.TYPE + }; + for (int i = 0; i < primitives.length; i++) { + Class wrapperCls = ClassUtils.primitiveToWrapper(primitives[i]); + assertFalse("Still primitive", wrapperCls.isPrimitive()); + assertEquals(wrapperCls + " -> " + primitives[i], primitives[i], + ClassUtils.wrapperToPrimitive(wrapperCls)); + } + } + + public void testWrapperToPrimitiveNoWrapper() { + assertNull("Wrong result for non wrapper class", ClassUtils.wrapperToPrimitive(String.class)); + } + + public void testWrapperToPrimitiveNull() { + assertNull("Wrong result for null class", ClassUtils.wrapperToPrimitive(null)); + } + + public void testWrappersToPrimitives() { + // an array with classes to test + final Class[] classes = { + Boolean.class, Byte.class, Character.class, Short.class, + Integer.class, Long.class, Float.class, Double.class, + String.class, ClassUtils.class, null + }; + + Class[] primitives = ClassUtils.wrappersToPrimitives(classes); + // now test the result + assertEquals("Wrong length of result array", classes.length, primitives.length); + for (int i = 0; i < classes.length; i++) { + Class expectedPrimitive = ClassUtils.wrapperToPrimitive(classes[i]); + assertEquals(classes[i] + " -> " + expectedPrimitive, expectedPrimitive, + primitives[i]); + } + } + + public void testWrappersToPrimitivesNull() { + assertNull("Wrong result for null input", ClassUtils.wrappersToPrimitives(null)); + } + + public void testWrappersToPrimitivesEmpty() { + Class[] empty = new Class[0]; + assertEquals("Wrong result for empty input", empty, ClassUtils.wrappersToPrimitives(empty)); + } + public void testGetClassClassNotFound() throws Exception { assertGetClassThrowsClassNotFound( "bool" ); assertGetClassThrowsClassNotFound( "bool[]" );