From 2dc56182579703153e5c9886b3ecd22bbc348b49 Mon Sep 17 00:00:00 2001 From: Henri Yandell Date: Thu, 15 Oct 2009 07:31:32 +0000 Subject: [PATCH] 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 --- .../org/apache/commons/lang/ClassUtils.java | 34 +++++++++++-- .../apache/commons/lang/ClassUtilsTest.java | 49 ++++++++++++++++++- 2 files changed, 79 insertions(+), 4 deletions(-) diff --git a/src/java/org/apache/commons/lang/ClassUtils.java b/src/java/org/apache/commons/lang/ClassUtils.java index ebfd25a6e..478b37776 100644 --- a/src/java/org/apache/commons/lang/ClassUtils.java +++ b/src/java/org/apache/commons/lang/ClassUtils.java @@ -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; diff --git a/src/test/org/apache/commons/lang/ClassUtilsTest.java b/src/test/org/apache/commons/lang/ClassUtilsTest.java index 48986d6f6..896d542e4 100644 --- a/src/test/org/apache/commons/lang/ClassUtilsTest.java +++ b/src/test/org/apache/commons/lang/ClassUtilsTest.java @@ -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() { @@ -100,7 +129,7 @@ public class ClassUtilsTest extends TestCase { assertEquals("", ClassUtils.getShortClassName((String) null)); assertEquals("", ClassUtils.getShortClassName("")); } - + // ------------------------------------------------------------------------- public void test_getPackageName_Object() { assertEquals("org.apache.commons.lang", ClassUtils.getPackageName(new ClassUtils(), "")); @@ -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() {