added Class.forName improvement to see what people think
git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/lang/trunk@138016 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
4af43102aa
commit
9bf6c67314
|
@ -31,7 +31,7 @@ import java.util.Map;
|
|||
* @author Gary Gregory
|
||||
* @author Norm Deane
|
||||
* @since 2.0
|
||||
* @version $Id: ClassUtils.java,v 1.34 2004/12/19 22:35:38 scolebourne Exp $
|
||||
* @version $Id: ClassUtils.java,v 1.35 2005/01/27 06:45:11 bayard Exp $
|
||||
*/
|
||||
public class ClassUtils {
|
||||
|
||||
|
@ -480,6 +480,79 @@ public class ClassUtils {
|
|||
}
|
||||
return convertedClasses;
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>Enhanced version of java.lang.Class.forName(String) that can handle
|
||||
* primitive types and arrays using the Foo[] notation.
|
||||
*
|
||||
* @param name the fully qualified name of the class to create
|
||||
* @return the desired class
|
||||
* @since 2.1
|
||||
*/
|
||||
public static Class forName(String name) throws ClassNotFoundException {
|
||||
String fixedName = name;
|
||||
if(name.endsWith("[]")) {
|
||||
fixedName = "[L" + name.substring(0, name.length() - "[]".length()) + ";";
|
||||
}
|
||||
try {
|
||||
return Class.forName(fixedName);
|
||||
} catch(ClassNotFoundException cnfe) {
|
||||
// try primitives
|
||||
if("boolean".equals(name)) {
|
||||
return boolean.class;
|
||||
} else
|
||||
if("char".equals(name)) {
|
||||
return char.class;
|
||||
} else
|
||||
if("byte".equals(name)) {
|
||||
return byte.class;
|
||||
} else
|
||||
if("short".equals(name)) {
|
||||
return short.class;
|
||||
} else
|
||||
if("int".equals(name)) {
|
||||
return int.class;
|
||||
} else
|
||||
if("long".equals(name)) {
|
||||
return long.class;
|
||||
} else
|
||||
if("float".equals(name)) {
|
||||
return float.class;
|
||||
} else
|
||||
if("double".equals(name)) {
|
||||
return double.class;
|
||||
}
|
||||
|
||||
// try primitive arrays
|
||||
if("boolean[]".equals(name)) {
|
||||
return boolean[].class;
|
||||
} else
|
||||
if("char[]".equals(name)) {
|
||||
return char[].class;
|
||||
} else
|
||||
if("byte[]".equals(name)) {
|
||||
return byte[].class;
|
||||
} else
|
||||
if("short[]".equals(name)) {
|
||||
return short[].class;
|
||||
} else
|
||||
if("int[]".equals(name)) {
|
||||
return int[].class;
|
||||
} else
|
||||
if("long[]".equals(name)) {
|
||||
return long[].class;
|
||||
} else
|
||||
if("float[]".equals(name)) {
|
||||
return float[].class;
|
||||
} else
|
||||
if("double[]".equals(name)) {
|
||||
return double[].class;
|
||||
}
|
||||
|
||||
throw cnfe;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// Inner class
|
||||
// ----------------------------------------------------------------------
|
||||
|
|
|
@ -32,7 +32,7 @@ import junit.textui.TestRunner;
|
|||
*
|
||||
* @author Stephen Colebourne
|
||||
* @author Gary D. Gregory
|
||||
* @version $Id: ClassUtilsTest.java,v 1.14 2004/12/19 22:35:38 scolebourne Exp $
|
||||
* @version $Id: ClassUtilsTest.java,v 1.15 2005/01/27 06:45:11 bayard Exp $
|
||||
*/
|
||||
public class ClassUtilsTest extends TestCase {
|
||||
|
||||
|
@ -389,6 +389,35 @@ public class ClassUtilsTest extends TestCase {
|
|||
assertNotSame("unmodified", noPrimitives, ClassUtils.primitivesToWrappers(noPrimitives));
|
||||
}
|
||||
|
||||
public void testForName() {
|
||||
String[] names = new String[] {
|
||||
"boolean", "char", "byte", "short", "int", "long", "float", "double",
|
||||
"boolean[]", "char[]", "byte[]", "short[]", "int[]", "long[]", "float[]", "double[]",
|
||||
"java.lang.Object[]", "java.lang.String", "java.lang.String[]"
|
||||
};
|
||||
|
||||
Class[] classes = new Class[] {
|
||||
boolean.class, char.class, byte.class, short.class, int.class, long.class, float.class, double.class,
|
||||
boolean[].class, char[].class, byte[].class, short[].class, int[].class, long[].class, float[].class, double[].class,
|
||||
Object[].class, String.class, String[].class
|
||||
};
|
||||
|
||||
for(int i=0; i<names.length; i++) {
|
||||
try {
|
||||
assertEquals( "Incorrect class found. ", classes[i], ClassUtils.forName(names[i]) );
|
||||
} catch(ClassNotFoundException cnfe) {
|
||||
fail("Failed to find class for '" + names[i] + "'");
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
ClassUtils.forName("SomeSillyMadeUpClassName");
|
||||
fail("Non-existent classname should have thrown an exception. ");
|
||||
} catch(ClassNotFoundException cnfe) {
|
||||
// should fail
|
||||
}
|
||||
}
|
||||
|
||||
public static ClassLoader newSystemClassLoader() throws SecurityException, IllegalArgumentException {
|
||||
ClassLoader scl = ClassLoader.getSystemClassLoader();
|
||||
if (!(scl instanceof URLClassLoader)) {
|
||||
|
|
Loading…
Reference in New Issue