Title: [waffle-scm] [711] trunk/waffle-core/src/main/java/org/codehaus/waffle/bind: WAFFLE-84: refactored method definition finder and string transmuter to support Java 5 generic types.

Diff

Modified: trunk/waffle-core/src/main/java/org/codehaus/waffle/action/AbstractMethodDefinitionFinder.java (710 => 711)

--- trunk/waffle-core/src/main/java/org/codehaus/waffle/action/AbstractMethodDefinitionFinder.java	2008-06-16 17:35:31 UTC (rev 710)
+++ trunk/waffle-core/src/main/java/org/codehaus/waffle/action/AbstractMethodDefinitionFinder.java	2008-06-16 18:46:58 UTC (rev 711)
@@ -14,6 +14,8 @@
 
 import java.lang.reflect.Method;
 import java.lang.reflect.Modifier;
+import java.lang.reflect.ParameterizedType;
+import java.lang.reflect.Type;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.HashMap;
@@ -244,7 +246,7 @@
     }
 
     private boolean hasEquivalentParameterTypes(MethodDefinition methodDefinition) {
-        Class<?>[] methodParameterTypes = methodDefinition.getMethod().getParameterTypes();
+        Type[] methodParameterTypes = methodDefinition.getMethod().getGenericParameterTypes();
         List<Object> methodArguments = methodDefinition.getMethodArguments();
 
         if (methodParameterTypes.length != methodArguments.size()) {
@@ -252,13 +254,13 @@
         }
 
         for (int i = 0; i < methodParameterTypes.length; i++) {
-            Class<?> methodParameterType = methodParameterTypes[i];
+            Type methodParameterType = methodParameterTypes[i];
 
             // the types must be assignable to be considered a valid method (assume true if actualParameterType is null)
             if (methodArguments.get(i) != null) {
                 Class<?> type = methodArguments.get(i).getClass();
 
-                if (!methodParameterType.isAssignableFrom(type)) {
+                if (!isAssignableFrom(methodParameterType, type)) {
                     if (String.class.equals(type)) {
                         try {
                             // Can the String be converted to the parameter type? If so convert it...
@@ -277,6 +279,16 @@
         return true;
     }
 
+    private boolean isAssignableFrom(Type methodParameterType, Class<?> type) {
+        if ( methodParameterType instanceof Class ){
+            return ((Class<?>)methodParameterType).isAssignableFrom(type);
+        } else if ( methodParameterType instanceof ParameterizedType ){            
+            Type rawType = ((ParameterizedType)methodParameterType).getRawType();
+            return ((Class<?>)rawType).isAssignableFrom(type);
+        }
+        return false;
+    }
+
     // Protected methods, accessible by  subclasses
 
     /**

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

--- trunk/waffle-core/src/main/java/org/codehaus/waffle/bind/DefaultStringTransmuter.java	2008-06-16 17:35:31 UTC (rev 710)
+++ trunk/waffle-core/src/main/java/org/codehaus/waffle/bind/DefaultStringTransmuter.java	2008-06-16 18:46:58 UTC (rev 711)
@@ -10,6 +10,8 @@
  *****************************************************************************/
 package org.codehaus.waffle.bind;
 
+import java.lang.reflect.Type;
+
 /**
  * This implementation uses the [EMAIL PROTECTED] org.codehaus.waffle.bind.ValueConverterFinder} and its resulting
  * [EMAIL PROTECTED] ValueConverter} to transform a String value into the specified type.
@@ -23,13 +25,20 @@
         this.valueConverterFinder = valueConverterFinder;
     }
 
-    public <T> T transmute(String value, Class<T> toType) {
-        if (isEmpty(value) && toType.isPrimitive()) {
+    public Object transmute(String value, Type toType) {
+        if (isEmpty(value) && isPrimitive(toType)) {
             value = null; // this allows Ognl to use that primitives default value
         }
-        return (T) valueConverterFinder.findConverter(toType).convertValue(null, value, toType);
+        return valueConverterFinder.findConverter(toType).convertValue(null, value, toType);
     }
 
+    private boolean isPrimitive(Type toType) {
+        if ( toType instanceof Class ){
+            return ((Class<?>)toType).isPrimitive();
+        }
+        return false;
+    }
+
     private boolean isEmpty(String value) {
         return value == null || value.length() == 0;
     }

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

--- trunk/waffle-core/src/main/java/org/codehaus/waffle/bind/StringTransmuter.java	2008-06-16 17:35:31 UTC (rev 710)
+++ trunk/waffle-core/src/main/java/org/codehaus/waffle/bind/StringTransmuter.java	2008-06-16 18:46:58 UTC (rev 711)
@@ -10,6 +10,8 @@
  *****************************************************************************/
 package org.codehaus.waffle.bind;
 
+import java.lang.reflect.Type;
+
 /**
  * Not to be confused with the [EMAIL PROTECTED] ValueConverter} this interface is used to simplify converting (transmuting) a
  * String value into a given type.
@@ -22,9 +24,8 @@
      * Convert (transmute) the string value into the Type requested
      *
      * @param value the String value
-     * @param toType the Object type
+     * @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);
+    Object transmute(String value, Type toType);
 }

Modified: trunk/waffle-core/src/test/java/org/codehaus/waffle/action/AnnotatedMethodDefinitionFinderTest.java (710 => 711)

--- trunk/waffle-core/src/test/java/org/codehaus/waffle/action/AnnotatedMethodDefinitionFinderTest.java	2008-06-16 17:35:31 UTC (rev 710)
+++ trunk/waffle-core/src/test/java/org/codehaus/waffle/action/AnnotatedMethodDefinitionFinderTest.java	2008-06-16 18:46:58 UTC (rev 711)
@@ -39,7 +39,7 @@
 
     private ActionMonitor monitor = new SilentMonitor();
 
-    public void testDefaultMethodReturned() throws NoSuchMethodException {
+    public void canReturnDefaultMethod() throws NoSuchMethodException {
         // Mock HttpServletRequest
         final HttpServletRequest request = mockery.mock(HttpServletRequest.class);
 
@@ -68,7 +68,7 @@
     }
 
     @Test
-    public void canDefaultActionMethodWithArgumentReturned() throws NoSuchMethodException {
+    public void canReturnDefaultActionMethodWithArgument() throws NoSuchMethodException {
         // Mock HttpServletRequest
         final HttpServletRequest request = mockery.mock(HttpServletRequest.class);
 

Modified: trunk/waffle-core/src/test/java/org/codehaus/waffle/testmodel/StubStringTransmuter.java (710 => 711)

--- trunk/waffle-core/src/test/java/org/codehaus/waffle/testmodel/StubStringTransmuter.java	2008-06-16 17:35:31 UTC (rev 710)
+++ trunk/waffle-core/src/test/java/org/codehaus/waffle/testmodel/StubStringTransmuter.java	2008-06-16 18:46:58 UTC (rev 711)
@@ -1,9 +1,11 @@
 package org.codehaus.waffle.testmodel;
 
+import java.lang.reflect.Type;
+
 import org.codehaus.waffle.bind.StringTransmuter;
 
 public class StubStringTransmuter implements StringTransmuter{
-    public <T> T transmute(String value, Class<T> toType) {
+    public Object transmute(String value, Type toType) {
         return null;
     }
 }


To unsubscribe from this list please visit:

http://xircles.codehaus.org/manage_email

Reply via email to