Author: hlship
Date: Fri Feb 19 18:53:40 2010
New Revision: 911918

URL: http://svn.apache.org/viewvc?rev=911918&view=rev
Log:
Recode more workers using the updated class transformation APIs

Removed:
    
tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/internal/transform/InvokePostRenderCleanupOnResourcesWorkerTest.java
Modified:
    
tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/transform/PageLifecycleAnnotationWorker.java

Modified: 
tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/transform/PageLifecycleAnnotationWorker.java
URL: 
http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/transform/PageLifecycleAnnotationWorker.java?rev=911918&r1=911917&r2=911918&view=diff
==============================================================================
--- 
tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/transform/PageLifecycleAnnotationWorker.java
 (original)
+++ 
tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/transform/PageLifecycleAnnotationWorker.java
 Fri Feb 19 18:53:40 2010
@@ -1,10 +1,10 @@
-// Copyright 2007 The Apache Software Foundation
+// Copyright 2007, 2010 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
+// 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,
@@ -14,18 +14,23 @@
 
 package org.apache.tapestry5.internal.transform;
 
-import org.apache.tapestry5.internal.util.MethodInvocationBuilder;
+import java.lang.annotation.Annotation;
+import java.util.List;
+
+import org.apache.tapestry5.ioc.Predicate;
 import org.apache.tapestry5.model.MutableComponentModel;
 import org.apache.tapestry5.services.ClassTransformation;
 import org.apache.tapestry5.services.ComponentClassTransformWorker;
-import org.apache.tapestry5.services.MethodFilter;
+import org.apache.tapestry5.services.ComponentMethodAdvice;
+import org.apache.tapestry5.services.ComponentMethodInvocation;
+import org.apache.tapestry5.services.MethodAccess;
+import org.apache.tapestry5.services.MethodInvocationResult;
+import org.apache.tapestry5.services.TransformMethod;
 import org.apache.tapestry5.services.TransformMethodSignature;
 
-import java.lang.annotation.Annotation;
-
 /**
  * Similar to {...@link 
org.apache.tapestry5.internal.transform.RenderPhaseMethodWorker} but applies to 
annotations/methods
- * related to the overall page lifecycle.
+ * related to the overall page lifecycle. Page lifecycle methods are always 
void and take no parameters.
  */
 public class PageLifecycleAnnotationWorker implements 
ComponentClassTransformWorker
 {
@@ -35,11 +40,8 @@
 
     private final String methodAlias;
 
-    private final MethodInvocationBuilder invocationBuilder = new 
MethodInvocationBuilder();
-
-    public PageLifecycleAnnotationWorker(final Class<? extends Annotation> 
methodAnnotationClass,
-                                         final TransformMethodSignature 
lifecycleMethodSignature,
-                                         final String methodAlias)
+    public PageLifecycleAnnotationWorker(Class<? extends Annotation> 
methodAnnotationClass,
+            TransformMethodSignature lifecycleMethodSignature, String 
methodAlias)
     {
         this.methodAnnotationClass = methodAnnotationClass;
         this.lifecycleMethodSignature = lifecycleMethodSignature;
@@ -48,31 +50,61 @@
 
     public void transform(final ClassTransformation transformation, 
MutableComponentModel model)
     {
-        MethodFilter filter = new MethodFilter()
+        for (TransformMethod method : matchLifecycleMethods(transformation))
         {
-            public boolean accept(TransformMethodSignature signature)
+            invokeMethodWithinLifecycle(transformation, method);
+        }
+    }
+
+    private void invokeMethodWithinLifecycle(final ClassTransformation 
transformation, TransformMethod method)
+    {
+        validateMethodSignature(method);
+
+        final MethodAccess access = method.getAccess();
+
+        ComponentMethodAdvice advice = createAdviceToInvokeMethod(access);
+
+        transformation.getMethod(lifecycleMethodSignature).addAdvice(advice);
+    }
+
+    private ComponentMethodAdvice createAdviceToInvokeMethod(final 
MethodAccess access)
+    {
+        return new ComponentMethodAdvice()
+        {
+            public void advise(ComponentMethodInvocation invocation)
             {
-                if (signature.getMethodName().equals(methodAlias))
-                    return true;
+                invocation.proceed();
 
-                return transformation.getMethodAnnotation(signature, 
methodAnnotationClass) != null;
+                MethodInvocationResult result = 
access.invoke(invocation.getInstance());
+
+                result.rethrow();
             }
         };
+    }
 
-        // Did this they easy way, because I doubt there will be more than one 
signature.
-        // If I expected lots of signatures, I'd build up a BodyBuilder in the 
loop and extend the
-        // method outside the loop.
+    private void validateMethodSignature(TransformMethod method)
+    {
+        TransformMethodSignature signature = method.getSignature();
 
-        for (TransformMethodSignature signature : 
transformation.findMethods(filter))
-        {
-            // TODO: Filter out the non-void methods (with a non-fatal 
warning/error?) For the
-            // moment, we just invoke the method anyway, and ignore the 
result. Also, MethodInvocationBuilder
-            // is very forgiving (and silent) about unexpected parameters 
(passing null/0/false).
+        if (!signature.getReturnType().equals("void"))
+            throw new RuntimeException(String.format("Method %s is a lifecycle 
method and should return void.", method
+                    .getMethodIdentifier()));
+
+        if (signature.getParameterTypes().length > 0)
+            throw new RuntimeException(String.format("Method %s is a lifecycle 
method and should take no parameters.",
+                    method.getMethodIdentifier()));
+    }
 
-            String body = invocationBuilder.buildMethodInvocation(signature, 
transformation);
+    private List<TransformMethod> matchLifecycleMethods(final 
ClassTransformation transformation)
+    {
+        return transformation.matchMethods(new Predicate<TransformMethod>()
+        {
 
-            transformation.extendMethod(lifecycleMethodSignature, body + ";");
-        }
+            public boolean accept(TransformMethod method)
+            {
+                return method.getName().equalsIgnoreCase(methodAlias)
+                        || method.getAnnotation(methodAnnotationClass) != null;
+            }
+        });
     }
-
 }


Reply via email to