Title: [waffle-scm] [710] trunk/waffle-core/src/main/java/org/codehaus/waffle/bind: WAFFLE-84: refactored value converters to support Java 5 Type.

Diff

Modified: trunk/examples/freemarker-example/src/main/webapp/WEB-INF/web.xml (709 => 710)

--- trunk/examples/freemarker-example/src/main/webapp/WEB-INF/web.xml	2008-06-14 22:43:07 UTC (rev 709)
+++ trunk/examples/freemarker-example/src/main/webapp/WEB-INF/web.xml	2008-06-16 17:35:31 UTC (rev 710)
@@ -19,10 +19,6 @@
     <param-name>register:ListValueConverter</param-name>
     <param-value>org.codehaus.waffle.bind.converters.ListValueConverter</param-value>
   </context-param>
-  <context-param>
-    <param-name>org.codehaus.waffle.monitor.BindMonitor</param-name>
-    <param-value>org.codehaus.waffle.monitor.ConsoleMonitor</param-value>
-  </context-param>
 
   <!-- Waffle context listener -->
   <listener>

Modified: trunk/waffle-core/src/main/java/org/codehaus/waffle/bind/DefaultStringTransmuter.java (709 => 710)

--- trunk/waffle-core/src/main/java/org/codehaus/waffle/bind/DefaultStringTransmuter.java	2008-06-14 22:43:07 UTC (rev 709)
+++ trunk/waffle-core/src/main/java/org/codehaus/waffle/bind/DefaultStringTransmuter.java	2008-06-16 17:35:31 UTC (rev 710)
@@ -27,7 +27,7 @@
         if (isEmpty(value) && toType.isPrimitive()) {
             value = null; // this allows Ognl to use that primitives default value
         }
-        return valueConverterFinder.findConverter(toType).convertValue(null, value, toType);
+        return (T) valueConverterFinder.findConverter(toType).convertValue(null, value, toType);
     }
 
     private boolean isEmpty(String value) {

Modified: trunk/waffle-core/src/main/java/org/codehaus/waffle/bind/StringTransmuter.java (709 => 710)

--- trunk/waffle-core/src/main/java/org/codehaus/waffle/bind/StringTransmuter.java	2008-06-14 22:43:07 UTC (rev 709)
+++ trunk/waffle-core/src/main/java/org/codehaus/waffle/bind/StringTransmuter.java	2008-06-16 17:35:31 UTC (rev 710)
@@ -25,5 +25,6 @@
      * @param toType the Object type
      * @return The converted Object
      */
+    //TODO use Type in place of Class<T> as for ValueConverter?
     <T> T transmute(String value, Class<T> toType);
 }

Modified: trunk/waffle-core/src/main/java/org/codehaus/waffle/bind/ValueConverter.java (709 => 710)

--- trunk/waffle-core/src/main/java/org/codehaus/waffle/bind/ValueConverter.java	2008-06-14 22:43:07 UTC (rev 709)
+++ trunk/waffle-core/src/main/java/org/codehaus/waffle/bind/ValueConverter.java	2008-06-16 17:35:31 UTC (rev 710)
@@ -10,6 +10,8 @@
  *****************************************************************************/
 package org.codehaus.waffle.bind;
 
+import java.lang.reflect.Type;
+
 /**
  * Implementation of this interface will be responsible for converting String values to the specific type.  These are
  * registered with Waffle through the [EMAIL PROTECTED] web.xml}.
@@ -22,10 +24,10 @@
     /**
      * Determines if converter is compatible with the given type
      * 
-     * @param type the type of the field a value is to be bound to
+     * @param type the Type a value is to be bound to
      * @return A boolean <code>true</code> is type is compatible
      */
-    boolean accept(Class<?> type);
+    boolean accept(Type type);
 
     /**
      * Converts a String value to an Object of a given type
@@ -33,10 +35,10 @@
      * @param propertyName the associated property name, which can be <code>null</code>, also needed to present
      *                     customized error messages.
      * @param value the String value
-     * @param toType  the Object type
+     * @param toType  the Object Type
      * @return The converted Object
      * @throws BindException if conversion fails
      */
-    <T> T convertValue(String propertyName, String value, Class<T> toType);
+    Object convertValue(String propertyName, String value, Type toType);
 
 }

Modified: trunk/waffle-core/src/main/java/org/codehaus/waffle/bind/ValueConverterFinder.java (709 => 710)

