mirror of
https://github.com/apache/commons-lang.git
synced 2025-02-08 19:14:52 +00:00
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 git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/lang/trunk@137858 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
be6e4802f4
commit
14164d88eb
@ -32,7 +32,7 @@
|
||||
* @author Gary Gregory
|
||||
* @author Norm Deane
|
||||
* @since 2.0
|
||||
* @version $Id: ClassUtils.java,v 1.29 2004/06/27 04:42:54 bayard Exp $
|
||||
* @version $Id: ClassUtils.java,v 1.30 2004/06/30 18:33:58 ggregory Exp $
|
||||
*/
|
||||
public class ClassUtils {
|
||||
|
||||
@ -101,14 +101,12 @@ public static String getShortClassName(Object object, String valueIfNull) {
|
||||
/**
|
||||
* <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 @@ public static String getShortClassName(Class cls) {
|
||||
*
|
||||
* <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 @@ public static String getPackageName(Object object, String valueIfNull) {
|
||||
/**
|
||||
* <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 @@ public static String getPackageName(Class cls) {
|
||||
* <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);
|
||||
}
|
||||
|
@ -33,7 +33,7 @@
|
||||
*
|
||||
* @author Stephen Colebourne
|
||||
* @author Gary D. Gregory
|
||||
* @version $Id: ClassUtilsTest.java,v 1.12 2004/06/27 04:42:54 bayard Exp $
|
||||
* @version $Id: ClassUtilsTest.java,v 1.13 2004/06/30 18:33:58 ggregory Exp $
|
||||
*/
|
||||
public class ClassUtilsTest extends TestCase {
|
||||
|
||||
@ -83,23 +83,14 @@ public void test_getShortClassName_Object() {
|
||||
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_Object() {
|
||||
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(""));
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
|
Loading…
x
Reference in New Issue
Block a user