Clover coverage report - Code Coverage for tapestry release 4.0.2
Coverage timestamp: Thu Apr 13 2006 10:52:06 EDT
file stats: LOC: 110   Methods: 3
NCLOC: 59   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
InjectStateWorker.java - 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.lang.reflect.Modifier;
 18   
 19    import org.apache.hivemind.Location;
 20    import org.apache.hivemind.service.BodyBuilder;
 21    import org.apache.hivemind.service.ClassFabUtils;
 22    import org.apache.hivemind.service.MethodSignature;
 23    import org.apache.hivemind.util.Defense;
 24    import org.apache.tapestry.engine.state.ApplicationStateManager;
 25    import org.apache.tapestry.event.PageDetachListener;
 26    import org.apache.tapestry.spec.InjectSpecification;
 27   
 28    /**
 29    * Worker for injecting application state objects as properties of the component. These properties
 30    * are read/write and must be "live" (changes are propogated back into the
 31    * {@link org.apache.tapestry.engine.state.ApplicationStateManager}). They should also cache in a
 32    * local variable for efficiency, and clear out that variable at the end of the request.
 33    *
 34    * @author Howard M. Lewis Ship
 35    * @since 4.0
 36    */
 37    public class InjectStateWorker implements InjectEnhancementWorker
 38    {
 39    private ApplicationStateManager _applicationStateManager;
 40   
 41  1 public void performEnhancement(EnhancementOperation op, InjectSpecification spec)
 42    {
 43  1 injectState(op, spec.getObject(), spec.getProperty(), spec.getLocation());
 44    }
 45   
 46  1 void injectState(EnhancementOperation op, String objectName, String propertyName,
 47    Location location)
 48    {
 49  1 Defense.notNull(op, "op");
 50  1 Defense.notNull(objectName, "objectName");
 51  1 Defense.notNull(propertyName, "propertyName");
 52   
 53  1 Class propertyType = EnhanceUtils.extractPropertyType(op, propertyName, null);
 54  1 String fieldName = "_$" + propertyName;
 55   
 56    // State properties are read/write
 57   
 58  1 op.claimProperty(propertyName);
 59   
 60  1 op.addField(fieldName, propertyType);
 61   
 62  1 String managerField = op.addInjectedField(
 63    "_$applicationStateManager",
 64    ApplicationStateManager.class,
 65    _applicationStateManager);
 66   
 67  1 BodyBuilder builder = new BodyBuilder();
 68   
 69    // Accessor
 70   
 71  1 builder.begin();
 72  1 builder.addln("if ({0} == null)", fieldName);
 73  1 builder.addln(" {0} = ({1}) {2}.get(\"{3}\");", new Object[]
 74    { fieldName, ClassFabUtils.getJavaClassName(propertyType), managerField, objectName });
 75  1 builder.addln("return {0};", fieldName);
 76  1 builder.end();
 77   
 78  1 String methodName = op.getAccessorMethodName(propertyName);
 79   
 80  1 MethodSignature sig = new MethodSignature(propertyType, methodName, null, null);
 81   
 82  1 op.addMethod(Modifier.PUBLIC, sig, builder.toString(), location);
 83   
 84    // Mutator
 85   
 86  1 builder.clear();
 87  1 builder.begin();
 88  1 builder.addln("{0}.store(\"{1}\", $1);", managerField, objectName);
 89  1 builder.addln("{0} = $1;", fieldName);
 90  1 builder.end();
 91   
 92  1 sig = new MethodSignature(void.class, EnhanceUtils.createMutatorMethodName(propertyName),
 93    new Class[]
 94    { propertyType }, null);
 95   
 96  1 op.addMethod(Modifier.PUBLIC, sig, builder.toString(), location);
 97   
 98    // Extend pageDetached() to clean out the cached field value.
 99   
 100  1 op.extendMethodImplementation(
 101    PageDetachListener.class,
 102    EnhanceUtils.PAGE_DETACHED_SIGNATURE,
 103    fieldName + " = null;");
 104    }
 105   
 106  1 public void setApplicationStateManager(ApplicationStateManager applicationStateManager)
 107    {
 108  1 _applicationStateManager = applicationStateManager;
 109    }
 110    }