Coverage Report - org.apache.tapestry5.internal.transform.ComponentWorker
 
Classes in this File Line Coverage Branch Coverage Complexity
ComponentWorker
100%
37/37
100%
16/16
0
 
 1  
 // Copyright 2006, 2007, 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.Component;
 18  
 import org.apache.tapestry5.annotations.MixinClasses;
 19  
 import org.apache.tapestry5.annotations.Mixins;
 20  
 import org.apache.tapestry5.internal.KeyValue;
 21  
 import org.apache.tapestry5.internal.TapestryInternalUtils;
 22  
 import org.apache.tapestry5.ioc.Location;
 23  
 import org.apache.tapestry5.ioc.internal.services.StringLocation;
 24  
 import org.apache.tapestry5.ioc.internal.util.CollectionFactory;
 25  
 import org.apache.tapestry5.ioc.internal.util.InternalUtils;
 26  
 import org.apache.tapestry5.model.ComponentModel;
 27  
 import org.apache.tapestry5.model.MutableComponentModel;
 28  
 import org.apache.tapestry5.model.MutableEmbeddedComponentModel;
 29  
 import org.apache.tapestry5.services.ClassTransformation;
 30  
 import org.apache.tapestry5.services.ComponentClassResolver;
 31  
 import org.apache.tapestry5.services.ComponentClassTransformWorker;
 32  
 import org.apache.tapestry5.services.TransformConstants;
 33  
 
 34  
 /**
 35  
  * Finds fields with the {@link org.apache.tapestry5.annotations.Component} annotation and updates the model. Also
 36  
  * checks for the {@link Mixins} and {@link MixinClasses} annotations and uses them to update the {@link
 37  
  * ComponentModel}.
 38  
  */
 39  
 public class ComponentWorker implements ComponentClassTransformWorker
 40  
 {
 41  
     private final ComponentClassResolver resolver;
 42  
 
 43  
     public ComponentWorker(final ComponentClassResolver resolver)
 44  58
     {
 45  58
         this.resolver = resolver;
 46  58
     }
 47  
 
 48  
     public void transform(ClassTransformation transformation, MutableComponentModel model)
 49  
     {
 50  814
         for (String fieldName : transformation.findFieldsWithAnnotation(Component.class))
 51  
         {
 52  166
             Component annotation = transformation.getFieldAnnotation(fieldName, Component.class);
 53  
 
 54  166
             transformation.claimField(fieldName, annotation);
 55  
 
 56  166
             String id = annotation.id();
 57  
 
 58  166
             if (InternalUtils.isBlank(id)) id = InternalUtils.stripMemberName(fieldName);
 59  
 
 60  166
             String type = transformation.getFieldType(fieldName);
 61  
 
 62  166
             Location location = new StringLocation(String.format("%s.%s", transformation
 63  
                     .getClassName(), fieldName), 0);
 64  
 
 65  166
             MutableEmbeddedComponentModel embedded = model.addEmbeddedComponent(id, annotation
 66  
                     .type(), type, annotation.inheritInformalParameters(), location);
 67  
 
 68  166
             addParameters(embedded, annotation.parameters());
 69  
 
 70  
 
 71  166
             String names = annotation.publishParameters();
 72  166
             if (InternalUtils.isNonBlank(names))
 73  
             {
 74  16
                 embedded.setPublishedParameters(CollectionFactory.newList(TapestryInternalUtils.splitAtCommas(names)));
 75  
             }
 76  
 
 77  
 
 78  166
             transformation.makeReadOnly(fieldName);
 79  
 
 80  166
             String body = String.format("%s = (%s) %s.getEmbeddedComponent(\"%s\");", fieldName, type,
 81  
                                         transformation.getResourcesFieldName(), id);
 82  
 
 83  166
             transformation
 84  
                     .extendMethod(TransformConstants.CONTAINING_PAGE_DID_LOAD_SIGNATURE, body);
 85  
 
 86  166
             addMixinClasses(fieldName, transformation, embedded);
 87  166
             addMixinTypes(fieldName, transformation, embedded);
 88  166
         }
 89  814
     }
 90  
 
 91  
     private void addMixinClasses(String fieldName, ClassTransformation transformation,
 92  
                                  MutableEmbeddedComponentModel model)
 93  
     {
 94  166
         MixinClasses annotation = transformation.getFieldAnnotation(fieldName, MixinClasses.class);
 95  
 
 96  166
         if (annotation == null) return;
 97  
 
 98  4
         for (Class c : annotation.value())
 99  2
             model.addMixin(c.getName());
 100  2
     }
 101  
 
 102  
     private void addMixinTypes(String fieldName, ClassTransformation transformation,
 103  
                                MutableEmbeddedComponentModel model)
 104  
     {
 105  166
         Mixins annotation = transformation.getFieldAnnotation(fieldName, Mixins.class);
 106  
 
 107  166
         if (annotation == null) return;
 108  
 
 109  4
         for (String typeName : annotation.value())
 110  
         {
 111  2
             String mixinClassName = resolver.resolveMixinTypeToClassName(typeName);
 112  2
             model.addMixin(mixinClassName);
 113  
         }
 114  2
     }
 115  
 
 116  
     private void addParameters(MutableEmbeddedComponentModel embedded, String[] parameters)
 117  
     {
 118  430
         for (String parameter : parameters)
 119  
         {
 120  264
             KeyValue kv = TapestryInternalUtils.parseKeyValue(parameter);
 121  
 
 122  264
             embedded.addParameter(kv.getKey(), kv.getValue());
 123  
         }
 124  166
     }
 125  
 }