commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bay...@apache.org
Subject svn commit: r919340 - in /commons/proper/lang/trunk/src: main/java/org/apache/commons/lang3/ClassUtils.java test/java/org/apache/commons/lang3/ClassUtilsTest.java
Date Fri, 05 Mar 2010 09:05:47 GMT
Author: bayard
Date: Fri Mar  5 09:05:47 2010
New Revision: 919340

URL: http://svn.apache.org/viewvc?rev=919340&view=rev
Log:
Per David M. Sledge's patch to LANG-599, added support to ClassUtils.getClass(String) to support
inner classes being separated by '.' as well as the official '$'

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

Modified: commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/ClassUtils.java
URL: http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/ClassUtils.java?rev=919340&r1=919339&r2=919340&view=diff
==============================================================================
--- commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/ClassUtils.java (original)
+++ commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/ClassUtils.java Fri Mar
 5 09:05:47 2010
@@ -740,8 +740,9 @@
     // ----------------------------------------------------------------------
     /**
      * 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
@@ -751,21 +752,38 @@
      */
     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
@@ -779,8 +797,9 @@
     /**
      * 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
@@ -792,9 +811,9 @@
 
     /**
      * 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/trunk/src/test/java/org/apache/commons/lang3/ClassUtilsTest.java
URL: http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/ClassUtilsTest.java?rev=919340&r1=919339&r2=919340&view=diff
==============================================================================
--- commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/ClassUtilsTest.java (original)
+++ commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/ClassUtilsTest.java Fri
Mar  5 09:05:47 2010
@@ -44,6 +44,7 @@
     }
 
     private static class Inner {
+        private class DeeplyNested{}
     }
 
     //-----------------------------------------------------------------------
@@ -787,6 +788,13 @@
         assertEquals( byte[].class, ClassUtils.getClass( "byte[\t\t\n\r]   " ) );
     }
 
+    public void testGetInnerClass() throws ClassNotFoundException {
+        assertEquals( Inner.DeeplyNested.class, ClassUtils.getClass( "org.apache.commons.lang3.ClassUtilsTest.Inner.DeeplyNested"
) );
+        assertEquals( Inner.DeeplyNested.class, ClassUtils.getClass( "org.apache.commons.lang3.ClassUtilsTest.Inner$DeeplyNested"
) );
+        assertEquals( Inner.DeeplyNested.class, ClassUtils.getClass( "org.apache.commons.lang3.ClassUtilsTest$Inner$DeeplyNested"
) );
+        assertEquals( Inner.DeeplyNested.class, ClassUtils.getClass( "org.apache.commons.lang3.ClassUtilsTest$Inner.DeeplyNested"
) );
+    }
+
     public void testGetClassByNormalNameArrays() throws ClassNotFoundException {
         assertEquals( int[].class, ClassUtils.getClass( "int[]" ) );
         assertEquals( long[].class, ClassUtils.getClass( "long[]" ) );
@@ -797,6 +805,10 @@
         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