Coverage Report - org.apache.tapestry.enhance.SpecifiedPropertyWorker
 
Classes in this File Line Coverage Branch Coverage Complexity
SpecifiedPropertyWorker
0%
0/64
0%
0/20
1.5
 
 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 org.apache.hivemind.ErrorLog;
 18  
 import org.apache.hivemind.Location;
 19  
 import org.apache.hivemind.service.BodyBuilder;
 20  
 import org.apache.hivemind.service.MethodSignature;
 21  
 import org.apache.hivemind.util.Defense;
 22  
 import org.apache.tapestry.IBinding;
 23  
 import org.apache.tapestry.IComponent;
 24  
 import org.apache.tapestry.binding.BindingSource;
 25  
 import org.apache.tapestry.event.PageDetachListener;
 26  
 import org.apache.tapestry.spec.IComponentSpecification;
 27  
 import org.apache.tapestry.spec.IPropertySpecification;
 28  
 
 29  
 import java.lang.reflect.Modifier;
 30  
 import java.util.Iterator;
 31  
 
 32  
 /**
 33  
  * Responsible for adding properties to a class corresponding to specified
 34  
  * properties in the component's specification - which may come from .jwc / .page specifications
 35  
  * or annotated abstract methods.
 36  
  * 
 37  
  * @author Howard M. Lewis Ship
 38  
  * @since 4.0
 39  
  */
 40  0
 public class SpecifiedPropertyWorker implements EnhancementWorker
 41  
 {
 42  
     private ErrorLog _errorLog;
 43  
 
 44  
     private BindingSource _bindingSource;
 45  
     
 46  
     /**
 47  
      * Iterates over the specified properties, creating an enhanced property for
 48  
      * each (a field, an accessor, a mutator). Persistent properties will invoke
 49  
      * {@link org.apache.tapestry.Tapestry#fireObservedChange(IComponent, String, Object)}in
 50  
      * thier mutator.
 51  
      */
 52  
 
 53  
     public void performEnhancement(EnhancementOperation op, IComponentSpecification spec)
 54  
     {
 55  0
         Iterator i = spec.getPropertySpecificationNames().iterator();
 56  
 
 57  0
         while(i.hasNext())
 58  
         {
 59  0
             String name = (String) i.next();
 60  0
             IPropertySpecification ps = spec.getPropertySpecification(name);
 61  
             
 62  
             try
 63  
             {
 64  0
                 performEnhancement(op, ps);
 65  
             }
 66  0
             catch (RuntimeException ex)
 67  
             {
 68  0
                 _errorLog.error(EnhanceMessages.errorAddingProperty(name, op
 69  
                         .getBaseClass(), ex), ps.getLocation(), ex);
 70  0
             }
 71  0
         }
 72  0
     }
 73  
 
 74  
     private void performEnhancement(EnhancementOperation op,
 75  
             IPropertySpecification ps)
 76  
     {
 77  0
         Defense.notNull(ps, "ps");
 78  
 
 79  0
         String propertyName = ps.getName();
 80  0
         String specifiedType = ps.getType();
 81  0
         boolean persistent = ps.isPersistent();
 82  0
         String initialValue = ps.getInitialValue();
 83  0
         Location location = ps.getLocation();
 84  
         
 85  0
         addProperty(op, propertyName, specifiedType, persistent, initialValue, location, ps);
 86  0
     }
 87  
 
 88  
     public void addProperty(EnhancementOperation op, String propertyName, String specifiedType, 
 89  
             boolean persistent, String initialValue, Location location, IPropertySpecification ps)
 90  
     {
 91  0
         Class propertyType = EnhanceUtils.extractPropertyType(op, propertyName, specifiedType, ps.isGeneric());
 92  
 
 93  0
         op.claimProperty(propertyName);
 94  
 
 95  0
         String field = "_$" + propertyName;
 96  
 
 97  0
         op.addField(field, propertyType);
 98  
 
 99  
         // Release 3.0 would squack a bit about overriding non-abstract methods
 100  
         // if they exist. 4.0 is less picky ... it blindly adds new methods,
 101  
         // possibly
 102  
         // overwriting methods in the base component class.
 103  
         
 104  0
         EnhanceUtils.createSimpleAccessor(op, field, propertyName, propertyType, location);
 105  
                 
 106  0
         addMutator(op, propertyName, propertyType, field, persistent, location);
 107  
         
 108  0
         if (initialValue == null)
 109  0
             addReinitializer(op, propertyType, field);
 110  
         else 
 111  0
             addInitialValue(op, propertyName, propertyType, field, initialValue, persistent, location);
 112  0
     }
 113  
 
 114  
     private void addReinitializer(EnhancementOperation op, Class propertyType, String fieldName)
 115  
     {
 116  0
         String defaultFieldName = fieldName + "$default";
 117  
         
 118  0
         op.addField(defaultFieldName, propertyType);
 119  
         
 120  
         // On finishLoad(), store the current value into the default field.
 121  
 
 122  0
         op.extendMethodImplementation(IComponent.class, EnhanceUtils.FINISH_LOAD_SIGNATURE, 
 123  
                 defaultFieldName + " = " + fieldName + ";");
 124  
 
 125  
         // On pageDetach(), restore the attribute to its default value.
 126  
         
 127  0
         op.extendMethodImplementation(PageDetachListener.class,
 128  
                 EnhanceUtils.PAGE_DETACHED_SIGNATURE, fieldName + " = " + defaultFieldName + ";");
 129  0
     }
 130  
 
 131  
     private void addInitialValue(EnhancementOperation op, String propertyName, Class propertyType, 
 132  
             String fieldName, String initialValue, boolean persistent, Location location)
 133  
     {
 134  0
         String description = EnhanceMessages.initialValueForProperty(propertyName);
 135  
 
 136  0
         InitialValueBindingCreator creator = 
 137  
             new InitialValueBindingCreator(_bindingSource, description, initialValue, location);
 138  
         
 139  0
         String creatorField = op.addInjectedField(fieldName + "$initialValueBindingCreator", InitialValueBindingCreator.class, creator);
 140  
 
 141  0
         String bindingField = fieldName + "$initialValueBinding";
 142  0
         op.addField(bindingField, IBinding.class);
 143  
 
 144  0
         BodyBuilder builder = new BodyBuilder();
 145  
         
 146  0
         builder.addln("{0} = {1}.createBinding(this);", bindingField, creatorField);
 147  
 
 148  0
         op.extendMethodImplementation(IComponent.class, EnhanceUtils.FINISH_LOAD_SIGNATURE, builder.toString());
 149  
 
 150  0
         builder.clear();
 151  
         
 152  0
         builder.addln("{0} = {1};", fieldName, EnhanceUtils.createUnwrapExpression(op, bindingField, propertyType));
 153  
         
 154  0
         String code = builder.toString();
 155  
         
 156  
         // In finishLoad() and pageDetach(), de-reference the binding to get the
 157  
         // value
 158  
         // for the property.
 159  
 
 160  0
         op.extendMethodImplementation(IComponent.class, EnhanceUtils.FINISH_LOAD_SIGNATURE, code);
 161  
         
 162  0
         op.extendMethodImplementation(PageDetachListener.class, EnhanceUtils.PAGE_DETACHED_SIGNATURE, code);
 163  0
     }
 164  
 
 165  
     private void addMutator(EnhancementOperation op, String propertyName,
 166  
             Class propertyType, String fieldName, boolean persistent, Location location)
 167  
     {
 168  0
         String methodName = EnhanceUtils.createMutatorMethodName(propertyName);
 169  
 
 170  0
         BodyBuilder body = new BodyBuilder();
 171  
 
 172  0
         body.begin();
 173  
         
 174  0
         if (persistent) {
 175  
             
 176  0
             body.add("org.apache.tapestry.Tapestry#fireObservedChange(this, ");
 177  0
             body.addQuoted(propertyName);
 178  0
             body.addln(", ($w) $1);");
 179  
         }
 180  
 
 181  0
         body.addln(fieldName + " = $1;");
 182  0
         body.end();
 183  
         
 184  0
         MethodSignature sig = new MethodSignature(void.class, methodName, new Class[] { propertyType }, null);
 185  
 
 186  0
         op.addMethod(Modifier.PUBLIC, sig, body.toString(), location);
 187  0
     }
 188  
 
 189  
     public void setErrorLog(ErrorLog errorLog)
 190  
     {
 191  0
         _errorLog = errorLog;
 192  0
     }
 193  
 
 194  
     public void setBindingSource(BindingSource bindingSource)
 195  
     {
 196  0
         _bindingSource = bindingSource;
 197  0
     }
 198  
 }