Clover coverage report - Code Coverage for tapestry release 3.1-alpha-1
Coverage timestamp: Mon Feb 21 2005 09:16:14 EST
file stats: LOC: 251   Methods: 7
NCLOC: 153   Classes: 1
30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover
 
 Source file Conditionals Statements Methods TOTAL
ParameterPropertyWorker.java 100% 100% 100% 100%
coverage
 1   
 // Copyright 2004, 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   
 import java.util.Iterator;
 19   
 
 20   
 import org.apache.hivemind.ApplicationRuntimeException;
 21   
 import org.apache.hivemind.ErrorLog;
 22   
 import org.apache.hivemind.service.BodyBuilder;
 23   
 import org.apache.hivemind.service.ClassFabUtils;
 24   
 import org.apache.hivemind.service.MethodSignature;
 25   
 import org.apache.tapestry.IBinding;
 26   
 import org.apache.tapestry.IComponent;
 27   
 import org.apache.tapestry.spec.IComponentSpecification;
 28   
 import org.apache.tapestry.spec.IParameterSpecification;
 29   
 
 30   
 /**
 31   
  * Responsible for creating properties for connected parameters.
 32   
  * 
 33   
  * @author Howard M. Lewis Ship
 34   
  * @since 3.1
 35   
  */
 36   
 public class ParameterPropertyWorker implements EnhancementWorker
 37   
 {
 38   
     private ErrorLog _errorLog;
 39   
 
 40  534
     public void performEnhancement(EnhancementOperation op, IComponentSpecification spec)
 41   
     {
 42  534
         Iterator i = spec.getParameterNames().iterator();
 43  534
         while (i.hasNext())
 44   
         {
 45  1336
             String name = (String) i.next();
 46   
 
 47  1336
             IParameterSpecification ps = spec.getParameter(name);
 48   
 
 49  1336
             try
 50   
             {
 51  1336
                 performEnhancement(op, name, ps);
 52   
             }
 53   
             catch (RuntimeException ex)
 54   
             {
 55  1
                 _errorLog.error(EnhanceMessages.errorAddingProperty(ps.getPropertyName(), op
 56   
                         .getBaseClass(), ex), ps.getLocation(), ex);
 57   
             }
 58   
         }
 59   
     }
 60   
 
 61   
     /**
 62   
      * Performs the enhancement for a single parameter; this is about to change radically in release
 63   
      * 3.1 but for the moment we're emulating 3.0 behavior.
 64   
      */
 65   
 
 66  1336
     private void performEnhancement(EnhancementOperation op, String parameterName,
 67   
             IParameterSpecification ps)
 68   
     {
 69  1336
         String propertyName = ps.getPropertyName();
 70   
 
 71  1336
         Class propertyType = EnhanceUtils.extractPropertyType(op, propertyName, ps.getType());
 72   
 
 73   
         // 3.0 would allow connected parameter properties to be fully implemented
 74   
         // in the component class. This is not supported in 3.1 and an existing
 75   
         // property will be overwritten in the subclass.
 76   
 
 77  1335
         op.claimProperty(propertyName);
 78   
 
 79   
         // 3.0 used to support a property for the binding itself. That's
 80   
         // no longer the case.
 81   
 
 82  1335
         String fieldName = "_$" + propertyName;
 83  1335
         String defaultFieldName = fieldName + "$Default";
 84  1335
         String cachedFieldName = fieldName + "$Cached";
 85   
 
 86  1335
         op.addField(fieldName, propertyType);
 87  1335
         op.addField(defaultFieldName, propertyType);
 88  1335
         op.addField(cachedFieldName, boolean.class);
 89   
 
 90  1335
         buildAccessor(
 91   
                 op,
 92   
                 parameterName,
 93   
                 propertyName,
 94   
                 propertyType,
 95   
                 fieldName,
 96   
                 defaultFieldName,
 97   
                 cachedFieldName);
 98   
 
 99  1335
         buildMutator(
 100   
                 op,
 101   
                 parameterName,
 102   
                 propertyName,
 103   
                 propertyType,
 104   
                 fieldName,
 105   
                 defaultFieldName,
 106   
                 cachedFieldName);
 107   
 
 108  1335
         extendCleanupAfterRender(
 109   
                 op,
 110   
                 parameterName,
 111   
                 propertyName,
 112   
                 propertyType,
 113   
                 fieldName,
 114   
                 defaultFieldName,
 115   
                 cachedFieldName);
 116   
     }
 117   
 
 118  1335
     private void extendCleanupAfterRender(EnhancementOperation op, String parameterName,
 119   
             String propertyName, Class propertyType, String fieldName, String defaultFieldName,
 120   
             String cachedFieldName)
 121   
     {
 122  1335
         BodyBuilder cleanupBody = new BodyBuilder();
 123   
 
 124   
         // Cached is only set when the field is updated in the accessor or mutator.
 125   
         // After rendering, we want to clear the cached value and cached flag
 126   
         // unless the binding is invariant, in which case it can stick around
 127   
         // for some future render.
 128   
 
 129  1335
         String bindingName = propertyName + "Binding";
 130   
 
 131  1335
         addBindingReference(cleanupBody, bindingName, parameterName);
 132   
 
 133  1335
         cleanupBody.addln("if ({0} && ! {1}.isInvariant())", cachedFieldName, bindingName);
 134  1335
         cleanupBody.begin();
 135  1335
         cleanupBody.addln("{0} = false;", cachedFieldName);
 136  1335
         cleanupBody.addln("{0} = {1};", fieldName, defaultFieldName);
 137  1335
         cleanupBody.end();
 138   
 
 139  1335
         op.extendMethodImplementation(
 140   
                 IComponent.class,
 141   
                 EnhanceUtils.CLEANUP_AFTER_RENDER_SIGNATURE,
 142   
                 cleanupBody.toString());
 143   
     }
 144   
 
 145  4006
     private void addBindingReference(BodyBuilder builder, String localVariableName,
 146   
             String parameterName)
 147   
     {
 148  4006
         builder.addln(
 149   
                 "{0} {1} = getBinding(\"{2}\");",
 150   
                 IBinding.class.getName(),
 151   
                 localVariableName,
 152   
                 parameterName);
 153   
     }
 154   
 
 155  1335
     private void buildMutator(EnhancementOperation op, String parameterName, String propertyName,
 156   
             Class propertyType, String fieldName, String defaultFieldName, String cachedFieldName)
 157   
     {
 158  1335
         BodyBuilder builder = new BodyBuilder();
 159  1335
         builder.begin();
 160   
 
 161   
         // The mutator method may be invoked from finishLoad(), in which
 162   
         // case it changes the default value for the parameter property, if the parameter
 163   
         // is not bound.
 164   
 
 165  1335
         builder.addln("if (! isInActiveState())");
 166  1335
         builder.begin();
 167  1335
         builder.addln("{0} = $1;", defaultFieldName);
 168  1335
         builder.addln("return;");
 169  1335
         builder.end();
 170   
 
 171   
         // In the normal state, we update the binding firstm, and it's an error
 172   
         // if the parameter is not bound.
 173   
 
 174  1335
         addBindingReference(builder, "binding", parameterName);
 175   
 
 176  1335
         builder.addln("if (binding == null)");
 177  1335
         builder.addln(
 178   
                 "  throw new {0}(\"Parameter ''{1}'' is not bound and can not be updated.\");",
 179   
                 ApplicationRuntimeException.class.getName(),
 180   
                 parameterName);
 181   
 
 182   
         // Always updated the binding first (which may fail with an exception).
 183   
 
 184  1335
         builder.addln("binding.setObject(($w) $1);");
 185   
 
 186   
         // While rendering, we store the updated value for fast
 187   
         // access again (while the component is still rendering).
 188   
         // The property value will be reset to default by cleanupAfterRender().
 189   
 
 190  1335
         builder.addln("if (isRendering())");
 191  1335
         builder.begin();
 192  1335
         builder.addln("{0} = $1;", fieldName);
 193  1335
         builder.addln("{0} = true;", cachedFieldName);
 194  1335
         builder.end();
 195   
 
 196  1335
         builder.end();
 197   
 
 198  1335
         String mutatorMethodName = EnhanceUtils.createMutatorMethodName(propertyName);
 199   
 
 200  1335
         op.addMethod(Modifier.PUBLIC, new MethodSignature(void.class, mutatorMethodName,
 201   
                 new Class[]
 202   
                 { propertyType }, null), builder.toString());
 203   
     }
 204   
 
 205   
     // Package private for testing
 206   
 
 207  1336
     void buildAccessor(EnhancementOperation op, String parameterName, String propertyName,
 208   
             Class propertyType, String fieldName, String defaultFieldName, String cachedFieldName)
 209   
     {
 210  1336
         BodyBuilder builder = new BodyBuilder();
 211  1336
         builder.begin();
 212   
 
 213  1336
         builder.addln("if ({0}) return {1};", cachedFieldName, fieldName);
 214   
 
 215  1336
         addBindingReference(builder, "binding", parameterName);
 216   
 
 217  1336
         builder.addln("if (binding == null) return {0};", defaultFieldName);
 218   
 
 219  1336
         String javaTypeName = ClassFabUtils.getJavaClassName(propertyType);
 220   
 
 221  1336
         builder.addln("{0} result = {1};", javaTypeName, 
 222   
                 EnhanceUtils.createUnwrapExpression(
 223   
                 op,
 224   
                 "binding",
 225   
                 propertyType));
 226   
 
 227   
         // Values read via the binding are cached during the render of
 228   
         // the component, or when the binding is invariant
 229   
         // (such as a StringBinding or MessageBinding).
 230   
 
 231  1336
         builder.addln("if (isRendering() || binding.isInvariant())");
 232  1336
         builder.begin();
 233  1336
         builder.addln("{0} = result;", fieldName);
 234  1336
         builder.addln("{0} = true;", cachedFieldName);
 235  1336
         builder.end();
 236   
 
 237  1336
         builder.addln("return result;");
 238   
 
 239  1336
         builder.end();
 240   
 
 241  1336
         String accessorMethodName = op.getAccessorMethodName(propertyName);
 242   
 
 243  1336
         op.addMethod(Modifier.PUBLIC, new MethodSignature(propertyType, accessorMethodName, null,
 244   
                 null), builder.toString());
 245   
     }
 246   
 
 247  53
     public void setErrorLog(ErrorLog errorLog)
 248   
     {
 249  53
         _errorLog = errorLog;
 250   
     }
 251   
 }