Applying my patch from LANG-535 - adding support to getShortClassName and getPackageName for arrays, including primitive arrays and multi-dimensional arrays. Also stopped getPackageName relying on the underlying class.getPackage as it's sometimes null

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/lang/trunk@825420 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Henri Yandell 2009-10-15 07:31:32 +00:00
parent bb8709f3e3
commit 2dc5618257
2 changed files with 79 additions and 4 deletions

View File

@ -188,6 +188,24 @@ public class ClassUtils {
return StringUtils.EMPTY;
}
StringBuffer arrayPrefix = new StringBuffer();
// Handle array encoding
if (className.startsWith("[")) {
while (className.charAt(0) == '[') {
className = className.substring(1);
arrayPrefix.append("[]");
}
// Strip Object type encoding
if (className.charAt(0) == 'L' && className.charAt(className.length() - 1) == ';') {
className = className.substring(1, className.length() - 1);
}
}
if (reverseAbbreviationMap.containsKey(className)) {
className = reverseAbbreviationMap.get(className);
}
int lastDotIdx = className.lastIndexOf(PACKAGE_SEPARATOR_CHAR);
int innerIdx = className.indexOf(
INNER_CLASS_SEPARATOR_CHAR, lastDotIdx == -1 ? 0 : lastDotIdx + 1);
@ -195,7 +213,7 @@ public class ClassUtils {
if (innerIdx != -1) {
out = out.replace(INNER_CLASS_SEPARATOR_CHAR, PACKAGE_SEPARATOR_CHAR);
}
return out;
return out + arrayPrefix;
}
// Package name
@ -224,7 +242,7 @@ public class ClassUtils {
if (cls == null) {
return StringUtils.EMPTY;
}
return cls.getPackage().getName();
return getPackageName(cls.getName());
}
/**
@ -237,9 +255,19 @@ public class ClassUtils {
* @return the package name or an empty string
*/
public static String getPackageName(String className) {
if (className == null) {
if (className == null || className.length() == 0) {
return StringUtils.EMPTY;
}
// Strip array encoding
while (className.charAt(0) == '[') {
className = className.substring(1);
}
// Strip Object type encoding
if (className.charAt(0) == 'L' && className.charAt(className.length() - 1) == ';') {
className = className.substring(1);
}
int i = className.lastIndexOf(PACKAGE_SEPARATOR_CHAR);
if (i == -1) {
return StringUtils.EMPTY;

View File

@ -92,6 +92,35 @@ public class ClassUtilsTest extends TestCase {
assertEquals("ClassUtils", ClassUtils.getShortClassName(ClassUtils.class));
assertEquals("Map.Entry", ClassUtils.getShortClassName(Map.Entry.class));
assertEquals("", ClassUtils.getShortClassName((Class) null));
// LANG-535
assertEquals("String[]", ClassUtils.getShortClassName(String[].class));
assertEquals("Map.Entry[]", ClassUtils.getShortClassName(Map.Entry[].class));
// Primitives
assertEquals("boolean", ClassUtils.getShortClassName(boolean.class));
assertEquals("byte", ClassUtils.getShortClassName(byte.class));
assertEquals("char", ClassUtils.getShortClassName(char.class));
assertEquals("short", ClassUtils.getShortClassName(short.class));
assertEquals("int", ClassUtils.getShortClassName(int.class));
assertEquals("long", ClassUtils.getShortClassName(long.class));
assertEquals("float", ClassUtils.getShortClassName(float.class));
assertEquals("double", ClassUtils.getShortClassName(double.class));
// Primitive Arrays
assertEquals("boolean[]", ClassUtils.getShortClassName(boolean[].class));
assertEquals("byte[]", ClassUtils.getShortClassName(byte[].class));
assertEquals("char[]", ClassUtils.getShortClassName(char[].class));
assertEquals("short[]", ClassUtils.getShortClassName(short[].class));
assertEquals("int[]", ClassUtils.getShortClassName(int[].class));
assertEquals("long[]", ClassUtils.getShortClassName(long[].class));
assertEquals("float[]", ClassUtils.getShortClassName(float[].class));
assertEquals("double[]", ClassUtils.getShortClassName(double[].class));
// Arrays of arrays of ...
assertEquals("String[][]", ClassUtils.getShortClassName(String[][].class));
assertEquals("String[][][]", ClassUtils.getShortClassName(String[][][].class));
assertEquals("String[][][][]", ClassUtils.getShortClassName(String[][][][].class));
}
public void test_getShortClassName_String() {
@ -112,6 +141,24 @@ public class ClassUtilsTest extends TestCase {
assertEquals("java.lang", ClassUtils.getPackageName(String.class));
assertEquals("java.util", ClassUtils.getPackageName(Map.Entry.class));
assertEquals("", ClassUtils.getPackageName((Class)null));
// LANG-535
assertEquals("java.lang", ClassUtils.getPackageName(String[].class));
// Primitive Arrays
assertEquals("", ClassUtils.getPackageName(boolean[].class));
assertEquals("", ClassUtils.getPackageName(byte[].class));
assertEquals("", ClassUtils.getPackageName(char[].class));
assertEquals("", ClassUtils.getPackageName(short[].class));
assertEquals("", ClassUtils.getPackageName(int[].class));
assertEquals("", ClassUtils.getPackageName(long[].class));
assertEquals("", ClassUtils.getPackageName(float[].class));
assertEquals("", ClassUtils.getPackageName(double[].class));
// Arrays of arrays of ...
assertEquals("java.lang", ClassUtils.getPackageName(String[][].class));
assertEquals("java.lang", ClassUtils.getPackageName(String[][][].class));
assertEquals("java.lang", ClassUtils.getPackageName(String[][][][].class));
}
public void test_getPackageName_String() {