Coverage Report - org.apache.tapestry5.internal.transform.InjectPageWorker
 
Classes in this File Line Coverage Branch Coverage Complexity
InjectPageWorker
100%
26/26
100%
6/6
0
 
 1  
 // Copyright 2006, 2007, 2008 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.InjectPage;
 18  
 import org.apache.tapestry5.ioc.internal.util.InternalUtils;
 19  
 import org.apache.tapestry5.ioc.util.BodyBuilder;
 20  
 import org.apache.tapestry5.model.MutableComponentModel;
 21  
 import org.apache.tapestry5.services.*;
 22  
 
 23  
 import java.lang.reflect.Modifier;
 24  
 import java.util.List;
 25  
 
 26  
 /**
 27  
  * Peforms transformations that allow pages to be injected into components.
 28  
  *
 29  
  * @see org.apache.tapestry5.annotations.InjectPage
 30  
  */
 31  
 public class InjectPageWorker implements ComponentClassTransformWorker
 32  
 {
 33  
     private final ComponentSource componentSource;
 34  
 
 35  
     private final ComponentClassResolver resolver;
 36  
 
 37  
     public InjectPageWorker(ComponentSource componentSource, ComponentClassResolver resolver)
 38  58
     {
 39  58
         this.componentSource = componentSource;
 40  58
         this.resolver = resolver;
 41  58
     }
 42  
 
 43  
     public void transform(ClassTransformation transformation, MutableComponentModel model)
 44  
     {
 45  814
         List<String> names = transformation.findFieldsWithAnnotation(InjectPage.class);
 46  
 
 47  814
         if (names.isEmpty()) return;
 48  
 
 49  40
         String componentSource = transformation.addInjectedField(ComponentSource.class, "componentSource",
 50  
                                                                  this.componentSource);
 51  
 
 52  
 
 53  40
         for (String name : names)
 54  42
             addInjectedPage(transformation, name, componentSource);
 55  
 
 56  40
     }
 57  
 
 58  
     private void addInjectedPage(ClassTransformation transformation, String fieldName, String componentSource)
 59  
     {
 60  42
         InjectPage annotation = transformation.getFieldAnnotation(fieldName, InjectPage.class);
 61  
         
 62  42
         transformation.claimField(fieldName, annotation);
 63  
 
 64  42
         String pageName = annotation.value();
 65  
 
 66  42
         String fieldType = transformation.getFieldType(fieldName);
 67  42
         String methodName = transformation.newMemberName("read_inject_page", fieldName);
 68  
 
 69  42
         String injectedPageName = InternalUtils.isBlank(pageName) ? resolver
 70  
                 .resolvePageClassNameToPageName(fieldType) : pageName;
 71  
 
 72  42
         TransformMethodSignature sig = new TransformMethodSignature(Modifier.PRIVATE, fieldType, methodName, null,
 73  
                                                                     null);
 74  
 
 75  42
         BodyBuilder builder = new BodyBuilder();
 76  42
         builder.begin();
 77  
 
 78  42
         builder.addln("return (%s) %s.getPage(\"%s\");", fieldType, componentSource, injectedPageName);
 79  
 
 80  42
         builder.end();
 81  
 
 82  42
         transformation.addMethod(sig, builder.toString());
 83  42
         transformation.replaceReadAccess(fieldName, methodName);
 84  42
         transformation.makeReadOnly(fieldName);
 85  42
         transformation.removeField(fieldName);
 86  42
     }
 87  
 }