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 static boolean isInnerClass(Class<?> cls) {
|
||||||
// ----------------------------------------------------------------------
|
// ----------------------------------------------------------------------
|
||||||
/**
|
/**
|
||||||
* Returns the class represented by <code>className</code> using the
|
* Returns the class represented by <code>className</code> using the
|
||||||
* <code>classLoader</code>. This implementation supports names like
|
* <code>classLoader</code>. This implementation supports the syntaxes
|
||||||
* "<code>java.lang.String[]</code>" as well as "<code>[Ljava.lang.String;</code>".
|
* "<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 classLoader the class loader to use to load the class
|
||||||
* @param className the class name
|
* @param className the class name
|
||||||
|
@ -751,21 +752,38 @@ public static boolean isInnerClass(Class<?> cls) {
|
||||||
*/
|
*/
|
||||||
public static Class<?> getClass(
|
public static Class<?> getClass(
|
||||||
ClassLoader classLoader, String className, boolean initialize) throws ClassNotFoundException {
|
ClassLoader classLoader, String className, boolean initialize) throws ClassNotFoundException {
|
||||||
Class<?> clazz;
|
try {
|
||||||
if (abbreviationMap.containsKey(className)) {
|
Class<?> clazz;
|
||||||
String clsName = "[" + abbreviationMap.get(className);
|
if (abbreviationMap.containsKey(className)) {
|
||||||
clazz = Class.forName(clsName, initialize, classLoader).getComponentType();
|
String clsName = "[" + abbreviationMap.get(className);
|
||||||
} else {
|
clazz = Class.forName(clsName, initialize, classLoader).getComponentType();
|
||||||
clazz = Class.forName(toCanonicalName(className), initialize, classLoader);
|
} 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>
|
* Returns the (initialized) class represented by <code>className</code>
|
||||||
* using the <code>classLoader</code>. This implementation supports names
|
* using the <code>classLoader</code>. This implementation supports
|
||||||
* like "<code>java.lang.String[]</code>" as well as
|
* the syntaxes "<code>java.util.Map.Entry[]</code>",
|
||||||
* "<code>[Ljava.lang.String;</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 classLoader the class loader to use to load the class
|
||||||
* @param className the class name
|
* @param className the class name
|
||||||
|
@ -779,8 +797,9 @@ public static Class<?> getClass(ClassLoader classLoader, String className) throw
|
||||||
/**
|
/**
|
||||||
* Returns the (initialized) class represented by <code>className</code>
|
* Returns the (initialized) class represented by <code>className</code>
|
||||||
* using the current thread's context class loader. This implementation
|
* using the current thread's context class loader. This implementation
|
||||||
* supports names like "<code>java.lang.String[]</code>" as well as
|
* supports the syntaxes "<code>java.util.Map.Entry[]</code>",
|
||||||
* "<code>[Ljava.lang.String;</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 className the class name
|
||||||
* @return the class represented by <code>className</code> using the current thread's context class loader
|
* @return the class represented by <code>className</code> using the current thread's context class loader
|
||||||
|
@ -792,9 +811,9 @@ public static Class<?> getClass(String className) throws ClassNotFoundException
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the class represented by <code>className</code> using the
|
* Returns the class represented by <code>className</code> using the
|
||||||
* current thread's context class loader. This implementation supports
|
* current thread's context class loader. This implementation supports the
|
||||||
* names like "<code>java.lang.String[]</code>" as well as
|
* syntaxes "<code>java.util.Map.Entry[]</code>", "<code>java.util.Map$Entry[]</code>",
|
||||||
* "<code>[Ljava.lang.String;</code>".
|
* "<code>[Ljava.util.Map.Entry;</code>", and "<code>[Ljava.util.Map$Entry;</code>".
|
||||||
*
|
*
|
||||||
* @param className the class name
|
* @param className the class name
|
||||||
* @param initialize whether the class must be initialized
|
* @param initialize whether the class must be initialized
|
||||||
|
|
|
@ -44,6 +44,7 @@ public ClassUtilsTest(String name) {
|
||||||
}
|
}
|
||||||
|
|
||||||
private static class Inner {
|
private static class Inner {
|
||||||
|
private class DeeplyNested{}
|
||||||
}
|
}
|
||||||
|
|
||||||
//-----------------------------------------------------------------------
|
//-----------------------------------------------------------------------
|
||||||
|
@ -787,6 +788,13 @@ public void testWithInterleavingWhitespace() throws ClassNotFoundException {
|
||||||
assertEquals( byte[].class, ClassUtils.getClass( "byte[\t\t\n\r] " ) );
|
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 {
|
public void testGetClassByNormalNameArrays() throws ClassNotFoundException {
|
||||||
assertEquals( int[].class, ClassUtils.getClass( "int[]" ) );
|
assertEquals( int[].class, ClassUtils.getClass( "int[]" ) );
|
||||||
assertEquals( long[].class, ClassUtils.getClass( "long[]" ) );
|
assertEquals( long[].class, ClassUtils.getClass( "long[]" ) );
|
||||||
|
@ -797,6 +805,10 @@ public void testGetClassByNormalNameArrays() throws ClassNotFoundException {
|
||||||
assertEquals( double[].class, ClassUtils.getClass( "double[]" ) );
|
assertEquals( double[].class, ClassUtils.getClass( "double[]" ) );
|
||||||
assertEquals( boolean[].class, ClassUtils.getClass( "boolean[]" ) );
|
assertEquals( boolean[].class, ClassUtils.getClass( "boolean[]" ) );
|
||||||
assertEquals( String[].class, ClassUtils.getClass( "java.lang.String[]" ) );
|
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 {
|
public void testGetClassByNormalNameArrays2D() throws ClassNotFoundException {
|
||||||
|
|
Loading…
Reference in New Issue