Coverage Report - org.apache.tapestry.enhance.InjectComponentWorker
 
Classes in this File Line Coverage Branch Coverage Complexity
InjectComponentWorker
0%
0/33
0%
0/10
2
 
 1  
 // Copyright 2005 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.tapestry.enhance;
 16  
 
 17  
 import java.util.Iterator;
 18  
 
 19  
 import org.apache.hivemind.ErrorLog;
 20  
 import org.apache.hivemind.Location;
 21  
 import org.apache.hivemind.service.BodyBuilder;
 22  
 import org.apache.hivemind.service.ClassFabUtils;
 23  
 import org.apache.hivemind.util.Defense;
 24  
 import org.apache.tapestry.IComponent;
 25  
 import org.apache.tapestry.TapestryUtils;
 26  
 import org.apache.tapestry.spec.IComponentSpecification;
 27  
 import org.apache.tapestry.spec.IContainedComponent;
 28  
 
 29  
 /**
 30  
  * Injects components for which the property attribute of the <component>
 31  
  * element was specified. This makes it easier to reference a particular
 32  
  * component from Java code.
 33  
  * 
 34  
  * @author Howard M. Lewis Ship
 35  
  * @since 4.0
 36  
  */
 37  0
 public class InjectComponentWorker implements EnhancementWorker
 38  
 {
 39  
 
 40  
     private ErrorLog _errorLog;
 41  
 
 42  
     public void performEnhancement(EnhancementOperation op,
 43  
             IComponentSpecification spec)
 44  
     {
 45  0
         Iterator i = spec.getComponentIds().iterator();
 46  
 
 47  0
         while(i.hasNext())
 48  
         {
 49  0
             String id = (String) i.next();
 50  
 
 51  0
             IContainedComponent cc = spec.getComponent(id);
 52  
 
 53  0
             String propertyName = cc.getPropertyName();
 54  
 
 55  0
             if (propertyName != null)
 56  
             {
 57  
                 try
 58  
                 {
 59  0
                     injectComponent(op, id, propertyName, cc.getLocation());
 60  
                 }
 61  0
                 catch (Exception ex)
 62  
                 {
 63  0
                     _errorLog.error(EnhanceMessages.errorAddingProperty(
 64  
                             propertyName, op.getBaseClass(), ex), cc
 65  
                             .getLocation(), ex);
 66  0
                 }
 67  
             }
 68  0
         }
 69  0
     }
 70  
 
 71  
     public void injectComponent(EnhancementOperation op, String componentId,
 72  
             String propertyName, Location location)
 73  
     {
 74  0
         Defense.notNull(op, "op");
 75  0
         Defense.notNull(componentId, "componentId");
 76  0
         Defense.notNull(propertyName, "propertyName");
 77  
 
 78  0
         Class propertyType = EnhanceUtils.extractPropertyType(op, propertyName,
 79  
                 null);
 80  
 
 81  0
         op.claimReadonlyProperty(propertyName);
 82  
 
 83  0
         String fieldName = "_$" + propertyName;
 84  0
         String classField = op.getClassReference(propertyType);
 85  0
         String locationField = op.addInjectedField(fieldName + "$location",
 86  0
                 Location.class, location);
 87  
 
 88  0
         op.addField(fieldName, propertyType);
 89  
 
 90  0
         EnhanceUtils.createSimpleAccessor(op, fieldName, propertyName,
 91  
                 propertyType, location);
 92  
 
 93  
         // I.e. _$fred = (IComponent) TapestryUtils.getComponent(this, "fred",
 94  
         // IComponent.class,
 95  
         // location)
 96  
 
 97  0
         BodyBuilder builder = new BodyBuilder();
 98  
 
 99  0
         builder.add("{0} = ({1}) ", fieldName, ClassFabUtils
 100  
                 .getJavaClassName(propertyType));
 101  0
         builder.add("{0}#getComponent(this, ", TapestryUtils.class.getName());
 102  0
         builder.addQuoted(componentId);
 103  0
         builder.add(", {0}, {1});", classField, locationField);
 104  
 
 105  0
         op.extendMethodImplementation(IComponent.class,
 106  
                 EnhanceUtils.FINISH_LOAD_SIGNATURE, builder.toString());
 107  0
     }
 108  
 
 109  
     public void setErrorLog(ErrorLog errorLog)
 110  
     {
 111  0
         _errorLog = errorLog;
 112  0
     }
 113  
 }