rolling the forName method out for the 2.1 release

git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/lang/trunk@151282 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Henri Yandell 2005-02-04 01:21:19 +00:00
parent ab27cc9c09
commit 3378d09f16
2 changed files with 2 additions and 104 deletions

View File

@ -31,7 +31,7 @@
* @author Gary Gregory * @author Gary Gregory
* @author Norm Deane * @author Norm Deane
* @since 2.0 * @since 2.0
* @version $Id: ClassUtils.java,v 1.35 2005/01/27 06:45:11 bayard Exp $ * @version $Id$
*/ */
public class ClassUtils { public class ClassUtils {
@ -481,79 +481,6 @@ public static Class[] primitivesToWrappers(Class[] classes) {
return convertedClasses; 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 // Inner class
// ---------------------------------------------------------------------- // ----------------------------------------------------------------------
/** /**

View File

@ -32,7 +32,7 @@
* *
* @author Stephen Colebourne * @author Stephen Colebourne
* @author Gary D. Gregory * @author Gary D. Gregory
* @version $Id: ClassUtilsTest.java,v 1.15 2005/01/27 06:45:11 bayard Exp $ * @version $Id$
*/ */
public class ClassUtilsTest extends TestCase { public class ClassUtilsTest extends TestCase {
@ -389,35 +389,6 @@ public void testPrimitivesToWrappers() {
assertNotSame("unmodified", noPrimitives, ClassUtils.primitivesToWrappers(noPrimitives)); 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 { public static ClassLoader newSystemClassLoader() throws SecurityException, IllegalArgumentException {
ClassLoader scl = ClassLoader.getSystemClassLoader(); ClassLoader scl = ClassLoader.getSystemClassLoader();
if (!(scl instanceof URLClassLoader)) { if (!(scl instanceof URLClassLoader)) {