You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tapestry.apache.org by hl...@apache.org on 2011/04/14 00:23:39 UTC

svn commit: r1091951 - in /tapestry/tapestry5/trunk/plastic/src: main/java/org/apache/tapestry5/internal/plastic/ main/java/org/apache/tapestry5/plastic/ test/groovy/org/apache/tapestry5/internal/plastic/

Author: hlship
Date: Wed Apr 13 22:23:39 2011
New Revision: 1091951

URL: http://svn.apache.org/viewvc?rev=1091951&view=rev
Log:
TAP5-853: Add utility method for converting from primitive type to wrapper type

Modified:
    tapestry/tapestry5/trunk/plastic/src/main/java/org/apache/tapestry5/internal/plastic/PrimitiveType.java
    tapestry/tapestry5/trunk/plastic/src/main/java/org/apache/tapestry5/plastic/PlasticUtils.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/PrimitiveType.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/plastic/src/main/java/org/apache/tapestry5/internal/plastic/PrimitiveType.java?rev=1091951&r1=1091950&r2=1091951&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/plastic/src/main/java/org/apache/tapestry5/internal/plastic/PrimitiveType.java (original)
+++ tapestry/tapestry5/trunk/plastic/src/main/java/org/apache/tapestry5/internal/plastic/PrimitiveType.java Wed Apr 13 22:23:39 2011
@@ -27,29 +27,31 @@ import org.apache.tapestry5.internal.pla
 @SuppressWarnings("rawtypes")
 public enum PrimitiveType implements Opcodes
 {
-    VOID("void", "V", null, null, null, ILOAD, ISTORE, RETURN),
+    VOID("void", "V", void.class, Void.class, null, null, ILOAD, ISTORE, RETURN),
 
-    BOOLEAN("boolean", "Z", Boolean.class, "booleanValue", "getBoolean", ILOAD, ISTORE, IRETURN),
+    BOOLEAN("boolean", "Z", boolean.class, Boolean.class, "booleanValue", "getBoolean", ILOAD, ISTORE, IRETURN),
 
-    CHAR("char", "C", Character.class, "charValue", "getChar", ILOAD, ISTORE, IRETURN),
+    CHAR("char", "C", char.class, Character.class, "charValue", "getChar", ILOAD, ISTORE, IRETURN),
 
-    BYTE("byte", "B", Byte.class, "byteValue", "getByte", ILOAD, ISTORE, IRETURN),
+    BYTE("byte", "B", byte.class, Byte.class, "byteValue", "getByte", ILOAD, ISTORE, IRETURN),
 
-    SHORT("short", "S", Short.class, "shortValue", "getShort", ILOAD, ISTORE, IRETURN),
+    SHORT("short", "S", short.class, Short.class, "shortValue", "getShort", ILOAD, ISTORE, IRETURN),
 
-    INT("int", "I", Integer.class, "intValue", "getInt", ILOAD, ISTORE, IRETURN),
+    INT("int", "I", int.class, Integer.class, "intValue", "getInt", ILOAD, ISTORE, IRETURN),
 
-    FLOAT("float", "F", Float.class, "floatValue", "getFloat", FLOAD, FSTORE, FRETURN),
+    FLOAT("float", "F", float.class, Float.class, "floatValue", "getFloat", FLOAD, FSTORE, FRETURN),
 
-    LONG("long", "J", Long.class, "longValue", "getLong", LLOAD, LSTORE, LRETURN),
+    LONG("long", "J", long.class, Long.class, "longValue", "getLong", LLOAD, LSTORE, LRETURN),
 
-    DOUBLE("double", "D", Double.class, "doubleValue", "getDouble", DLOAD, DSTORE, DRETURN);
+    DOUBLE("double", "D", double.class, Double.class, "doubleValue", "getDouble", DLOAD, DSTORE, DRETURN);
 
     /**
      * @param name
      *            the Java source name for the type
      * @param descriptor
      *            Java descriptor for the type ('Z', 'I', etc.)
+     * @param primitiveType
+     *            TODO
      * @param wrapperType
      *            wrapper type, e.g., java.lang.Integer
      * @param toValueMethodName
@@ -65,11 +67,13 @@ public enum PrimitiveType implements Opc
      *            Correct opcode for returning the top value on the stack (IRETURN, LRETURN, FRETURN
      *            or DRETURN)
      */
-    private PrimitiveType(String name, String descriptor, Class wrapperType, String toValueMethodName,
-            String getFromStaticContextMethodName, int loadOpcode, int storeOpcode, int returnOpcode)
+    private PrimitiveType(String name, String descriptor, Class primitiveType, Class wrapperType,
+            String toValueMethodName, String getFromStaticContextMethodName, int loadOpcode, int storeOpcode,
+            int returnOpcode)
     {
         this.name = name;
         this.descriptor = descriptor;
+        this.primitiveType = primitiveType;
         this.wrapperType = wrapperType;
         this.wrapperInternalName = wrapperType == null ? null : PlasticInternalUtils.toInternalName(wrapperType
                 .getName());
@@ -87,12 +91,13 @@ public enum PrimitiveType implements Opc
     public final String name, descriptor, wrapperInternalName, valueOfMethodDescriptor, toValueMethodName,
             getFromStaticContextMethodName, toValueMethodDescriptor, getFromStaticContextMethodDescriptor;
 
-    public final Class wrapperType;
+    public final Class primitiveType, wrapperType;
 
     public final int loadOpcode, storeOpcode, returnOpcode;
 
     private static final Map<String, PrimitiveType> BY_NAME = new HashMap<String, PrimitiveType>();
     private static final Map<String, PrimitiveType> BY_DESC = new HashMap<String, PrimitiveType>();
+    private static final Map<Class, PrimitiveType> BY_PRIMITIVE_TYPE = new HashMap<Class, PrimitiveType>();
 
     static
     {
@@ -100,6 +105,7 @@ public enum PrimitiveType implements Opc
         {
             BY_NAME.put(type.name, type);
             BY_DESC.put(type.descriptor, type);
+            BY_PRIMITIVE_TYPE.put(type.primitiveType, type);
         }
     }
 
@@ -120,4 +126,9 @@ public enum PrimitiveType implements Opc
     {
         return BY_NAME.get(name);
     }
+
+    public static PrimitiveType getByPrimitiveType(Class primitiveType)
+    {
+        return BY_PRIMITIVE_TYPE.get(primitiveType);
+    }
 }

Modified: tapestry/tapestry5/trunk/plastic/src/main/java/org/apache/tapestry5/plastic/PlasticUtils.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/plastic/src/main/java/org/apache/tapestry5/plastic/PlasticUtils.java?rev=1091951&r1=1091950&r2=1091951&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/plastic/src/main/java/org/apache/tapestry5/plastic/PlasticUtils.java (original)
+++ tapestry/tapestry5/trunk/plastic/src/main/java/org/apache/tapestry5/plastic/PlasticUtils.java Wed Apr 13 22:23:39 2011
@@ -16,6 +16,8 @@ package org.apache.tapestry5.plastic;
 
 import java.util.concurrent.atomic.AtomicLong;
 
+import org.apache.tapestry5.internal.plastic.PrimitiveType;
+
 /**
  * Utilities for user code making use of Plastic.
  */
@@ -46,7 +48,7 @@ public class PlasticUtils
     {
         if (type.isArray())
             return toTypeName(type.getComponentType()) + "[]";
-    
+
         return type.getName();
     }
 
@@ -54,10 +56,25 @@ public class PlasticUtils
     public static String[] toTypeNames(Class[] types)
     {
         String[] result = new String[types.length];
-    
+
         for (int i = 0; i < result.length; i++)
             result[i] = toTypeName(types[i]);
-    
+
         return result;
     }
+
+    /**
+     * Gets the wrapper type for a given type (if primitive)
+     * 
+     * @param type
+     *            type to look up
+     * @return the input type for non-primitive type, or corresponding wrapper type (Boolean.class for boolean.class,
+     *         etc.)
+     */
+    public static Class toWrapperType(Class type)
+    {
+        assert type != null;
+
+        return type.isPrimitive() ? PrimitiveType.getByPrimitiveType(type).wrapperType : type;
+    }
 }

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=1091951&r1=1091950&r2=1091951&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 13 22:23:39 2011
@@ -15,6 +15,7 @@
 package org.apache.tapestry5.internal.plastic
 
 import org.apache.tapestry5.internal.plastic.PlasticInternalUtils;
+import org.apache.tapestry5.plastic.PlasticUtils;
 
 import spock.lang.Specification
 import spock.lang.Unroll
@@ -126,4 +127,19 @@ class PlasticUtilsTests extends Specific
 
         "goodbye" | "Goodbye"
     }
+
+    @Unroll("toWrapperType #primitiveType should be #wrapperType")
+    def "primitive type to wrapper type"() {
+        expect:
+
+        PlasticUtils.toWrapperType (primitiveType) == wrapperType
+        where:
+
+        primitiveType | wrapperType
+
+        String.class | String.class
+        boolean.class | Boolean.class
+        double.class | Double.class
+        int[].class  | int[].class
+    }
 }