[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
This commit is contained in:
parent
95b3ce93b8
commit
d0c4768911
|
@ -24,6 +24,7 @@ import java.util.Iterator;
|
|||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
|
||||
/**
|
||||
* <p>Operates on classes without using reflection.</p>
|
||||
*
|
||||
|
@ -75,6 +76,20 @@ public class ClassUtils {
|
|||
primitiveWrapperMap.put(Void.TYPE, Void.TYPE);
|
||||
}
|
||||
|
||||
/**
|
||||
* Maps wrapper <code>Class</code>es 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;
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>Converts the specified wrapper class to its corresponding primitive
|
||||
* class.</p>
|
||||
*
|
||||
* <p>This method is the counter part of <code>primitiveToWrapper()</code>.
|
||||
* If the passed in class is a wrapper class for a primitive type, this
|
||||
* primitive type will be returned (e.g. <code>Integer.TYPE</code> for
|
||||
* <code>Integer.class</code>). For other classes, or if the parameter is
|
||||
* <b>null</b>, the return value is <b>null</b>.</p>
|
||||
*
|
||||
* @param cls the class to convert, may be <b>null</b>
|
||||
* @return the corresponding primitive type if <code>cls</code> is a
|
||||
* wrapper class, <b>null</b> otherwise
|
||||
* @see #primitiveToWrapper(Class)
|
||||
* @since 2.4
|
||||
*/
|
||||
public static Class wrapperToPrimitive(Class cls) {
|
||||
return (Class) wrapperPrimitiveMap.get(cls);
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>Converts the specified array of wrapper Class objects to an array of
|
||||
* its corresponding primitive Class objects.</p>
|
||||
*
|
||||
* <p>This method invokes <code>wrapperToPrimitive()</code> for each element
|
||||
* of the passed in array.</p>
|
||||
*
|
||||
* @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
|
||||
* <b>null</b> if the original class is not a wrapper class. <code>null</code> 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
|
||||
// ----------------------------------------------------------------------
|
||||
/**
|
||||
|
|
|
@ -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[]" );
|
||||
|
|
Loading…
Reference in New Issue