Coverage Report - org.apache.tapestry5.internal.transform.MixinWorker
 
Classes in this File Line Coverage Branch Coverage Complexity
MixinWorker
100%
16/16
100%
4/4
0
 
 1  
 // Copyright 2006, 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.Mixin;
 18  
 import org.apache.tapestry5.ioc.internal.util.InternalUtils;
 19  
 import org.apache.tapestry5.model.MutableComponentModel;
 20  
 import org.apache.tapestry5.services.ClassTransformation;
 21  
 import org.apache.tapestry5.services.ComponentClassResolver;
 22  
 import org.apache.tapestry5.services.ComponentClassTransformWorker;
 23  
 import org.apache.tapestry5.services.TransformConstants;
 24  
 
 25  
 import java.util.List;
 26  
 
 27  
 /**
 28  
  * Supports the {@link org.apache.tapestry5.annotations.Mixin} annotation, which allows a mixin to be part of the
 29  
  * implementation of a component. The annotation is applied to a field, which will become read-only, and contain a
 30  
  * reference to the mixin instance.
 31  
  */
 32  
 public class MixinWorker implements ComponentClassTransformWorker
 33  
 {
 34  
     private final ComponentClassResolver resolver;
 35  
 
 36  
     public MixinWorker(final ComponentClassResolver resolver)
 37  64
     {
 38  64
         this.resolver = resolver;
 39  64
     }
 40  
 
 41  
     public void transform(ClassTransformation transformation, MutableComponentModel model)
 42  
     {
 43  820
         List<String> fields = transformation.findFieldsWithAnnotation(Mixin.class);
 44  
 
 45  820
         for (String fieldName : fields)
 46  
         {
 47  72
             Mixin annotation = transformation.getFieldAnnotation(fieldName, Mixin.class);
 48  
 
 49  72
             transformation.claimField(fieldName, annotation);
 50  
             
 51  72
             String mixinType = annotation.value();
 52  
 
 53  72
             String fieldType = transformation.getFieldType(fieldName);
 54  
 
 55  72
             String mixinClassName = InternalUtils.isBlank(mixinType) ? fieldType : resolver
 56  
                     .resolveMixinTypeToClassName(mixinType);
 57  
 
 58  72
             model.addMixinClassName(mixinClassName);
 59  
 
 60  72
             transformation.makeReadOnly(fieldName);
 61  
 
 62  72
             String body = String.format("%s = (%s) %s.getMixinByClassName(\"%s\");", fieldName, fieldType,
 63  
                                         transformation.getResourcesFieldName(), mixinClassName);
 64  
 
 65  72
             transformation
 66  
                     .extendMethod(TransformConstants.CONTAINING_PAGE_DID_LOAD_SIGNATURE, body);
 67  72
         }
 68  820
     }
 69  
 }