Author: hlship
Date: Fri Jul  8 00:52:40 2011
New Revision: 1144110

URL: http://svn.apache.org/viewvc?rev=1144110&view=rev
Log:
TAP5-1227: Binding a parameter whose type is primitive to null results in a 
difficult to understand NullPointerException

Added:
    
tapestry/tapestry5/trunk/tapestry-core/src/test/groovy/org/apache/tapestry5/integration/app1/ParameterTests.groovy
    
tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/components/ShowInt.java
    
tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/pages/NullBindingToPrimitive.java
    
tapestry/tapestry5/trunk/tapestry-core/src/test/resources/org/apache/tapestry5/integration/app1/pages/NullBindingToPrimitive.tml
Modified:
    
tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/transform/ParameterWorker.java
    
tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/pages/Index.java

Modified: 
tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/transform/ParameterWorker.java
URL: 
http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/transform/ParameterWorker.java?rev=1144110&r1=1144109&r2=1144110&view=diff
==============================================================================
--- 
tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/transform/ParameterWorker.java
 (original)
+++ 
tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/transform/ParameterWorker.java
 Fri Jul  8 00:52:40 2011
@@ -160,6 +160,10 @@ public class ParameterWorker implements 
                                                                                
final String fieldTypeName, final Parameter annotation,
                                                                                
final MethodHandle defaultMethodHandle)
     {
+        boolean primitive = PlasticUtils.isPrimitive(fieldTypeName);
+
+        final boolean allowNull = annotation.allowNull() && !primitive;
+
         return new ComputedValue<FieldConduit<Object>>()
         {
             public ParameterConduit get(InstanceContext context)
@@ -257,15 +261,15 @@ public class ParameterWorker implements 
                                     icr.getCompleteId(), 
InternalUtils.toMessage(ex)), parameterBinding, ex);
                         }
 
-                        if (result != null || annotation.allowNull())
+                        if (result == null && !allowNull)
                         {
-                            return result;
+                            throw new TapestryException(
+                                    String.format(
+                                            "Parameter '%s' of component %s is 
bound to null. This parameter is not allowed to be null.",
+                                            parameterName, 
icr.getCompleteId()), parameterBinding, null);
                         }
 
-                        throw new TapestryException(
-                                String.format(
-                                        "Parameter '%s' of component %s is 
bound to null. This parameter is not allowed to be null.",
-                                        parameterName, icr.getCompleteId()), 
parameterBinding, null);
+                        return result;
                     }
 
                     private void writeToBinding(Object newValue)

Added: 
tapestry/tapestry5/trunk/tapestry-core/src/test/groovy/org/apache/tapestry5/integration/app1/ParameterTests.groovy
URL: 
http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/test/groovy/org/apache/tapestry5/integration/app1/ParameterTests.groovy?rev=1144110&view=auto
==============================================================================
--- 
tapestry/tapestry5/trunk/tapestry-core/src/test/groovy/org/apache/tapestry5/integration/app1/ParameterTests.groovy
 (added)
+++ 
tapestry/tapestry5/trunk/tapestry-core/src/test/groovy/org/apache/tapestry5/integration/app1/ParameterTests.groovy
 Fri Jul  8 00:52:40 2011
@@ -0,0 +1,36 @@
+// Copyright 2011 The Apache Software Foundation
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package org.apache.tapestry5.integration.app1
+
+import org.apache.tapestry5.integration.TapestryCoreTestCase
+import org.testng.annotations.Test
+
+/**
+ * @since 5.3
+ */
+class ParameterTests extends TapestryCoreTestCase {
+
+    /**
+     * https://issues.apache.org/jira/browse/TAP5-1227
+     */
+
+    @Test
+    void null_bound_to_primitive_field_is_an_error() {
+        openLinks "Null Bound to Primitive Demo"
+
+        assertTextPresent "Parameter 'value' of component 
NullBindingToPrimitive:showint is bound to null. This parameter is not allowed 
to be null."
+    }
+
+}

Added: 
tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/components/ShowInt.java
URL: 
http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/components/ShowInt.java?rev=1144110&view=auto
==============================================================================
--- 
tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/components/ShowInt.java
 (added)
+++ 
tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/components/ShowInt.java
 Fri Jul  8 00:52:40 2011
@@ -0,0 +1,35 @@
+// Copyright 2011 The Apache Software Foundation
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package org.apache.tapestry5.integration.app1.components;
+
+import org.apache.tapestry5.MarkupWriter;
+import org.apache.tapestry5.annotations.Parameter;
+
+/**
+ * @since 5.3
+ */
+public class ShowInt
+{
+    @Parameter
+    private int value;
+
+    void beginRender(MarkupWriter writer)
+    {
+        writer.element("strong");
+        writer.write(String.valueOf(value));
+        writer.end();
+    }
+
+}

