Coverage Report - org.apache.tapestry5.internal.transform.PageActivationContextWorker
 
Classes in this File Line Coverage Branch Coverage Complexity
PageActivationContextWorker
100%
15/15
100%
8/8
0
 
 1  
 // Copyright 2008, 2009 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.annotations.PageActivationContext;
 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.TransformMethodSignature;
 22  
 
 23  
 import java.lang.reflect.Modifier;
 24  
 import java.util.List;
 25  
 
 26  
 /**
 27  
  * Provides the page activation context handlers.  This worker must be scheduled before {@link
 28  
  * org.apache.tapestry5.internal.transform.OnEventWorker} in order for the added event handler methods to be properly
 29  
  * picked up and processed.
 30  
  *
 31  
  * @see org.apache.tapestry5.annotations.PageActivationContext
 32  
  */
 33  66
 public class PageActivationContextWorker implements ComponentClassTransformWorker
 34  
 {
 35  
     public void transform(ClassTransformation transformation, MutableComponentModel model)
 36  
     {
 37  822
         List<String> fields = transformation.findFieldsWithAnnotation(PageActivationContext.class);
 38  
 
 39  822
         if (fields.size() > 1)
 40  2
             throw new RuntimeException(TransformMessages.illegalNumberOfPageActivationContextHandlers(fields));
 41  
 
 42  820
         for (String fieldName : fields)
 43  
         {
 44  8
             PageActivationContext annotation = transformation.getFieldAnnotation(fieldName,
 45  
                                                                                  PageActivationContext.class);
 46  
 
 47  8
             String fieldType = transformation.getFieldType(fieldName);
 48  
 
 49  8
             if (annotation.activate())
 50  
             {
 51  4
                 TransformMethodSignature activate
 52  
                         = new TransformMethodSignature(Modifier.PROTECTED | Modifier.FINAL, "void",
 53  
                                                        "onActivate",
 54  
                                                        new String[] { fieldType }, null);
 55  4
                 transformation.addTransformedMethod(activate, fieldName + " = $1;");
 56  
             }
 57  
 
 58  8
             if (annotation.passivate())
 59  
             {
 60  4
                 TransformMethodSignature passivate
 61  
                         = new TransformMethodSignature(Modifier.PROTECTED | Modifier.FINAL, "java.lang.Object",
 62  
                                                        "onPassivate",
 63  
                                                        null, null);
 64  4
                 transformation.addTransformedMethod(passivate, "return ($w) " + fieldName + ";");
 65  
             }
 66  8
         }
 67  
 
 68  820
     }
 69  
 }