diff --git a/src/java/org/apache/commons/lang/ClassUtils.java b/src/java/org/apache/commons/lang/ClassUtils.java index d1c08f822..e54fe24a1 100644 --- a/src/java/org/apache/commons/lang/ClassUtils.java +++ b/src/java/org/apache/commons/lang/ClassUtils.java @@ -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; } + + /** + *
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
// ----------------------------------------------------------------------
diff --git a/src/test/org/apache/commons/lang/ClassUtilsTest.java b/src/test/org/apache/commons/lang/ClassUtilsTest.java
index 555950c8b..d2181fb74 100644
--- a/src/test/org/apache/commons/lang/ClassUtilsTest.java
+++ b/src/test/org/apache/commons/lang/ClassUtilsTest.java
@@ -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