Coverage Report - org.apache.tapestry5.internal.transform.PageLifecycleAnnotationWorker
 
Classes in this File Line Coverage Branch Coverage Complexity
PageLifecycleAnnotationWorker
100%
13/13
100%
2/2
0
PageLifecycleAnnotationWorker$1
100%
4/4
75%
3/4
0
 
 1  
 // Copyright 2007 The Apache Software Foundation
 2  
 //
 3  
 // Licensed under the Apache License, Version 2.0 (the "License");
 4  
 // you may not use this file except in compliance with the License.
 5  
 // You may obtain a copy of the License at
 6  
 //
 7  
 //     http://www.apache.org/licenses/LICENSE-2.0
 8  
 //
 9  
 // Unless required by applicable law or agreed to in writing, software
 10  
 // distributed under the License is distributed on an "AS IS" BASIS,
 11  
 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 12  
 // See the License for the specific language governing permissions and
 13  
 // limitations under the License.
 14  
 
 15  
 package org.apache.tapestry5.internal.transform;
 16  
 
 17  
 import org.apache.tapestry5.internal.util.MethodInvocationBuilder;
 18  
 import org.apache.tapestry5.model.MutableComponentModel;
 19  
 import org.apache.tapestry5.services.ClassTransformation;
 20  
 import org.apache.tapestry5.services.ComponentClassTransformWorker;
 21  
 import org.apache.tapestry5.services.MethodFilter;
 22  
 import org.apache.tapestry5.services.TransformMethodSignature;
 23  
 
 24  
 import java.lang.annotation.Annotation;
 25  
 
 26  
 /**
 27  
  * Similar to {@link org.apache.tapestry5.internal.transform.RenderPhaseMethodWorker} but applies to annotations/methods
 28  
  * related to the overall page lifecycle.
 29  
  */
 30  103844
 public class PageLifecycleAnnotationWorker implements ComponentClassTransformWorker
 31  
 {
 32  
     private final Class<? extends Annotation> methodAnnotationClass;
 33  
 
 34  
     private final TransformMethodSignature lifecycleMethodSignature;
 35  
 
 36  
     private final String methodAlias;
 37  
 
 38  174
     private final MethodInvocationBuilder invocationBuilder = new MethodInvocationBuilder();
 39  
 
 40  
     public PageLifecycleAnnotationWorker(final Class<? extends Annotation> methodAnnotationClass,
 41  
                                          final TransformMethodSignature lifecycleMethodSignature,
 42  
                                          final String methodAlias)
 43  174
     {
 44  174
         this.methodAnnotationClass = methodAnnotationClass;
 45  174
         this.lifecycleMethodSignature = lifecycleMethodSignature;
 46  174
         this.methodAlias = methodAlias;
 47  174
     }
 48  
 
 49  
     public void transform(final ClassTransformation transformation, MutableComponentModel model)
 50  
     {
 51  2436
         MethodFilter filter = new MethodFilter()
 52  
         {
 53  2436
             public boolean accept(TransformMethodSignature signature)
 54  
             {
 55  51924
                 if (signature.getMethodName().equals(methodAlias))
 56  4
                     return true;
 57  
 
 58  51920
                 return transformation.getMethodAnnotation(signature, methodAnnotationClass) != null;
 59  
             }
 60  
         };
 61  
 
 62  
         // Did this they easy way, because I doubt there will be more than one signature.
 63  
         // If I expected lots of signatures, I'd build up a BodyBuilder in the loop and extend the
 64  
         // method outside the loop.
 65  
 
 66  2436
         for (TransformMethodSignature signature : transformation.findMethods(filter))
 67  
         {
 68  
             // TODO: Filter out the non-void methods (with a non-fatal warning/error?) For the
 69  
             // moment, we just invoke the method anyway, and ignore the result. Also, MethodInvocationBuilder
 70  
             // is very forgiving (and silent) about unexpected parameters (passing null/0/false).
 71  
 
 72  4
             String body = invocationBuilder.buildMethodInvocation(signature, transformation);
 73  
 
 74  4
             transformation.extendMethod(lifecycleMethodSignature, body + ";");
 75  4
         }
 76  2436
     }
 77  
 
 78  
 }