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 '$'
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/lang/trunk@919340 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
51df5ea90c
commit
f91a7c97d3
|
@ -740,8 +740,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
|
||||
|
@ -751,21 +752,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
|
||||
|
@ -779,8 +797,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
|
||||
|
@ -792,9 +811,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
|
||||
|
|
|
@ -44,6 +44,7 @@ public class ClassUtilsTest extends TestCase {
|
|||
}
|
||||
|
||||
private static class Inner {
|
||||
private class DeeplyNested{}
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------
|
||||
|
@ -787,6 +788,13 @@ public class ClassUtilsTest extends TestCase {
|
|||
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 @@ public class ClassUtilsTest extends TestCase {
|
|||
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 {
|
||||
|
|
Loading…
Reference in New Issue