Modified: 
tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/pages/Index.java
URL: 
http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/pages/Index.java?rev=1144110&r1=1144109&r2=1144110&view=diff
==============================================================================
--- 
tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/pages/Index.java
 (original)
+++ 
tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/pages/Index.java
 Fri Jul  8 00:52:40 2011
@@ -14,10 +14,6 @@
 
 package org.apache.tapestry5.integration.app1.pages;
 
-import java.util.Arrays;
-import java.util.Collections;
-import java.util.List;
-
 import org.apache.tapestry5.ComponentResources;
 import org.apache.tapestry5.Link;
 import org.apache.tapestry5.PersistenceConstants;
@@ -27,6 +23,10 @@ import org.apache.tapestry5.annotations.
 import org.apache.tapestry5.ioc.annotations.Inject;
 import org.apache.tapestry5.ioc.internal.util.CollectionFactory;
 
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.List;
+
 /**
  * Have to start somewhere!
  */
@@ -57,6 +57,8 @@ public class Index
     private static final List<Item> ITEMS = CollectionFactory
             .newList(
 
+                    new Item("NullBindingToPrimitive", "Null Bound to 
Primitive Demo", "Correct exception when a primitive parameter is bound to 
null"),
+
                     new Item("TreeDemo", "Tree Component Demo", "Demo of Tree 
Component"),
 
                     new Item("InvalidExpressionInDynamicTemplate", "Invalid 
Dynamic Expression",
@@ -86,7 +88,7 @@ public class Index
                     new Item("ZoneFormUpdateDemo", "Zone/Form Update Demo", 
"Updating a Zone inside a Form"),
 
                     new Item("MultiZoneStringBodyDemo", "MultiZone String Body 
Demo",
-                                               "Multi-zone updates in a loop 
using strings coerced into blocks"),
+                            "Multi-zone updates in a loop using strings 
coerced into blocks"),
 
                     new Item("RenderNotificationDemo", "RenderNotification 
Demo", "Use of RenderNotification mixin"),
 
@@ -472,13 +474,13 @@ public class Index
                     new Item("AtInjectDemo", "@javax.inject.Inject Demo", 
"Using @javax.inject.Inject for injection"),
 
                     new Item("LinkQueryParameters", "Link Query Parameters 
Demo",
-                            "Providing Query Parameters directly to link 
components as a map of key=parameter name, value=parameter values") ,
+                            "Providing Query Parameters directly to link 
components as a map of key=parameter name, value=parameter values"),
 
-                    new Item("ChecklistDemo", "Checklist Demo", "Use Checklist 
component")            ,
+                    new Item("ChecklistDemo", "Checklist Demo", "Use Checklist 
component"),
 
                     new Item("BeanEditFormPrepareBubbling", "BeanEditor 
Prepare Bubbling Demo", "Prepare event bubbling"),
-                                       
-                                       new Item("NestedFormFragment","Nested 
Form Fragment Demo","Nesting Form Fragments work properly")
+
+                    new Item("NestedFormFragment", "Nested Form Fragment 
Demo", "Nesting Form Fragments work properly")
 
             );
 

Added: 
tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/pages/NullBindingToPrimitive.java
URL: 
http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/pages/NullBindingToPrimitive.java?rev=1144110&view=auto
==============================================================================
--- 
tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/pages/NullBindingToPrimitive.java
 (added)
+++ 
tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/pages/NullBindingToPrimitive.java
 Fri Jul  8 00:52:40 2011
@@ -0,0 +1,22 @@
+// Copyright 2011 The Apache Software Foundation
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package org.apache.tapestry5.integration.app1.pages;
+
+/**
+ * @since 5.3
+ */
+public class NullBindingToPrimitive
+{
+}

Added: 
tapestry/tapestry5/trunk/tapestry-core/src/test/resources/org/apache/tapestry5/integration/app1/pages/NullBindingToPrimitive.tml
URL: 
http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/test/resources/org/apache/tapestry5/integration/app1/pages/NullBindingToPrimitive.tml?rev=1144110&view=auto
==============================================================================
--- 
tapestry/tapestry5/trunk/tapestry-core/src/test/resources/org/apache/tapestry5/integration/app1/pages/NullBindingToPrimitive.tml
 (added)
+++ 
tapestry/tapestry5/trunk/tapestry-core/src/test/resources/org/apache/tapestry5/integration/app1/pages/NullBindingToPrimitive.tml
 Fri Jul  8 00:52:40 2011
@@ -0,0 +1,5 @@
+<html t:type="Border" 
xmlns:t="http://tapestry.apache.org/schema/tapestry_5_3.xsd";>
+
+    <t:showint value="null"/>
+
+</html>


Reply via email to