Clover coverage report - Code Coverage for tapestry release 4.0-rc-2
Coverage timestamp: Sat Dec 17 2005 09:39:46 PST
file stats: LOC: 107   Methods: 3
NCLOC: 64   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
InjectComponentWorker.java 100% 100% 100% 100%
coverage
 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> element was
 31    * specified. This makes it easier to reference a particular component from Java code.
 32    *
 33    * @author Howard M. Lewis Ship
 34    * @since 4.0
 35    */
 36    public class InjectComponentWorker implements EnhancementWorker
 37    {
 38    private ErrorLog _errorLog;
 39   
 40  392 public void performEnhancement(EnhancementOperation op, IComponentSpecification spec)
 41    {
 42  392 Iterator i = spec.getComponentIds().iterator();
 43   
 44  392 while (i.hasNext())
 45    {
 46  234 String id = (String) i.next();
 47   
 48  234 IContainedComponent cc = spec.getComponent(id);
 49   
 50  234 String propertyName = cc.getPropertyName();
 51   
 52  234 if (propertyName != null)
 53    {
 54  2 try
 55    {
 56  2 injectComponent(op, id, propertyName, cc.getLocation());
 57    }
 58    catch (Exception ex)
 59    {
 60  1 _errorLog.error(EnhanceMessages.errorAddingProperty(propertyName, op
 61    .getBaseClass(), ex), cc.getLocation(), ex);
 62    }
 63    }
 64    }
 65    }
 66   
 67  2 public void injectComponent(EnhancementOperation op, String componentId, String propertyName,
 68    Location location)
 69    {
 70  2 Defense.notNull(op, "op");
 71  2 Defense.notNull(componentId, "componentId");
 72  2 Defense.notNull(propertyName, "propertyName");
 73   
 74  2 Class propertyType = EnhanceUtils.extractPropertyType(op, propertyName, null);
 75   
 76  2 op.claimReadonlyProperty(propertyName);
 77   
 78  1 String fieldName = "_$" + propertyName;
 79  1 String classField = op.getClassReference(propertyType);
 80  1 String locationField = op.addInjectedField(
 81    fieldName + "$location",
 82    Location.class,
 83    location);
 84   
 85  1 op.addField(fieldName, propertyType);
 86   
 87  1 EnhanceUtils.createSimpleAccessor(op, fieldName, propertyName, propertyType, location);
 88   
 89    // I.e. _$fred = (IComponent) TapestryUtils.getComponent(this, "fred", IComponent.class,
 90    // location)
 91   
 92  1 BodyBuilder builder = new BodyBuilder();
 93   
 94  1 builder.add("{0} = ({1}) ", fieldName, ClassFabUtils.getJavaClassName(propertyType));
 95  1 builder.add("{0}#getComponent(this, ", TapestryUtils.class.getName());
 96  1 builder.addQuoted(componentId);
 97  1 builder.add(", {0}, {1});", classField, locationField);
 98   
 99  1 op.extendMethodImplementation(IComponent.class, EnhanceUtils.FINISH_LOAD_SIGNATURE, builder
 100    .toString());
 101    }
 102   
 103  40 public void setErrorLog(ErrorLog errorLog)
 104    {
 105  40 _errorLog = errorLog;
 106    }
 107    }