Coverage Report - org.apache.tapestry5.internal.transform.PersistWorker
 
Classes in this File Line Coverage Branch Coverage Complexity
PersistWorker
100%
31/31
100%
4/4
0
 
 1  
 // Copyright 2006, 2007, 2008, 2009 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.tapestry5.internal.transform;
 16  
 
 17  
 import org.apache.tapestry5.annotations.Persist;
 18  
 import org.apache.tapestry5.ioc.util.BodyBuilder;
 19  
 import org.apache.tapestry5.model.MutableComponentModel;
 20  
 import org.apache.tapestry5.services.*;
 21  
 
 22  
 import java.lang.reflect.Modifier;
 23  
 
 24  
 /**
 25  
  * Converts fields with the {@link org.apache.tapestry5.annotations.Persist} annotation into persistent fields.
 26  
  */
 27  58
 public class PersistWorker implements ComponentClassTransformWorker
 28  
 {
 29  
 
 30  
     public void transform(ClassTransformation transformation, MutableComponentModel model)
 31  
     {
 32  812
         for (String name : transformation.findFieldsWithAnnotation(Persist.class))
 33  
         {
 34  130
             makeFieldPersistent(name, transformation, model);
 35  
         }
 36  812
     }
 37  
 
 38  
     /**
 39  
      * Making a field persistent: <ul> <li>Need a secondary default field that stores the initial value</li> <li>Store
 40  
      * the active value into the default field when the page finishes loading</li> <li>Roll the active value back to the
 41  
      * default when the page detaches</li> <ii>On changes to the active field, post the change via the {@link
 42  
      * org.apache.tapestry5.internal.InternalComponentResources} </li> <li>When the page attaches, pull the persisted
 43  
      * value for the field out of the {@link org.apache.tapestry5.services.PersistentFieldBundle}</li> </ul>
 44  
      *
 45  
      * @see org.apache.tapestry5.runtime.PageLifecycleListener#restoreStateBeforePageAttach()
 46  
      */
 47  
     private void makeFieldPersistent(String fieldName, ClassTransformation transformation,
 48  
                                      MutableComponentModel model)
 49  
     {
 50  130
         String fieldType = transformation.getFieldType(fieldName);
 51  130
         Persist annotation = transformation.getFieldAnnotation(fieldName, Persist.class);
 52  
 
 53  130
         transformation.claimField(fieldName, annotation);
 54  
 
 55  
         // Record the type of persistence, until needed later.
 56  
 
 57  130
         String logicalFieldName = model.setFieldPersistenceStrategy(fieldName, annotation.value());
 58  
 
 59  130
         String defaultValue = TransformUtils.getDefaultValue(fieldType);
 60  
 
 61  
         // Force the field back to its default value (null, 0, false) at the end of each request.
 62  
 
 63  130
         transformation.extendMethod(
 64  
                 TransformConstants.CONTAINING_PAGE_DID_DETACH_SIGNATURE,
 65  
                 String.format("%s = %s;", fieldName, defaultValue));
 66  
 
 67  130
         String resourcesFieldName = transformation.getResourcesFieldName();
 68  
 
 69  130
         String writeMethodName = transformation.newMemberName("write", fieldName);
 70  
 
 71  130
         BodyBuilder builder = new BodyBuilder();
 72  
 
 73  130
         builder.begin();
 74  130
         builder.addln(
 75  
                 "%s.persistFieldChange(\"%s\", ($w) $1);",
 76  
                 resourcesFieldName,
 77  
                 logicalFieldName);
 78  130
         builder.addln("%s = $1;", fieldName);
 79  130
         builder.end();
 80  
 
 81  130
         transformation.addMethod(new TransformMethodSignature(Modifier.PRIVATE, "void", writeMethodName,
 82  
                                                               new String[]
 83  
                                                                       { fieldType }, null), builder.toString());
 84  
 
 85  130
         transformation.replaceWriteAccess(fieldName, writeMethodName);
 86  
 
 87  130
         builder.clear();
 88  130
         builder.begin();
 89  
 
 90  
         // Check to see if there's a recorded change for this component, this field.
 91  
 
 92  130
         builder.addln("if (%s.hasFieldChange(\"%s\"))", resourcesFieldName, logicalFieldName);
 93  
 
 94  130
         String wrapperType = TransformUtils.getWrapperTypeName(fieldType);
 95  
 
 96  
         // Get the value, cast it to the correct type (or wrapper type)
 97  130
         builder.add(
 98  
                 "  %s = ((%s) %s.getFieldChange(\"%s\"))",
 99  
                 fieldName,
 100  
                 wrapperType,
 101  
                 resourcesFieldName,
 102  
                 logicalFieldName);
 103  
 
 104  
         // For primtive types, add in the method call to unwrap the wrapper type to a primitive type
 105  
 
 106  130
         String unwrapMethodName = TransformUtils.getUnwrapperMethodName(fieldType);
 107  
 
 108  130
         if (unwrapMethodName == null)
 109  106
             builder.addln(";");
 110  
         else
 111  24
             builder.addln(".%s();", unwrapMethodName);
 112  
 
 113  130
         builder.end();
 114  
 
 115  130
         transformation.extendMethod(
 116  
                 TransformConstants.RESTORE_STATE_BEFORE_PAGE_ATTACH_SIGNATURE,
 117  
                 builder.toString());
 118  130
     }
 119  
 }