commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From nia...@apache.org
Subject svn commit: r1056767 - 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 Sat, 08 Jan 2011 19:11:50 GMT
Author: niallp
Date: Sat Jan  8 19:11:50 2011
New Revision: 1056767

URL: http://svn.apache.org/viewvc?rev=1056767&view=rev
Log:
Port LANG-599 to LANG 2.x Branch -  ClassUtils.getClass(): Allow Dots as Inner Class Separators

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=1056767&r1=1056766&r2=1056767&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
Sat Jan  8 19:11:50 2011
@@ -735,8 +735,9 @@ public class ClassUtils {
     // ----------------------------------------------------------------------
     /**
      * Returns the class represented by <code>className</code> using the
-     * <code>classLoader</code>.  This implementation supports names like
-     * "<code>java.lang.String[]</code>" as well as "<code>[Ljava.lang.String;</code>".
+     * <code>classLoader</code>.  This implementation supports the syntaxes
+     * "<code>java.util.Map.Entry[]</code>", "<code>java.util.Map$Entry[]</code>",
+     * "<code>[Ljava.util.Map.Entry;</code>", and "<code>[Ljava.util.Map$Entry;</code>".
      *
      * @param classLoader  the class loader to use to load the class
      * @param className  the class name
@@ -746,21 +747,38 @@ public class ClassUtils {
      */
     public static Class getClass(
             ClassLoader classLoader, String className, boolean initialize) throws ClassNotFoundException
{
-        Class clazz;
-        if (abbreviationMap.containsKey(className)) {
-            String clsName = "[" + abbreviationMap.get(className);
-            clazz = Class.forName(clsName, initialize, classLoader).getComponentType();
-        } else {
-            clazz = Class.forName(toCanonicalName(className), initialize, classLoader);
+        try {
+            Class clazz;
+            if (abbreviationMap.containsKey(className)) {
+                String clsName = "[" + abbreviationMap.get(className);
+                clazz = Class.forName(clsName, initialize, classLoader).getComponentType();
+            } else {
+                clazz = Class.forName(toCanonicalName(className), initialize, classLoader);
+            }
+            return clazz;
+        } catch (ClassNotFoundException ex) {
+            // allow path separators (.) as inner class name separators
+            int lastDotIndex = className.lastIndexOf(PACKAGE_SEPARATOR_CHAR);
+
+            if (lastDotIndex != -1) {
+                try {
+                    return getClass(classLoader, className.substring(0, lastDotIndex) +
+                            INNER_CLASS_SEPARATOR_CHAR + className.substring(lastDotIndex
+ 1),
+                            initialize);
+                } catch (ClassNotFoundException ex2) {
+                }
+            }
+
+            throw ex;
         }
-        return clazz;
     }
 
     /**
      * Returns the (initialized) class represented by <code>className</code>
-     * using the <code>classLoader</code>.  This implementation supports names
-     * like "<code>java.lang.String[]</code>" as well as
-     * "<code>[Ljava.lang.String;</code>".
+     * using the <code>classLoader</code>.  This implementation supports
+     * the syntaxes "<code>java.util.Map.Entry[]</code>",
+     * "<code>java.util.Map$Entry[]</code>", "<code>[Ljava.util.Map.Entry;</code>",
+     * and "<code>[Ljava.util.Map$Entry;</code>".
      *
      * @param classLoader  the class loader to use to load the class
      * @param className  the class name
@@ -774,8 +792,9 @@ public class ClassUtils {
     /**
      * Returns the (initialized) class represented by <code>className</code>
      * using the current thread's context class loader. This implementation
-     * supports names like "<code>java.lang.String[]</code>" as well as
-     * "<code>[Ljava.lang.String;</code>".
+     * supports the syntaxes "<code>java.util.Map.Entry[]</code>",
+     * "<code>java.util.Map$Entry[]</code>", "<code>[Ljava.util.Map.Entry;</code>",
+     * and "<code>[Ljava.util.Map$Entry;</code>".
      *
      * @param className  the class name
      * @return the class represented by <code>className</code> using the current
thread's context class loader
@@ -787,9 +806,9 @@ public class ClassUtils {
 
     /**
      * Returns the class represented by <code>className</code> using the
-     * current thread's context class loader. This implementation supports
-     * names like "<code>java.lang.String[]</code>" as well as
-     * "<code>[Ljava.lang.String;</code>".
+     * current thread's context class loader. This implementation supports the
+     * syntaxes "<code>java.util.Map.Entry[]</code>", "<code>java.util.Map$Entry[]</code>",
+     * "<code>[Ljava.util.Map.Entry;</code>", and "<code>[Ljava.util.Map$Entry;</code>".
      *
      * @param className  the class name
      * @param initialize  whether the class must be initialized

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=1056767&r1=1056766&r2=1056767&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
Sat Jan  8 19:11:50 2011
@@ -46,6 +46,7 @@ public class ClassUtilsTest extends Test
     }
 
     private static class Inner {
+        private class DeeplyNested{}
     }
     
     //-----------------------------------------------------------------------
@@ -603,6 +604,13 @@ public class ClassUtilsTest extends Test
         assertEquals( byte[].class, ClassUtils.getClass( "byte[\t\t\n\r]   " ) );
     }
 
+    public void testGetInnerClass() throws ClassNotFoundException {
+        assertEquals( Inner.DeeplyNested.class, ClassUtils.getClass( "org.apache.commons.lang.ClassUtilsTest.Inner.DeeplyNested"
) );
+        assertEquals( Inner.DeeplyNested.class, ClassUtils.getClass( "org.apache.commons.lang.ClassUtilsTest.Inner$DeeplyNested"
) );
+        assertEquals( Inner.DeeplyNested.class, ClassUtils.getClass( "org.apache.commons.lang.ClassUtilsTest$Inner$DeeplyNested"
) );
+        assertEquals( Inner.DeeplyNested.class, ClassUtils.getClass( "org.apache.commons.lang.ClassUtilsTest$Inner.DeeplyNested"
) );
+    }
+
     public void testGetClassByNormalNameArrays() throws ClassNotFoundException {
         assertEquals( int[].class, ClassUtils.getClass( "int[]" ) );
         assertEquals( long[].class, ClassUtils.getClass( "long[]" ) );
@@ -613,6 +621,10 @@ public class ClassUtilsTest extends Test
         assertEquals( double[].class, ClassUtils.getClass( "double[]" ) );
         assertEquals( boolean[].class, ClassUtils.getClass( "boolean[]" ) );
         assertEquals( String[].class, ClassUtils.getClass( "java.lang.String[]" ) );
+        assertEquals( java.util.Map.Entry[].class, ClassUtils.getClass( "java.util.Map.Entry[]"
) );
+        assertEquals( java.util.Map.Entry[].class, ClassUtils.getClass( "java.util.Map$Entry[]"
) );
+        assertEquals( java.util.Map.Entry[].class, ClassUtils.getClass( "[Ljava.util.Map.Entry;"
) );
+        assertEquals( java.util.Map.Entry[].class, ClassUtils.getClass( "[Ljava.util.Map$Entry;"
) );
     }
 
     public void testGetClassByNormalNameArrays2D() throws ClassNotFoundException {



Mime
View raw message