commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From scolebou...@apache.org
Subject cvs commit: jakarta-commons/lang/src/test/org/apache/commons/lang ClassUtilsTest.java
Date Thu, 23 Oct 2003 21:03:44 GMT
scolebourne    2003/10/23 14:03:44

  Modified:    lang/src/java/org/apache/commons/lang ClassUtils.java
               lang/src/test/org/apache/commons/lang ClassUtilsTest.java
  Log:
  Add primitiveToWrapper()
  bug 23683, from Norm Deane
  
  Revision  Changes    Path
  1.23      +36 -2     jakarta-commons/lang/src/java/org/apache/commons/lang/ClassUtils.java
  
  Index: ClassUtils.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/lang/src/java/org/apache/commons/lang/ClassUtils.java,v
  retrieving revision 1.22
  retrieving revision 1.23
  diff -u -r1.22 -r1.23
  --- ClassUtils.java	7 Sep 2003 14:32:34 -0000	1.22
  +++ ClassUtils.java	23 Oct 2003 21:03:43 -0000	1.23
  @@ -54,8 +54,10 @@
   package org.apache.commons.lang;
   
   import java.util.ArrayList;
  +import java.util.HashMap;
   import java.util.Iterator;
   import java.util.List;
  +import java.util.Map;
   /**
    * <p>Operates on classes without using reflection.</p>
    *
  @@ -64,6 +66,7 @@
    *
    * @author Stephen Colebourne
    * @author Gary Gregory
  + * @author Norm Deane
    * @since 2.0
    * @version $Id$
    */
  @@ -89,6 +92,21 @@
        */
       public static final String INNER_CLASS_SEPARATOR = String.valueOf(INNER_CLASS_SEPARATOR_CHAR);
       
  +    /** 
  +     * Maps primitive <code>Class</code>es to their corresponding wrapper <code>Class</code>.

  +     */
  +    private static Map  primitiveWrapperMap = new HashMap();
  +    static {
  +         primitiveWrapperMap.put(Boolean.TYPE, Boolean.class);
  +         primitiveWrapperMap.put(Byte.TYPE, Byte.class);
  +         primitiveWrapperMap.put(Character.TYPE, Character.class);
  +         primitiveWrapperMap.put(Short.TYPE, Short.class);
  +         primitiveWrapperMap.put(Integer.TYPE, Integer.class);
  +         primitiveWrapperMap.put(Long.TYPE, Long.class);
  +         primitiveWrapperMap.put(Double.TYPE, Double.class);
  +         primitiveWrapperMap.put(Float.TYPE, Float.class);
  +    }
  +    
       /**
        * <p>ClassUtils instances should NOT be constructed in standard programming.
        * Instead, the class should be used as
  @@ -514,12 +532,28 @@
           return toClass.isAssignableFrom(cls);
       }
       
  +    /**
  +     * <p>Converts the specified primitive Class object to its corresponding
  +     * wrapper Class object.</p>
  +     *
  +     * @param cls  the class to convert, may be null
  +     * @return the wrapper class for <code>cls</code> or <code>cls</code>
if
  +     * <code>cls</code> is not a primitive. <code>null</code> if
null input.
  +     */
  +    public static Class primitiveToWrapper(Class cls) {
  +        Class convertedClass = cls;
  +        if (cls != null && cls.isPrimitive()) {
  +            convertedClass = (Class) primitiveWrapperMap.get(cls);
  +        }   
  +        return convertedClass;
  +    }
  +    
       // Inner class
       // ----------------------------------------------------------------------
       /**
        * <p>Is the specified class an inner class or static nested class.</p>
        * 
  -     * @param cls  the class to check
  +     * @param cls  the class to check, may be null
        * @return <code>true</code> if the class is an inner or static nested
class,
        *  false if not or <code>null</code>
        */
  
  
  
  1.7       +33 -1     jakarta-commons/lang/src/test/org/apache/commons/lang/ClassUtilsTest.java
  
  Index: ClassUtilsTest.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/lang/src/test/org/apache/commons/lang/ClassUtilsTest.java,v
  retrieving revision 1.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- ClassUtilsTest.java	18 Aug 2003 02:22:25 -0000	1.6
  +++ ClassUtilsTest.java	23 Oct 2003 21:03:44 -0000	1.7
  @@ -379,6 +379,38 @@
           assertEquals("boolean -> boolean", true, ClassUtils.isAssignable(Boolean.TYPE,
Boolean.TYPE));
       }
       
  +    public void testPrimitiveToWrapper() {
  +       
  +        // test primitive classes
  +        assertEquals("boolean -> Boolean.class", 
  +            Boolean.class, ClassUtils.primitiveToWrapper(Boolean.TYPE));   
  +        assertEquals("byte -> Byte.class",
  +            Byte.class, ClassUtils.primitiveToWrapper(Byte.TYPE));
  +        assertEquals("char -> Character.class",
  +            Character.class, ClassUtils.primitiveToWrapper(Character.TYPE));
  +        assertEquals("short -> Short.class",
  +            Short.class, ClassUtils.primitiveToWrapper(Short.TYPE));
  +        assertEquals("int -> Integer.class",
  +            Integer.class, ClassUtils.primitiveToWrapper(Integer.TYPE));
  +        assertEquals("long -> Long.class",
  +            Long.class, ClassUtils.primitiveToWrapper(Long.TYPE));
  +        assertEquals("double -> Double.class",
  +            Double.class, ClassUtils.primitiveToWrapper(Double.TYPE));
  +        assertEquals("float -> Float.class",
  +            Float.class, ClassUtils.primitiveToWrapper(Float.TYPE));
  +        
  +        // test a few other classes
  +        assertEquals("String.class -> String.class",
  +            String.class, ClassUtils.primitiveToWrapper(String.class));
  +        assertEquals("ClassUtils.class -> ClassUtils.class",
  +            org.apache.commons.lang.ClassUtils.class, 
  +            ClassUtils.primitiveToWrapper(org.apache.commons.lang.ClassUtils.class));
  +            
  +        // test null     
  +        assertNull("null -> null",
  +            ClassUtils.primitiveToWrapper(null));
  +    }
  +    
   //    public static List getAssignableFrom(List classes, Class superclass) {
   //    public static boolean isAssignable(Class[] classArray, Class[] toClassArray) {
   //    public static boolean isAssignable(Class cls, Class toClass) {
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Mime
View raw message