commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bay...@apache.org
Subject svn commit: r825420 - in /commons/proper/lang/trunk/src: java/org/apache/commons/lang/ClassUtils.java test/org/apache/commons/lang/ClassUtilsTest.java
Date Thu, 15 Oct 2009 07:31:33 GMT
Author: bayard
Date: Thu Oct 15 07:31:32 2009
New Revision: 825420

URL: http://svn.apache.org/viewvc?rev=825420&view=rev
Log:
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

Modified:
    commons/proper/lang/trunk/src/java/org/apache/commons/lang/ClassUtils.java
    commons/proper/lang/trunk/src/test/org/apache/commons/lang/ClassUtilsTest.java

Modified: commons/proper/lang/trunk/src/java/org/apache/commons/lang/ClassUtils.java
URL: http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/java/org/apache/commons/lang/ClassUtils.java?rev=825420&r1=825419&r2=825420&view=diff
==============================================================================
--- commons/proper/lang/trunk/src/java/org/apache/commons/lang/ClassUtils.java (original)
+++ commons/proper/lang/trunk/src/java/org/apache/commons/lang/ClassUtils.java Thu Oct 15
07:31:32 2009
@@ -188,6 +188,24 @@
             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 @@
         if (innerIdx != -1) {
             out = out.replace(INNER_CLASS_SEPARATOR_CHAR, PACKAGE_SEPARATOR_CHAR);
         }
-        return out;
+        return out + arrayPrefix;
     }
 
     // Package name
@@ -224,7 +242,7 @@
         if (cls == null) {
             return StringUtils.EMPTY;
         }
-        return cls.getPackage().getName();
+        return getPackageName(cls.getName());
     }
 
     /**
@@ -237,9 +255,19 @@
      * @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;

Modified: commons/proper/lang/trunk/src/test/org/apache/commons/lang/ClassUtilsTest.java
URL: http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/test/org/apache/commons/lang/ClassUtilsTest.java?rev=825420&r1=825419&r2=825420&view=diff
==============================================================================
--- commons/proper/lang/trunk/src/test/org/apache/commons/lang/ClassUtilsTest.java (original)
+++ commons/proper/lang/trunk/src/test/org/apache/commons/lang/ClassUtilsTest.java Thu Oct
15 07:31:32 2009
@@ -92,6 +92,35 @@
         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 @@
         assertEquals("", ClassUtils.getShortClassName((String) null));
         assertEquals("", ClassUtils.getShortClassName(""));
     }
-    
+
     // -------------------------------------------------------------------------
     public void test_getPackageName_Object() {
         assertEquals("org.apache.commons.lang", ClassUtils.getPackageName(new ClassUtils(),
"<null>"));
@@ -112,6 +141,24 @@
         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() {



Mime
View raw message