You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tapestry.apache.org by hl...@apache.org on 2007/01/05 03:09:48 UTC

svn commit: r492847 - in /tapestry/tapestry5/tapestry-ioc/trunk/src: main/java/org/apache/tapestry/ioc/internal/services/ main/java/org/apache/tapestry/ioc/services/ test/java/org/apache/tapestry/ioc/internal/services/

Author: hlship
Date: Thu Jan  4 18:09:48 2007
New Revision: 492847

URL: http://svn.apache.org/viewvc?view=rev&rev=492847
Log:
Hard-code in a meta default for void coercions: null to anything is allowed if a better match is not found.

Modified:
    tapestry/tapestry5/tapestry-ioc/trunk/src/main/java/org/apache/tapestry/ioc/internal/services/TypeCoercerImpl.java
    tapestry/tapestry5/tapestry-ioc/trunk/src/main/java/org/apache/tapestry/ioc/services/TapestryIOCModule.java
    tapestry/tapestry5/tapestry-ioc/trunk/src/test/java/org/apache/tapestry/ioc/internal/services/TypeCoercerImplTest.java

Modified: tapestry/tapestry5/tapestry-ioc/trunk/src/main/java/org/apache/tapestry/ioc/internal/services/TypeCoercerImpl.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/tapestry-ioc/trunk/src/main/java/org/apache/tapestry/ioc/internal/services/TypeCoercerImpl.java?view=diff&rev=492847&r1=492846&r2=492847
==============================================================================
--- tapestry/tapestry5/tapestry-ioc/trunk/src/main/java/org/apache/tapestry/ioc/internal/services/TypeCoercerImpl.java (original)
+++ tapestry/tapestry5/tapestry-ioc/trunk/src/main/java/org/apache/tapestry/ioc/internal/services/TypeCoercerImpl.java Thu Jan  4 18:09:48 2007
@@ -83,6 +83,14 @@
         }
     }
 
+    private static final Coercion COERCION_NULL_TO_OBJECT = new Coercion<Void, Object>()
+    {
+        public Object coerce(Void input)
+        {
+            return null;
+        }
+    };
+
     public TypeCoercerImpl(Collection<CoercionTuple> tuples)
     {
         for (CoercionTuple tuple : tuples)
@@ -215,6 +223,10 @@
             queueIntermediates(sourceType, tuple, consideredTuples, queue);
         }
 
+        // A default rule for coercing nulls if nothing better is found.
+
+        if (sourceType == void.class) return COERCION_NULL_TO_OBJECT;
+
         // Not found anywhere. Identify the source and target type and a (sorted) list of
         // all the known coercions.
 
@@ -237,10 +249,9 @@
         {
             for (CoercionTuple tuple : list)
             {
-                String description = String.format(
-                        "%s --> %s",
-                        tuple.getSourceType().getName(),
-                        tuple.getTargetType().getName());
+                String description = String.format("%s --> %s", ClassFabUtils
+                        .getJavaClassName(tuple.getSourceType()), ClassFabUtils
+                        .getJavaClassName(tuple.getTargetType()));
 
                 descriptions.add(description);
             }

Modified: tapestry/tapestry5/tapestry-ioc/trunk/src/main/java/org/apache/tapestry/ioc/services/TapestryIOCModule.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/tapestry-ioc/trunk/src/main/java/org/apache/tapestry/ioc/services/TapestryIOCModule.java?view=diff&rev=492847&r1=492846&r2=492847
==============================================================================
--- tapestry/tapestry5/tapestry-ioc/trunk/src/main/java/org/apache/tapestry/ioc/services/TapestryIOCModule.java (original)
+++ tapestry/tapestry5/tapestry-ioc/trunk/src/main/java/org/apache/tapestry/ioc/services/TapestryIOCModule.java Thu Jan  4 18:09:48 2007
@@ -217,10 +217,11 @@
      * <li>String to Boolean ("false" is always false, other non-blank strings are true)</li>
      * <li>Long to Boolean (true if long value is non zero)</li>
      * <li>Null to Boolean (always false)</li>
+     * <li>Null to String (still null)</li>
      * <li>Collection to Boolean (false if empty)</li>
      * <li>Object[] to List</li>
      * <li>Object to List (by wrapping as a singleton list)</li>
-     * <li>Null to String (still null)</li>
+     * <li>Null to List (still null)</li>
      * </ul>
      * 
      * @see #buildTypeCoercer(Collection, ComponentInstantiatorSource)
@@ -236,10 +237,23 @@
             }
         });
 
+        // This is necessary, otherwise we get a failure because void --> Object : Object --> String
+        // throws an NPE
+
         add(configuration, void.class, String.class, new Coercion<Void, String>()
         {
-
             public String coerce(Void input)
+            {
+                return null;
+            }
+        });
+
+        // This keeps a null -> List from being null -> Object : Object -> List (i.e., an empty List
+        // of a single null).
+
+        add(configuration, void.class, List.class, new Coercion<Void, List>()
+        {
+            public List coerce(Void input)
             {
                 return null;
             }

Modified: tapestry/tapestry5/tapestry-ioc/trunk/src/test/java/org/apache/tapestry/ioc/internal/services/TypeCoercerImplTest.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/tapestry-ioc/trunk/src/test/java/org/apache/tapestry/ioc/internal/services/TypeCoercerImplTest.java?view=diff&rev=492847&r1=492846&r2=492847
==============================================================================
--- tapestry/tapestry5/tapestry-ioc/trunk/src/test/java/org/apache/tapestry/ioc/internal/services/TypeCoercerImplTest.java (original)
+++ tapestry/tapestry5/tapestry-ioc/trunk/src/test/java/org/apache/tapestry/ioc/internal/services/TypeCoercerImplTest.java Thu Jan  4 18:09:48 2007
@@ -17,6 +17,7 @@
 import java.math.BigDecimal;
 import java.math.BigInteger;
 import java.util.Arrays;
+import java.util.Collection;
 import java.util.Collections;
 import java.util.List;
 import java.util.Map;
@@ -27,6 +28,7 @@
 import org.testng.annotations.BeforeClass;
 import org.testng.annotations.DataProvider;
 import org.testng.annotations.Test;
+import org.xml.sax.XMLReader;
 
 public class TypeCoercerImplTest extends IOCInternalTestCase
 {
@@ -148,12 +150,18 @@
                 { new BigInteger("12345678"), Long.class, 12345678l },
                 { -12345678l, BigInteger.class, new BigInteger("-12345678") },
                 { object, List.class, Collections.singletonList(object) },
+                { null, Iterable.class, null },
+                { null, List.class, null },
+                { null, Collection.class, null },
                 { null, String.class, null },
                 { new Object[]
                 { "a", 123 }, List.class, Arrays.asList("a", 123) },
                 { new String[]
                 { "a", "b" }, List.class, Arrays.asList("a", "b") },
 
-        };
+                // null to arbitrary object is still null
+
+                { null, XMLReader.class, null } };
     }
+
 }