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