Coverage Report - org.apache.tapestry5.internal.transform.InjectServiceWorker
 
Classes in this File Line Coverage Branch Coverage Complexity
InjectServiceWorker
100%
15/15
100%
4/4
0
 
 1  
 // Copyright 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.internal.services.ComponentClassCache;
 18  
 import org.apache.tapestry5.ioc.ObjectLocator;
 19  
 import org.apache.tapestry5.ioc.annotations.InjectService;
 20  
 import org.apache.tapestry5.model.MutableComponentModel;
 21  
 import org.apache.tapestry5.services.ClassTransformation;
 22  
 import org.apache.tapestry5.services.ComponentClassTransformWorker;
 23  
 
 24  
 import java.util.List;
 25  
 
 26  
 /**
 27  
  * Processes the {@link org.apache.tapestry5.ioc.annotations.InjectService} annotation.
 28  
  *
 29  
  * @since 5.1.0.0
 30  
  */
 31  
 public class InjectServiceWorker implements ComponentClassTransformWorker
 32  
 {
 33  
     private final ObjectLocator locator;
 34  
 
 35  
     private final ComponentClassCache cache;
 36  
 
 37  
     public InjectServiceWorker(ObjectLocator locator, ComponentClassCache cache)
 38  58
     {
 39  58
         this.locator = locator;
 40  58
         this.cache = cache;
 41  58
     }
 42  
 
 43  
     public void transform(ClassTransformation transformation, MutableComponentModel model)
 44  
     {
 45  814
         List<String> names = transformation.findFieldsWithAnnotation(InjectService.class);
 46  
 
 47  814
         if (names.isEmpty()) return;
 48  
 
 49  2
         for (String name : names)
 50  
         {
 51  2
             InjectService annotation = transformation.getFieldAnnotation(name, InjectService.class);
 52  
 
 53  2
             String typeName = transformation.getFieldType(name);
 54  
 
 55  2
             Class fieldType = cache.forName(typeName);
 56  
 
 57  2
             Object service = locator.getService(annotation.value(), fieldType);
 58  
 
 59  2
             transformation.injectField(name, service);
 60  
 
 61  2
             transformation.claimField(name, annotation);
 62  2
         }
 63  2
     }
 64  
 }