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, 31 Jul 2003 23:47:20 GMT
scolebourne    2003/07/31 16:47:20

  Modified:    lang/src/java/org/apache/commons/lang ClassUtils.java
               lang/src/test/org/apache/commons/lang ClassUtilsTest.java
  Log:
  Make ClassUtils handle null more nicely
  
  Revision  Changes    Path
  1.18      +19 -20    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.17
  retrieving revision 1.18
  diff -u -r1.17 -r1.18
  --- ClassUtils.java	19 Jul 2003 21:55:05 -0000	1.17
  +++ ClassUtils.java	31 Jul 2003 23:47:20 -0000	1.18
  @@ -59,7 +59,7 @@
   /**
    * <p>Provides utility methods for working for classes without using reflection.</p>
    *
  - * <p>This class throws exceptions for invalid <code>null</code> inputs.
  + * <p>This class handles invalid <code>null</code> inputs as best it
can.
    * Each method documents its behaviour in more detail.</p>
    *
    * @author Stephen Colebourne
  @@ -216,11 +216,11 @@
        * 
        * @param cls  the class to look up, must not be <code>null</code>
        * @return the <code>List</code> of superclasses in order going up from
this one
  -     * @throws IllegalArgumentException if the class is <code>null</code>
  +     *  <code>null</code> if null input
        */
       public static List getAllSuperclasses(Class cls) {
           if (cls == null) {
  -            throw new IllegalArgumentException("The class must not be null");
  +            return null;
           }
           List classes = new ArrayList();
           Class superclass = cls.getSuperclass();
  @@ -241,12 +241,12 @@
        * so the order is maintained.</p>
        * 
        * @param cls  the class to look up, must not be <code>null</code>
  -     * @return the <code>List</code> of interfaces in order
  -     * @throws IllegalArgumentException if the class is <code>null</code>
  +     * @return the <code>List</code> of interfaces in order,
  +     *  <code>null</code> if null input
        */
       public static List getAllInterfaces(Class cls) {
           if (cls == null) {
  -            throw new IllegalArgumentException("The class must not be null");
  +            return null;
           }
           List list = new ArrayList();
           while (cls != null) {
  @@ -331,13 +331,13 @@
        * <code>null</code>, <code>null</code> is stored in the output
<code>List</code>.</p>
        * 
        * @param classNames  the classNames to change
  -     * @return a <code>List</code> of Class objects corresponding to the class
names
  -     * @throws IllegalArgumentException if the classNames is <code>null</code>
  +     * @return a <code>List</code> of Class objects corresponding to the class
names,
  +     *  <code>null</code> if null input
        * @throws ClassCastException if classNames contains a non String entry
        */
       public static List convertClassNamesToClasses(List classNames) {
           if (classNames == null) {
  -            throw new IllegalArgumentException("The class names must not be null");
  +            return null;
           }
           List classes = new ArrayList(classNames.size());
           for (Iterator it = classNames.iterator(); it.hasNext();) {
  @@ -359,13 +359,13 @@
        * the returned list as <code>null</code>.</p>
        * 
        * @param classes  the classes to change
  -     * @return a <code>List</code> of Class objects corresponding to the class
names
  -     * @throws IllegalArgumentException if the classNames is <code>null</code>
  +     * @return a <code>List</code> of Class objects corresponding to the class
names,
  +     *  <code>null</code> if null input
        * @throws ClassCastException if classNames contains a non Class or null entry
        */
       public static List convertClassesToClassNames(List classes) {
           if (classes == null) {
  -            throw new IllegalArgumentException("The classes list must not be null");
  +            return null;
           }
           List classNames = new ArrayList(classes.size());
           for (Iterator it = classes.iterator(); it.hasNext();) {
  @@ -452,14 +452,13 @@
        * <em><a href="http://java.sun.com/docs/books/jls/">The Java Language
Specification</a></em>,
        * sections 5.1.1, 5.1.2 and 5.1.4 for details.</p>
        *
  -     * @param cls  the Class to check, may be <code>null</code>
  -     * @param toClass  the Class to try to assign into, must not be <code>null</code>
  +     * @param cls  the Class to check, may be null
  +     * @param toClass  the Class to try to assign into, returns false if null
        * @return <code>true</code> if assignment possible
  -     * @throws IllegalArgumentException if the toClass is <code>null</code>
        */
       public static boolean isAssignable(Class cls, Class toClass) {
           if (toClass == null) {
  -            throw new IllegalArgumentException("The class must not be null");
  +            return false;
           }
           // have to check for null, as isAssignableFrom doesn't
           if (cls == null) {
  @@ -519,12 +518,12 @@
        * <p>Is the specified class an inner class or static nested class.</p>
        * 
        * @param cls  the class to check
  -     * @return <code>true</code> if the class is an inner or static nested
class
  -     * @throws IllegalArgumentException if the class is <code>null</code>
  +     * @return <code>true</code> if the class is an inner or static nested
class,
  +     *  false if not or <code>null</code>
        */
       public static boolean isInnerClass(Class cls) {
           if (cls == null) {
  -            throw new IllegalArgumentException("The class must not be null");
  +            return false;
           }
           return (cls.getName().indexOf(INNER_CLASS_SEPARATOR_CHAR) >= 0);
       }
  
  
  
  1.5       +11 -31    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.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- ClassUtilsTest.java	30 Jul 2003 22:21:39 -0000	1.4
  +++ ClassUtilsTest.java	31 Jul 2003 23:47:20 -0000	1.5
  @@ -172,10 +172,8 @@
           assertEquals(2, list.size());
           assertEquals(CX.class, list.get(0));
           assertEquals(Object.class, list.get(1));
  -        try {
  -            ClassUtils.getAllSuperclasses(null);
  -            fail();
  -        } catch (IllegalArgumentException ex) {}
  +        
  +        assertEquals(null, ClassUtils.getAllSuperclasses(null));
       }
       
       public void test_getAllInterfaces_Class() {
  @@ -187,10 +185,8 @@
           assertEquals(IE.class, list.get(3));
           assertEquals(IF.class, list.get(4));
           assertEquals(IA.class, list.get(5));
  -        try {
  -            ClassUtils.getAllInterfaces(null);
  -            fail();
  -        } catch (IllegalArgumentException ex) {}
  +        
  +        assertEquals(null, ClassUtils.getAllInterfaces(null));
       }
       
       private static interface IA {
  @@ -230,11 +226,7 @@
               ClassUtils.convertClassNamesToClasses(list);
               fail();
           } catch (ClassCastException ex) {}
  -        
  -        try {
  -            ClassUtils.convertClassNamesToClasses(null);
  -            fail();
  -        } catch (IllegalArgumentException ex) {}
  +        assertEquals(null, ClassUtils.convertClassNamesToClasses(null));
       }
       
       public void test_convertClassesToClassNames_List() {
  @@ -256,11 +248,7 @@
               ClassUtils.convertClassesToClassNames(list);
               fail();
           } catch (ClassCastException ex) {}
  -        
  -        try {
  -            ClassUtils.convertClassesToClassNames(null);
  -            fail();
  -        } catch (IllegalArgumentException ex) {}
  +        assertEquals(null, ClassUtils.convertClassesToClassNames(null));
       }
       
       // -------------------------------------------------------------------------
  @@ -271,10 +259,7 @@
           }.getClass()));
           assertEquals(false, ClassUtils.isInnerClass(this.getClass()));
           assertEquals(false, ClassUtils.isInnerClass(String.class));
  -        try {
  -            ClassUtils.isInnerClass(null);
  -            fail();
  -        } catch (IllegalArgumentException ex) {}
  +        assertEquals(false, ClassUtils.isInnerClass(null));
       }
       
       // -------------------------------------------------------------------------
  @@ -297,14 +282,9 @@
       }
       
       public void test_isAssignable() throws Exception {
  -        try {
  -            ClassUtils.isAssignable(String.class, null);
  -            fail();
  -        } catch (IllegalArgumentException ex) {}
  -        try {
  -            ClassUtils.isAssignable((Class) null, (Class) null);
  -            fail();
  -        } catch (IllegalArgumentException ex) {}
  +        assertEquals(false, ClassUtils.isAssignable((Class) null, null));
  +        assertEquals(false, ClassUtils.isAssignable(String.class, null));
  +        
           assertEquals(true, ClassUtils.isAssignable(null, Object.class));
           assertEquals(true, ClassUtils.isAssignable(null, Integer.class));
           assertEquals(false, ClassUtils.isAssignable(null, Integer.TYPE));
  
  
  

---------------------------------------------------------------------
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