Coverage Report - org.apache.tapestry5.internal.transform.InjectContainerWorker
 
Classes in this File Line Coverage Branch Coverage Complexity
InjectContainerWorker
100%
26/26
100%
4/4
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.InjectContainer;
 18  
 import org.apache.tapestry5.ioc.util.BodyBuilder;
 19  
 import org.apache.tapestry5.model.MutableComponentModel;
 20  
 import org.apache.tapestry5.runtime.Component;
 21  
 import org.apache.tapestry5.services.ClassTransformation;
 22  
 import org.apache.tapestry5.services.ComponentClassTransformWorker;
 23  
 import org.apache.tapestry5.services.TransformConstants;
 24  
 
 25  
 import java.util.List;
 26  
 
 27  
 /**
 28  
  * Identifies the {@link org.apache.tapestry5.annotations.InjectContainer} annotation and adds code to initialize it to
 29  
  * the core component.
 30  
  */
 31  58
 public class InjectContainerWorker implements ComponentClassTransformWorker
 32  
 {
 33  
 
 34  
     public void transform(ClassTransformation transformation, MutableComponentModel model)
 35  
     {
 36  814
         List<String> names = transformation.findFieldsWithAnnotation(InjectContainer.class);
 37  
 
 38  814
         if (names.isEmpty())
 39  788
             return;
 40  
 
 41  
         // I can't imagine a scenario where a component would have more than one
 42  
         // field with InjectComponent, but that's the way these APIs work, lists of names.
 43  
 
 44  26
         BodyBuilder builder = new BodyBuilder();
 45  26
         builder.begin();
 46  
 
 47  26
         builder.addln("%s container = %s.getContainer();", Component.class.getName(), transformation
 48  
                 .getResourcesFieldName());
 49  
 
 50  26
         for (String fieldName : names)
 51  
         {
 52  26
             InjectContainer annotation = transformation.getFieldAnnotation(
 53  
                     fieldName,
 54  
                     InjectContainer.class);
 55  
 
 56  26
             transformation.claimField(fieldName, annotation);
 57  
             
 58  26
             String fieldType = transformation.getFieldType(fieldName);
 59  
 
 60  26
             builder.addln("try");
 61  26
             builder.begin();
 62  26
             builder.addln("this.%s = (%s) container;", fieldName, fieldType);
 63  26
             builder.end();
 64  26
             builder.addln("catch (ClassCastException ex)");
 65  26
             builder.begin();
 66  26
             builder.addln(
 67  
                     "String message = %s.buildCastExceptionMessage(container, \"%s.%s\", \"%s\");",
 68  
                     InjectContainerWorker.class.getName(),
 69  
                     model.getComponentClassName(),
 70  
                     fieldName,
 71  
                     fieldType);
 72  26
             builder.addln("throw new RuntimeException(message, ex);");
 73  26
             builder.end();
 74  
 
 75  26
             transformation.makeReadOnly(fieldName);
 76  26
         }
 77  
 
 78  26
         builder.end();
 79  
 
 80  26
         transformation.extendMethod(TransformConstants.CONTAINING_PAGE_DID_LOAD_SIGNATURE, builder
 81  
                 .toString());
 82  26
     }
 83  
 
 84  
     public static String buildCastExceptionMessage(Component component, String fieldName,
 85  
                                                    String fieldType)
 86  
     {
 87  2
         return TransformMessages.componentNotAssignableToField(component, fieldName, fieldType);
 88  
     }
 89  
 }