Coverage Report - org.apache.tapestry5.internal.transform.UnclaimedFieldWorker
 
Classes in this File Line Coverage Branch Coverage Complexity
UnclaimedFieldWorker
100%
13/13
100%
4/4
0
 
 1  
 // Copyright 2006, 2007 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.model.MutableComponentModel;
 18  
 import org.apache.tapestry5.services.ClassTransformation;
 19  
 import org.apache.tapestry5.services.ComponentClassTransformWorker;
 20  
 import static org.apache.tapestry5.services.TransformConstants.CONTAINING_PAGE_DID_DETACH_SIGNATURE;
 21  
 import static org.apache.tapestry5.services.TransformConstants.CONTAINING_PAGE_DID_LOAD_SIGNATURE;
 22  
 
 23  
 import java.lang.reflect.Modifier;
 24  
 import java.util.List;
 25  
 
 26  
 /**
 27  
  * Designed to be just about the last worker in the pipeline. Its job is to add cleanup code that restores transient
 28  
  * fields back to their initial (null) value. Fields that have been previously {@link
 29  
  * org.apache.tapestry5.services.ClassTransformation#claimField(String, Object) claimed} are ignored, as are fields that
 30  
  * are final.
 31  
  */
 32  64
 public final class UnclaimedFieldWorker implements ComponentClassTransformWorker
 33  
 {
 34  
 
 35  
     public void transform(ClassTransformation transformation, MutableComponentModel model)
 36  
     {
 37  816
         List<String> fieldNames = transformation.findUnclaimedFields();
 38  
 
 39  816
         for (String fieldName : fieldNames)
 40  
         {
 41  588
             transformField(fieldName, transformation);
 42  
         }
 43  816
     }
 44  
 
 45  
     private void transformField(String fieldName, ClassTransformation transformation)
 46  
     {
 47  588
         int modifiers = transformation.getFieldModifiers(fieldName);
 48  
 
 49  588
         if (Modifier.isFinal(modifiers))
 50  22
             return;
 51  
 
 52  566
         String type = transformation.getFieldType(fieldName);
 53  
 
 54  566
         String defaultFieldName = transformation.addField(Modifier.PRIVATE, type, fieldName
 55  
                 + "_default");
 56  
 
 57  566
         transformation.extendMethod(CONTAINING_PAGE_DID_LOAD_SIGNATURE, defaultFieldName + " = "
 58  
                 + fieldName + ";");
 59  
 
 60  
         // At the end of the request, we want to move the default value back over the
 61  
         // active field value. This will most often be null.
 62  
 
 63  566
         transformation.extendMethod(CONTAINING_PAGE_DID_DETACH_SIGNATURE, fieldName + " = "
 64  
                 + defaultFieldName + ";");
 65  566
     }
 66  
 }