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/30 00:56:50 UTC

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

Author: hlship
Date: Fri Apr 29 22:56:49 2011
New Revision: 1098014

URL: http://svn.apache.org/viewvc?rev=1098014&view=rev
Log:
TAP5-853: If a return value for a method invocation is overridden, the checked exception should be discarded

Modified:
    tapestry/tapestry5/trunk/plastic/src/main/java/org/apache/tapestry5/internal/plastic/AbstractMethodInvocation.java
    tapestry/tapestry5/trunk/plastic/src/main/java/org/apache/tapestry5/internal/plastic/PlasticClassImpl.java
    tapestry/tapestry5/trunk/plastic/src/main/java/org/apache/tapestry5/plastic/MethodInvocation.java
    tapestry/tapestry5/trunk/plastic/src/test/groovy/org/apache/tapestry5/plastic/MethodAdviceTests.groovy

Modified: tapestry/tapestry5/trunk/plastic/src/main/java/org/apache/tapestry5/internal/plastic/AbstractMethodInvocation.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/plastic/src/main/java/org/apache/tapestry5/internal/plastic/AbstractMethodInvocation.java?rev=1098014&r1=1098013&r2=1098014&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/plastic/src/main/java/org/apache/tapestry5/internal/plastic/AbstractMethodInvocation.java (original)
+++ tapestry/tapestry5/trunk/plastic/src/main/java/org/apache/tapestry5/internal/plastic/AbstractMethodInvocation.java Fri Apr 29 22:56:49 2011
@@ -39,6 +39,14 @@ public abstract class AbstractMethodInvo
 
     private Exception checkedException;
 
+    /**
+     * Invoked from the implementation of {@link MethodInvocation#setReturnValue(Object)}.
+     */
+    protected void clearCheckedException()
+    {
+        checkedException = null;
+    }
+
     public void rethrow()
     {
         if (checkedException != null)

Modified: tapestry/tapestry5/trunk/plastic/src/main/java/org/apache/tapestry5/internal/plastic/PlasticClassImpl.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/plastic/src/main/java/org/apache/tapestry5/internal/plastic/PlasticClassImpl.java?rev=1098014&r1=1098013&r2=1098014&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/plastic/src/main/java/org/apache/tapestry5/internal/plastic/PlasticClassImpl.java (original)
+++ tapestry/tapestry5/trunk/plastic/src/main/java/org/apache/tapestry5/internal/plastic/PlasticClassImpl.java Fri Apr 29 22:56:49 2011
@@ -1143,6 +1143,8 @@ public class PlasticClassImpl extends Lo
                 builder.castOrUnbox(description.returnType);
                 builder.putField(invocationClassName, RETURN_VALUE, description.returnType);
 
+                builder.loadThis().invoke(AbstractMethodInvocation.class, void.class, "clearCheckedException");
+
                 builder.loadThis().returnResult();
             }
         }
@@ -1694,7 +1696,8 @@ public class PlasticClassImpl extends Lo
 
             // And replace it with a constructor that throws an exception
 
-            MethodNode replacementConstructor = new MethodNode(originalAccess, CONSTRUCTOR_NAME, NOTHING_TO_VOID, null, null);
+            MethodNode replacementConstructor = new MethodNode(originalAccess, CONSTRUCTOR_NAME, NOTHING_TO_VOID, null,
+                    null);
 
             newBuilder(replacementConstructor).throwException(IllegalStateException.class, invalidConstructorMessage());
 

Modified: tapestry/tapestry5/trunk/plastic/src/main/java/org/apache/tapestry5/plastic/MethodInvocation.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/plastic/src/main/java/org/apache/tapestry5/plastic/MethodInvocation.java?rev=1098014&r1=1098013&r2=1098014&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/plastic/src/main/java/org/apache/tapestry5/plastic/MethodInvocation.java (original)
+++ tapestry/tapestry5/trunk/plastic/src/main/java/org/apache/tapestry5/plastic/MethodInvocation.java Fri Apr 29 22:56:49 2011
@@ -39,6 +39,8 @@ public interface MethodInvocation extend
      * Overrides the return value of the method. The value provided will be cast to the actual return type
      * (or, if the return type is a primitive value, the value will be cast to the corresponding wrapper type and then
      * converted to a primitive).
+     * <p>
+     * Overriding the return value clears any checked exception.
      * 
      * @param returnValue
      * @return this method invocation, for a fluent API

Modified: tapestry/tapestry5/trunk/plastic/src/test/groovy/org/apache/tapestry5/plastic/MethodAdviceTests.groovy
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/plastic/src/test/groovy/org/apache/tapestry5/plastic/MethodAdviceTests.groovy?rev=1098014&r1=1098013&r2=1098014&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/plastic/src/test/groovy/org/apache/tapestry5/plastic/MethodAdviceTests.groovy (original)
+++ tapestry/tapestry5/trunk/plastic/src/test/groovy/org/apache/tapestry5/plastic/MethodAdviceTests.groovy Fri Apr 29 22:56:49 2011
@@ -89,17 +89,38 @@ class MethodAdviceTests extends Abstract
 
         expect:
 
-        o.maybeThrow(7) == 7
+        o.maybeThrow(7L) == 7L
 
         when:
 
-        o.maybeThrow(0)
+        o.maybeThrow(0L)
 
         then:
 
         thrown(SQLException)
     }
 
+    def "setting return value clears checked exceptions"() {
+        def mgr = createMgr({ PlasticClass pc ->
+            findMethod(pc, "maybeThrow").addAdvice({  MethodInvocation mi ->
+
+                mi.proceed()
+
+                if (mi.didThrowCheckedException()) {
+                    mi.setReturnValue(-1L)
+                }
+            } as MethodAdvice)
+        } as PlasticClassTransformer)
+
+        def o = mgr.getClassInstantiator("testsubjects.MethodAdviceTarget").newInstance()
+
+        expect:
+
+        o.maybeThrow(9L) == 9L
+
+        o.maybeThrow(0L) == -1L
+    }
+
     /**
      * This is important because each double/long takes up two local variable slots.
      *