--- trunk/waffle-core/src/main/java/org/codehaus/waffle/bind/ValueConverterFinder.java	2008-06-14 22:43:07 UTC (rev 709)
+++ trunk/waffle-core/src/main/java/org/codehaus/waffle/bind/ValueConverterFinder.java	2008-06-16 17:35:31 UTC (rev 710)
@@ -10,6 +10,8 @@
  *****************************************************************************/
 package org.codehaus.waffle.bind;
 
+import java.lang.reflect.Type;
+
 /**
  * Finder interface for [EMAIL PROTECTED] ValueConverters} registered per application.
  *
@@ -20,9 +22,9 @@
     /**
      * Should return the [EMAIL PROTECTED] ValueConverter} that is responsible for handling the type passed in.
      *
-     * @param type representing the ValueConverter needed
+     * @param type the Type identifying the ValueConverter needed
      * @return the associated ValueConverter is returned or [EMAIL PROTECTED] null} if none was found. 
      */
-    ValueConverter findConverter(Class<?> type);
+    ValueConverter findConverter(Type type);
 
 }

Modified: trunk/waffle-core/src/main/java/org/codehaus/waffle/bind/converters/DateValueConverter.java (709 => 710)

--- trunk/waffle-core/src/main/java/org/codehaus/waffle/bind/converters/DateValueConverter.java	2008-06-14 22:43:07 UTC (rev 709)
+++ trunk/waffle-core/src/main/java/org/codehaus/waffle/bind/converters/DateValueConverter.java	2008-06-16 17:35:31 UTC (rev 710)
@@ -10,13 +10,14 @@
  *****************************************************************************/
 package org.codehaus.waffle.bind.converters;
 
-import org.codehaus.waffle.i18n.MessageResources;
-
+import java.lang.reflect.Type;
 import java.text.ParseException;
 import java.text.SimpleDateFormat;
 import java.util.Date;
 import java.util.Properties;
 
+import org.codehaus.waffle.i18n.MessageResources;
+
 /**
  * <code>ValueConverter</code> that converts Date values. The date format is configurable via the message resources
  * bundle. A <code>null</code> or empty value (once trimmed) will be returned as <code>null</code> (behaviour which
@@ -71,21 +72,24 @@
         this.patterns = patterns;
     }
 
-    public boolean accept(Class<?> type) {
-        return Date.class.isAssignableFrom(type);
+    public boolean accept(Type type) {
+        if ( type instanceof Class ){
+            return Date.class.isAssignableFrom((Class<?>)type);            
+        }
+        return false;
     }
 
     @SuppressWarnings( { "unchecked" })
-    public <T> T convertValue(String propertyName, String value, Class<T> toType) {
+    public Object convertValue(String propertyName, String value, Type toType) {
         String fieldName = messageFor(propertyName, propertyName);
         if (missingValue(value)) {
-            return (T) convertMissingValue(BIND_ERROR_DATE_MISSING_KEY, DEFAULT_DATE_MISSING_MESSAGE, fieldName);
+            return convertMissingValue(BIND_ERROR_DATE_MISSING_KEY, DEFAULT_DATE_MISSING_MESSAGE, fieldName);
         }
 
         SimpleDateFormat dateFormat = dateFormatFor(propertyName);
 
         try {
-            return (T) dateFormat.parse(value);
+            return dateFormat.parse(value);
         } catch (ParseException e) {
             throw newBindException(BIND_ERROR_DATE_KEY, DEFAULT_DATE_MESSAGE, fieldName, value, dateFormat.toPattern());
         }

Modified: trunk/waffle-core/src/main/java/org/codehaus/waffle/bind/converters/ListValueConverter.java (709 => 710)

--- trunk/waffle-core/src/main/java/org/codehaus/waffle/bind/converters/ListValueConverter.java	2008-06-14 22:43:07 UTC (rev 709)
+++ trunk/waffle-core/src/main/java/org/codehaus/waffle/bind/converters/ListValueConverter.java	2008-06-16 17:35:31 UTC (rev 710)
@@ -10,14 +10,16 @@
  *****************************************************************************/
 package org.codehaus.waffle.bind.converters;
 
-import org.codehaus.waffle.i18n.MessageResources;
-
+import java.lang.reflect.ParameterizedType;
+import java.lang.reflect.Type;
 import java.text.NumberFormat;
 import java.text.ParseException;
 import java.util.ArrayList;
 import java.util.List;
 import java.util.Properties;
 
