commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From nia...@apache.org
Subject svn commit: r904093 - in /commons/proper/lang/branches/LANG_2_X/src: main/java/org/apache/commons/lang/ClassUtils.java test/java/org/apache/commons/lang/ClassUtilsTest.java
Date Thu, 28 Jan 2010 14:02:26 GMT
Author: niallp
Date: Thu Jan 28 14:02:25 2010
New Revision: 904093

URL: http://svn.apache.org/viewvc?rev=904093&view=rev
Log:
Port LANG-535 to 2.x branch - 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/branches/LANG_2_X/src/main/java/org/apache/commons/lang/ClassUtils.java
    commons/proper/lang/branches/LANG_2_X/src/test/java/org/apache/commons/lang/ClassUtilsTest.java

Modified: commons/proper/lang/branches/LANG_2_X/src/main/java/org/apache/commons/lang/ClassUtils.java
URL: http://svn.apache.org/viewvc/commons/proper/lang/branches/LANG_2_X/src/main/java/org/apache/commons/lang/ClassUtils.java?rev=904093&r1=904092&r2=904093&view=diff
==============================================================================
--- commons/proper/lang/branches/LANG_2_X/src/main/java/org/apache/commons/lang/ClassUtils.java
(original)
+++ commons/proper/lang/branches/LANG_2_X/src/main/java/org/apache/commons/lang/ClassUtils.java
Thu Jan 28 14:02:25 2010
@@ -156,7 +156,7 @@
         if (object == null) {
             return valueIfNull;
         }
-        return getShortClassName(object.getClass().getName());
+        return getShortClassName(object.getClass());
     }
 
     /**
@@ -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 = (String)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
@@ -211,7 +229,7 @@
         if (object == null) {
             return valueIfNull;
         }
-        return getPackageName(object.getClass().getName());
+        return getPackageName(object.getClass());
     }
 
     /**
@@ -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/branches/LANG_2_X/src/test/java/org/apache/commons/lang/ClassUtilsTest.java
URL: http://svn.apache.org/viewvc/commons/proper/lang/branches/LANG_2_X/src/test/java/org/apache/commons/lang/ClassUtilsTest.java?rev=904093&r1=904092&r2=904093&view=diff
==============================================================================
--- commons/proper/lang/branches/LANG_2_X/src/test/java/org/apache/commons/lang/ClassUtilsTest.java
(original)
+++ commons/proper/lang/branches/LANG_2_X/src/test/java/org/apache/commons/lang/ClassUtilsTest.java
Thu Jan 28 14:02:25 2010
@@ -90,6 +90,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() {
@@ -98,7 +127,7 @@
         assertEquals("", ClassUtils.getShortClassName((String) null));
         assertEquals("", ClassUtils.getShortClassName(""));
     }
-    
+
     // -------------------------------------------------------------------------
     public void test_getPackageName_Object() {
         assertEquals("org.apache.commons.lang", ClassUtils.getPackageName(new ClassUtils(),
"<null>"));
@@ -110,6 +139,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