Clover coverage report - Code Coverage for tapestry release 4.0-alpha-3
Coverage timestamp: Mon May 16 2005 09:05:49 EDT
file stats: LOC: 256   Methods: 7
NCLOC: 156   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 4.0
 35   
  */
 36   
 public class ParameterPropertyWorker implements EnhancementWorker
 37   
 {
 38   
     private ErrorLog _errorLog;
 39   
 
 40  465
     public void performEnhancement(EnhancementOperation op, IComponentSpecification spec)
 41   
     {
 42  465
         Iterator i = spec.getParameterNames().iterator();
 43  465
         while (i.hasNext())
 44   
         {
 45  1130
             String name = (String) i.next();
 46   
 
 47  1130
             IParameterSpecification ps = spec.getParameter(name);
 48   
 
 49  1130
             try
 50   
             {
 51  1130
                 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   
      * 4.0 but for the moment we're emulating 3.0 behavior.
 64   
      */
 65   
 
 66  1130
     private void performEnhancement(EnhancementOperation op, String parameterName,
 67   
             IParameterSpecification ps)
 68   
     {
 69  1130
         String propertyName = ps.getPropertyName();
 70   
 
 71  1130
         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 4.0 and an existing
 75   
         // property will be overwritten in the subclass.
 76   
 
 77  1129
         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  1129
         String fieldName = "_$" + propertyName;
 83  1129
         String defaultFieldName = fieldName + "$Default";
 84  1129
         String cachedFieldName = fieldName + "$Cached";
 85   
 
 86  1129
         op.addField(fieldName, propertyType);
 87  1129
         op.addField(defaultFieldName, propertyType);
 88  1129
         op.addField(cachedFieldName, boolean.class);
 89   
 
 90  1129
         buildAccessor(
 91   
                 op,
 92   
                 parameterName,
 93   
                 propertyName,
 94   
                 propertyType,
 95   
                 fieldName,
 96   
                 defaultFieldName,
 97   
                 cachedFieldName,
 98   
                 ps.getCache());
 99   
 
 100  1129
         buildMutator(
 101   
                 op,
 102   
                 parameterName,
 103   
                 propertyName,
 104   
                 propertyType,
 105   
                 fieldName,
 106   
                 defaultFieldName,
 107   
                 cachedFieldName);
 108   
 
 109  1129
         extendCleanupAfterRender(
 110   
                 op,
 111   
                 parameterName,
 112   
                 propertyName,
 113   
                 propertyType,
 114   
                 fieldName,
 115   
                 defaultFieldName,
 116   
                 cachedFieldName);
 117   
     }
 118   
 
 119  1129
     private void extendCleanupAfterRender(EnhancementOperation op, String parameterName,
 120   
             String propertyName, Class propertyType, String fieldName, String defaultFieldName,
 121   
             String cachedFieldName)
 122   
     {
 123  1129
         BodyBuilder cleanupBody = new BodyBuilder();
 124   
 
 125   
         // Cached is only set when the field is updated in the accessor or mutator.
 126   
         // After rendering, we want to clear the cached value and cached flag
 127   
         // unless the binding is invariant, in which case it can stick around
 128   
         // for some future render.
 129   
 
 130  1129
         String bindingName = propertyName + "Binding";
 131   
 
 132  1129
         addBindingReference(cleanupBody, bindingName, parameterName);
 133   
 
 134  1129
         cleanupBody.addln("if ({0} && ! {1}.isInvariant())", cachedFieldName, bindingName);
 135  1129
         cleanupBody.begin();
 136  1129
         cleanupBody.addln("{0} = false;", cachedFieldName);
 137  1129
         cleanupBody.addln("{0} = {1};", fieldName, defaultFieldName);
 138  1129
         cleanupBody.end();
 139   
 
 140  1129
         op.extendMethodImplementation(
 141   
                 IComponent.class,
 142   
                 EnhanceUtils.CLEANUP_AFTER_RENDER_SIGNATURE,
 143   
                 cleanupBody.toString());
 144   
     }
 145   
 
 146  3389
     private void addBindingReference(BodyBuilder builder, String localVariableName,
 147   
             String parameterName)
 148   
     {
 149  3389
         builder.addln(
 150   
                 "{0} {1} = getBinding(\"{2}\");",
 151   
                 IBinding.class.getName(),
 152   
                 localVariableName,
 153   
                 parameterName);
 154   
     }
 155   
 
 156  1129
     private void buildMutator(EnhancementOperation op, String parameterName, String propertyName,
 157   
             Class propertyType, String fieldName, String defaultFieldName, String cachedFieldName)
 158   
     {
 159  1129
         BodyBuilder builder = new BodyBuilder();
 160  1129
         builder.begin();
 161   
 
 162   
         // The mutator method may be invoked from finishLoad(), in which
 163   
         // case it changes the default value for the parameter property, if the parameter
 164   
         // is not bound.
 165   
 
 166  1129
         builder.addln("if (! isInActiveState())");
 167  1129
         builder.begin();
 168  1129
         builder.addln("{0} = $1;", defaultFieldName);
 169  1129
         builder.addln("return;");
 170  1129
         builder.end();
 171   
 
 172   
         // In the normal state, we update the binding firstm, and it's an error
 173   
         // if the parameter is not bound.
 174   
 
 175  1129
         addBindingReference(builder, "binding", parameterName);
 176   
 
 177  1129
         builder.addln("if (binding == null)");
 178  1129
         builder.addln(
 179   
                 "  throw new {0}(\"Parameter ''{1}'' is not bound and can not be updated.\");",
 180   
                 ApplicationRuntimeException.class.getName(),
 181   
                 parameterName);
 182   
 
 183   
         // Always updated the binding first (which may fail with an exception).
 184   
 
 185  1129
         builder.addln("binding.setObject(($w) $1);");
 186   
 
 187   
         // While rendering, we store the updated value for fast
 188   
         // access again (while the component is still rendering).
 189   
         // The property value will be reset to default by cleanupAfterRender().
 190   
 
 191  1129
         builder.addln("if (isRendering())");
 192  1129
         builder.begin();
 193  1129
         builder.addln("{0} = $1;", fieldName);
 194  1129
         builder.addln("{0} = true;", cachedFieldName);
 195  1129
         builder.end();
 196   
 
 197  1129
         builder.end();
 198   
 
 199  1129
         String mutatorMethodName = EnhanceUtils.createMutatorMethodName(propertyName);
 200   
 
 201  1129
         op.addMethod(Modifier.PUBLIC, new MethodSignature(void.class, mutatorMethodName,
 202   
                 new Class[]
 203   
                 { propertyType }, null), builder.toString());
 204   
     }
 205   
 
 206   
     // Package private for testing
 207   
 
 208  1131
     void buildAccessor(EnhancementOperation op, String parameterName, String propertyName,
 209   
             Class propertyType, String fieldName, String defaultFieldName, String cachedFieldName,
 210   
             boolean cache)
 211   
     {
 212  1131
         BodyBuilder builder = new BodyBuilder();
 213  1131
         builder.begin();
 214   
 
 215  1131
         builder.addln("if ({0}) return {1};", cachedFieldName, fieldName);
 216   
 
 217  1131
         addBindingReference(builder, "binding", parameterName);
 218   
 
 219  1131
         builder.addln("if (binding == null) return {0};", defaultFieldName);
 220   
 
 221  1131
         String javaTypeName = ClassFabUtils.getJavaClassName(propertyType);
 222   
 
 223  1131
         builder.addln("{0} result = {1};", javaTypeName, EnhanceUtils.createUnwrapExpression(
 224   
                 op,
 225   
                 "binding",
 226   
                 propertyType));
 227   
 
 228   
         // Values read via the binding are cached during the render of
 229   
         // the component (if the parameter defines cache to be true, which
 230   
         // is the default), or any time the binding is invariant
 231   
         // (such as most bindings besides ExpressionBinding.
 232   
 
 233  1131
         String expression = cache ? "isRendering() || binding.isInvariant()"
 234   
                 : "binding.isInvariant()";
 235   
 
 236  1131
         builder.addln("if ({0})", expression);
 237  1131
         builder.begin();
 238  1131
         builder.addln("{0} = result;", fieldName);
 239  1131
         builder.addln("{0} = true;", cachedFieldName);
 240  1131
         builder.end();
 241   
 
 242  1131
         builder.addln("return result;");
 243   
 
 244  1131
         builder.end();
 245   
 
 246  1131
         String accessorMethodName = op.getAccessorMethodName(propertyName);
 247   
 
 248  1131
         op.addMethod(Modifier.PUBLIC, new MethodSignature(propertyType, accessorMethodName, null,
 249   
                 null), builder.toString());
 250   
     }
 251   
 
 252  46
     public void setErrorLog(ErrorLog errorLog)
 253   
     {
 254  46
         _errorLog = errorLog;
 255   
     }
 256   
 }