+import org.codehaus.waffle.i18n.MessageResources;
+
 /**
  * <p>
  * <code>ValueConverter</code> that converts a CSV value to a List. A <code>null</code> or empty value (once
@@ -63,27 +65,33 @@
         this.patterns = patterns;
     }
 
-    public boolean accept(Class<?> type) {
-        return List.class.isAssignableFrom(type);
+    public boolean accept(Type type) {
+        if ( type instanceof Class ){
+            return List.class.isAssignableFrom((Class<?>)type);            
+        } else if ( type instanceof ParameterizedType ){            
+            Type rawType = ((ParameterizedType)type).getRawType();
+            return List.class.isAssignableFrom((Class<?>)rawType);
+        }
+        return false;
     }
 
     @SuppressWarnings( { "unchecked" })
-    public <T> T convertValue(String propertyName, String value, Class<T> toType) {
+    public Object convertValue(String propertyName, String value, Type toType) {
 
         if (missingValue(value)) {
             String fieldName = messageFor(propertyName, propertyName);
-            return (T) convertMissingValue(BIND_ERROR_LIST_KEY, DEFAULT_LIST_MESSAGE, fieldName);
+            return convertMissingValue(BIND_ERROR_LIST_KEY, DEFAULT_LIST_MESSAGE, fieldName);
         }
 
         List<String> values = listValues(value);
         if (areNumbers(values)) {
             try {
-                return (T) toNumbers(values);
+                return toNumbers(values);
             } catch (ParseException e) {
                 // failed to parse as numbers, return string values
             }
         }
-        return (T) values;
+        return values;
     }
 
     private List<String> listValues(String value) {

Modified: trunk/waffle-core/src/main/java/org/codehaus/waffle/bind/ognl/DelegatingTypeConverter.java (709 => 710)

--- trunk/waffle-core/src/main/java/org/codehaus/waffle/bind/ognl/DelegatingTypeConverter.java	2008-06-14 22:43:07 UTC (rev 709)
+++ trunk/waffle-core/src/main/java/org/codehaus/waffle/bind/ognl/DelegatingTypeConverter.java	2008-06-16 17:35:31 UTC (rev 710)
@@ -11,14 +11,16 @@
 package org.codehaus.waffle.bind.ognl;
 
 import java.lang.reflect.Member;
+import java.lang.reflect.Method;
+import java.lang.reflect.Type;
 import java.util.Map;
 
+import ognl.OgnlOps;
+import ognl.TypeConverter;
+
 import org.codehaus.waffle.bind.ValueConverter;
 import org.codehaus.waffle.bind.ValueConverterFinder;
 
-import ognl.OgnlOps;
-import ognl.TypeConverter;
-
 /**
  * An implementation of Ognl's <code>TypeConverter</code> which handles Java 5 enums and will delegate
  * custom <code>ValueConverter</code>'s registered per application.
@@ -61,9 +63,17 @@
                                String propertyName,
                                Object value,
                                Class toType) {
-        return convertValue(propertyName, (String) value, toType);
+        return convertValue(propertyName, (String) value, genericParameterTypeFor((Method)member));
     }
 
+    private Type genericParameterTypeFor(Method method) {
+        Type[] parameterTypes = method.getGenericParameterTypes();
+        if ( parameterTypes.length > 0 ){
+            return parameterTypes[0];
+        }
+        return null;
+    }
+
     /**
      * Simplified entry point for Ognl use in Waffle
      *
@@ -74,12 +84,12 @@
      *         conversion was not possible.
      */
     @SuppressWarnings({"unchecked"})
