commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ggreg...@apache.org
Subject cvs commit: jakarta-commons/lang/src/java/org/apache/commons/lang ClassUtils.java
Date Wed, 30 Jun 2004 18:33:58 GMT
ggregory    2004/06/30 11:33:58

  Modified:    lang/src/test/org/apache/commons/lang ClassUtilsTest.java
               lang/src/java/org/apache/commons/lang ClassUtils.java
  Log:
  Bugzilla Bug 27877
  [lang][patch] Make ClassUtils methods null-safe and not throw an IAE.
  http://issues.apache.org/bugzilla/show_bug.cgi?id=27877
  
  Revision  Changes    Path
  1.13      +7 -25     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.12
  retrieving revision 1.13
  diff -u -r1.12 -r1.13
  --- ClassUtilsTest.java	27 Jun 2004 04:42:54 -0000	1.12
  +++ ClassUtilsTest.java	30 Jun 2004 18:33:58 -0000	1.13
  @@ -83,23 +83,14 @@
       public void test_getShortClassName_Class() {
           assertEquals("ClassUtils", ClassUtils.getShortClassName(ClassUtils.class));
           assertEquals("Map.Entry", ClassUtils.getShortClassName(Map.Entry.class));
  -        try {
  -            ClassUtils.getShortClassName((Class) null);
  -            fail();
  -        } catch (IllegalArgumentException ex) {}
  +        assertEquals("", ClassUtils.getShortClassName((Class) null));
       }
       
       public void test_getShortClassName_String() {
           assertEquals("ClassUtils", ClassUtils.getShortClassName(ClassUtils.class.getName()));
           assertEquals("Map.Entry", ClassUtils.getShortClassName(Map.Entry.class.getName()));
  -        try {
  -            ClassUtils.getShortClassName((String) null);
  -            fail();
  -        } catch (IllegalArgumentException ex) {}
  -        try {
  -            ClassUtils.getShortClassName("");
  -            fail();
  -        } catch (IllegalArgumentException ex) {}
  +        assertEquals("", ClassUtils.getShortClassName((String) null));
  +        assertEquals("", ClassUtils.getShortClassName(""));
       }
       
       // -------------------------------------------------------------------------
  @@ -112,23 +103,14 @@
       public void test_getPackageName_Class() {
           assertEquals("java.lang", ClassUtils.getPackageName(String.class));
           assertEquals("java.util", ClassUtils.getPackageName(Map.Entry.class));
  -        try {
  -            ClassUtils.getPackageName((Class) null);
  -            fail();
  -        } catch (IllegalArgumentException ex) {}
  +        assertEquals("", ClassUtils.getPackageName((Class)null));
       }
       
       public void test_getPackageName_String() {
           assertEquals("org.apache.commons.lang", ClassUtils.getPackageName(ClassUtils.class.getName()));
           assertEquals("java.util", ClassUtils.getPackageName(Map.Entry.class.getName()));
  -        try {
  -            ClassUtils.getPackageName((String) null);
  -            fail();
  -        } catch (IllegalArgumentException ex) {}
  -        try {
  -            ClassUtils.getPackageName("");
  -            fail();
  -        } catch (IllegalArgumentException ex) {}
  +        assertEquals("", ClassUtils.getPackageName((String)null));
  +        assertEquals("", ClassUtils.getPackageName(""));
       }
       
       // -------------------------------------------------------------------------
  
  
  
  1.30      +20 -24    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.29
  retrieving revision 1.30
  diff -u -r1.29 -r1.30
  --- ClassUtils.java	27 Jun 2004 04:42:54 -0000	1.29
  +++ ClassUtils.java	30 Jun 2004 18:33:58 -0000	1.30
  @@ -101,14 +101,12 @@
       /**
        * <p>Gets the class name minus the package name from a <code>Class</code>.</p>
        * 
  -     * @param cls  the class to get the short name for, must not be
  -     *  <code>null</code>
  -     * @return the class name without the package name
  -     * @throws IllegalArgumentException if the class is <code>null</code>
  +     * @param cls  the class to get the short name for.
  +     * @return the class name without the package name or an empty string
        */
       public static String getShortClassName(Class cls) {
           if (cls == null) {
  -            throw new IllegalArgumentException("The class must not be null");
  +            return StringUtils.EMPTY;
           }
           return getShortClassName(cls.getName());
       }
  @@ -118,14 +116,15 @@
        *
        * <p>The string passed in is assumed to be a class name - it is not checked.</p>
        * 
  -     * @param className  the className to get the short name for,
  -     *  must not be empty or <code>null</code>
  -     * @return the class name of the class without the package name
  -     * @throws IllegalArgumentException if the className is empty
  +     * @param className  the className to get the short name for
  +     * @return the class name of the class without the package name or an empty string
        */
       public static String getShortClassName(String className) {
  -        if (StringUtils.isEmpty(className)) {
  -            throw new IllegalArgumentException("The class name must not be empty");
  +        if (className == null) {
  +            return StringUtils.EMPTY;
  +        }
  +        if (className.length() == 0) {
  +            return StringUtils.EMPTY;
           }
           char[] chars = className.toCharArray();
           int lastDot = 0;
  @@ -158,14 +157,12 @@
       /**
        * <p>Gets the package name of a <code>Class</code>.</p>
        * 
  -     * @param cls  the class to get the package name for,
  -     *  must not be <code>null</code>
  -     * @return the package name
  -     * @throws IllegalArgumentException if the class is <code>null</code>
  +     * @param cls  the class to get the package name for, may be <code>null</code>.
  +     * @return the package name or an empty string
        */
       public static String getPackageName(Class cls) {
           if (cls == null) {
  -            throw new IllegalArgumentException("The class must not be null");
  +            return StringUtils.EMPTY;
           }
           return getPackageName(cls.getName());
       }
  @@ -174,19 +171,18 @@
        * <p>Gets the package name from a <code>String</code>.</p>
        *
        * <p>The string passed in is assumed to be a class name - it is not checked.</p>
  +	 * <p>If the class is unpackaged, return an empty string.</p>
        * 
  -     * @param className  the className to get the package name for,
  -     *  must not be empty or <code>null</code>
  -     * @return the package name
  -     * @throws IllegalArgumentException if the className is empty
  +     * @param className  the className to get the package name for, may be <code>null</code>
  +     * @return the package name or an empty string
        */
       public static String getPackageName(String className) {
  -        if (StringUtils.isEmpty(className)) {
  -            throw new IllegalArgumentException("The class name must not be empty");
  +        if (className == null) {
  +            return StringUtils.EMPTY;
           }
           int i = className.lastIndexOf(PACKAGE_SEPARATOR_CHAR);
           if (i == -1) {
  -            return "";
  +            return StringUtils.EMPTY;
           }
           return className.substring(0, i);
       }
  
  
  

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