tapestry-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From hls...@apache.org
Subject svn commit: r1089580 - in /tapestry/tapestry5/trunk/plastic/src: main/java/org/apache/tapestry5/internal/plastic/PlasticInternalUtils.java test/groovy/org/apache/tapestry5/internal/plastic/PlasticUtilsTests.groovy
Date Wed, 06 Apr 2011 19:11:00 GMT
Author: hlship
Date: Wed Apr  6 19:11:00 2011
New Revision: 1089580

URL: http://svn.apache.org/viewvc?rev=1089580&view=rev
Log:
TAP5-853: Add utility for converting a Java source type name to a Class (handling primitives
and arrays)

Modified:
    tapestry/tapestry5/trunk/plastic/src/main/java/org/apache/tapestry5/internal/plastic/PlasticInternalUtils.java
    tapestry/tapestry5/trunk/plastic/src/test/groovy/org/apache/tapestry5/internal/plastic/PlasticUtilsTests.groovy

Modified: tapestry/tapestry5/trunk/plastic/src/main/java/org/apache/tapestry5/internal/plastic/PlasticInternalUtils.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/plastic/src/main/java/org/apache/tapestry5/internal/plastic/PlasticInternalUtils.java?rev=1089580&r1=1089579&r2=1089580&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/plastic/src/main/java/org/apache/tapestry5/internal/plastic/PlasticInternalUtils.java
(original)
+++ tapestry/tapestry5/trunk/plastic/src/main/java/org/apache/tapestry5/internal/plastic/PlasticInternalUtils.java
Wed Apr  6 19:11:00 2011
@@ -17,6 +17,7 @@ package org.apache.tapestry5.internal.pl
 import java.io.Closeable;
 import java.io.IOException;
 import java.io.PrintWriter;
+import java.lang.reflect.Array;
 import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.HashSet;
@@ -293,4 +294,65 @@ public class PlasticInternalUtils
 
         return String.valueOf(Character.toUpperCase(first)) + input.substring(1);
     }
+
+    private static final Map<String, Class> PRIMITIVES = new HashMap<String, Class>();
+
+    static
+    {
+        PRIMITIVES.put("boolean", boolean.class);
+        PRIMITIVES.put("char", char.class);
+        PRIMITIVES.put("byte", byte.class);
+        PRIMITIVES.put("short", short.class);
+        PRIMITIVES.put("int", int.class);
+        PRIMITIVES.put("long", long.class);
+        PRIMITIVES.put("float", float.class);
+        PRIMITIVES.put("double", double.class);
+        PRIMITIVES.put("void", void.class);
+    }
+
+    /**
+     * @param loader
+     *            class loader to look up in
+     * @param javaName
+     *            java name is Java source format (e.g., "int", "int[]", "java.lang.String",
"java.lang.String[]", etc.)
+     * @return class instance
+     * @throws ClassNotFoundException
+     */
+    public static Class toClass(ClassLoader loader, String javaName) throws ClassNotFoundException
+    {
+        int depth = 0;
+
+        while (javaName.endsWith("[]"))
+        {
+            depth++;
+            javaName = javaName.substring(0, javaName.length() - 2);
+        }
+
+        Class primitive = PRIMITIVES.get(javaName);
+
+        if (primitive != null)
+        {
+            Class result = primitive;
+            for (int i = 0; i < depth; i++)
+            {
+                result = Array.newInstance(result, 0).getClass();
+            }
+
+            return result;
+        }
+
+        if (depth == 0)
+            return Class.forName(javaName, true, loader);
+
+        StringBuilder builder = new StringBuilder(20);
+
+        for (int i = 0; i < depth; i++)
+        {
+            builder.append("[");
+        }
+
+        builder.append("L").append(javaName).append(";");
+
+        return Class.forName(builder.toString(), true, loader);
+    }
 }

Modified: tapestry/tapestry5/trunk/plastic/src/test/groovy/org/apache/tapestry5/internal/plastic/PlasticUtilsTests.groovy
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/plastic/src/test/groovy/org/apache/tapestry5/internal/plastic/PlasticUtilsTests.groovy?rev=1089580&r1=1089579&r2=1089580&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/plastic/src/test/groovy/org/apache/tapestry5/internal/plastic/PlasticUtilsTests.groovy
(original)
+++ tapestry/tapestry5/trunk/plastic/src/test/groovy/org/apache/tapestry5/internal/plastic/PlasticUtilsTests.groovy
Wed Apr  6 19:11:00 2011
@@ -44,6 +44,24 @@ class PlasticUtilsTests extends Specific
         'Lfoo/bar/Baz$Biff;' | 'foo.bar.Baz$Biff'
     }
 
+    @Unroll("toClass '#javaName' should be #expectedClass")
+    def "toClass tests"() {
+        expect:
+
+        PlasticInternalUtils.toClass(getClass().classLoader, javaName) == expectedClass
+
+        where:
+
+        javaName | expectedClass
+        "java.lang.String" | String.class
+        "java.lang.Integer[]" | Integer[].class
+        "java.lang.Long[][]" | Long[][].class
+        "void" | void.class
+        "int" | int.class
+        "int[]" | int[].class
+        "float[][]" | float[][].class
+    }
+
     def "not object descriptor is an exception"() {
         when:
         PlasticInternalUtils.objectDescriptorToClassName("I")



Mime
View raw message