-    public Object convertValue(String propertyName, String value, Class toType) {
-        if (toType.isEnum()) {
-            if (EMPTY.equals(value)) {
+    public Object convertValue(String propertyName, String value, Type toType) {
+        if (toType instanceof Class && ((Class)toType).isEnum()) {
+            if (EMPTY.equals(value)) {                
                 return null;
             }
-            return Enum.valueOf(toType, value);
+            return Enum.valueOf((Class)toType, value);
         }
 
         ValueConverter converter = valueConverterFinder.findConverter(toType);
@@ -88,7 +98,7 @@
             return converter.convertValue(propertyName, value, toType);
         }
 
-        return OgnlOps.convertValue(value, toType);
+        return OgnlOps.convertValue(value, (Class)toType);
     }
 
 }

Modified: trunk/waffle-core/src/main/java/org/codehaus/waffle/bind/ognl/OgnlValueConverter.java (709 => 710)

--- trunk/waffle-core/src/main/java/org/codehaus/waffle/bind/ognl/OgnlValueConverter.java	2008-06-14 22:43:07 UTC (rev 709)
+++ trunk/waffle-core/src/main/java/org/codehaus/waffle/bind/ognl/OgnlValueConverter.java	2008-06-16 17:35:31 UTC (rev 710)
@@ -10,11 +10,13 @@
  *****************************************************************************/
 package org.codehaus.waffle.bind.ognl;
 
-import org.codehaus.waffle.bind.ValueConverter;
+import java.lang.reflect.Type;
 
 import ognl.DefaultTypeConverter;
 import ognl.TypeConverter;
 
+import org.codehaus.waffle.bind.ValueConverter;
+
 /**
  * Ognl-based implementation of <code>ValueConverter</code>.
  *
@@ -32,13 +34,13 @@
         this.converter = converter;
     }
 
-    public boolean accept(Class<?> type) {
-        return true;
+    public boolean accept(Type type) {
+        return type instanceof Class;
     }
 
     @SuppressWarnings({"unchecked"})
-    public <T> T convertValue(String propertyName, String value, Class<T> toType) {
-        return (T) converter.convertValue(null, null, null, propertyName, value, toType);
+    public Object convertValue(String propertyName, String value, Type toType) {
+        return converter.convertValue(null, null, null, propertyName, value, (Class)toType);
     }
 
 }

Modified: trunk/waffle-core/src/main/java/org/codehaus/waffle/bind/ognl/OgnlValueConverterFinder.java (709 => 710)

--- trunk/waffle-core/src/main/java/org/codehaus/waffle/bind/ognl/OgnlValueConverterFinder.java	2008-06-14 22:43:07 UTC (rev 709)
+++ trunk/waffle-core/src/main/java/org/codehaus/waffle/bind/ognl/OgnlValueConverterFinder.java	2008-06-16 17:35:31 UTC (rev 710)
@@ -12,6 +12,7 @@
 
 import static java.util.Arrays.asList;
 
+import java.lang.reflect.Type;
 import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.List;
@@ -34,7 +35,7 @@
 
     private static final ValueConverter OGNL_VALUE_CONVERTER = new OgnlValueConverter();
     private final List<ValueConverter> DEFAULT_CONVERTERS = asList(OGNL_VALUE_CONVERTER);
-    private final Map<Class<?>, ValueConverter> cache = new HashMap<Class<?>, ValueConverter>();
+    private final Map<Type, ValueConverter> cache = new HashMap<Type, ValueConverter>();
     private final List<ValueConverter> converters;
 
     public OgnlValueConverterFinder() {
@@ -51,7 +52,7 @@
         }
     }
 
-    public ValueConverter findConverter(Class<?> type) {
+    public ValueConverter findConverter(Type type) {
         if (cache.containsKey(type)) { // cache hit
             return cache.get(type);
         }

Modified: trunk/waffle-core/src/test/java/org/codehaus/waffle/bind/converters/DateValueConverterTest.java (709 => 710)

--- trunk/waffle-core/src/test/java/org/codehaus/waffle/bind/converters/DateValueConverterTest.java	2008-06-14 22:43:07 UTC (rev 709)
+++ trunk/waffle-core/src/test/java/org/codehaus/waffle/bind/converters/DateValueConverterTest.java	2008-06-16 17:35:31 UTC (rev 710)
@@ -94,8 +94,8 @@
         assertDateFormattable("11:11:11", "HH:mm:ss", converter.convertValue("time-property", "11:11:11", Date.class));
     }
 
-    private void assertDateFormattable(String value, String pattern, Date date) {
-        assertEquals(value, new SimpleDateFormat(pattern).format(date));
+    private void assertDateFormattable(String value, String pattern, Object object) {
+        assertEquals(value, new SimpleDateFormat(pattern).format(object));
     }
 
     @Test

Modified: trunk/waffle-core/src/test/java/org/codehaus/waffle/bind/converters/ListValueConverterTest.java (709 => 710)

--- trunk/waffle-core/src/test/java/org/codehaus/waffle/bind/converters/ListValueConverterTest.java	2008-06-14 22:43:07 UTC (rev 709)
+++ trunk/waffle-core/src/test/java/org/codehaus/waffle/bind/converters/ListValueConverterTest.java	2008-06-16 17:35:31 UTC (rev 710)
@@ -75,7 +75,7 @@
      }
 
     private void assertEmptyList(ListValueConverter converter, String value) {
-        List<?> list = converter.convertValue("property-name", value, List.class);
+        List<?> list = (List<?>) converter.convertValue("property-name", value, List.class);
         assertNotNull(list);
         assertTrue(list.isEmpty());
     }


To unsubscribe from this list please visit:

http://xircles.codehaus.org/manage_email

Reply via email to