Coverage Report - org.apache.tapestry5.internal.transform.CachedWorker
 
Classes in this File Line Coverage Branch Coverage Complexity
CachedWorker
100%
49/49
100%
18/18
0
 
 1  
 // Copyright 2008 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.Binding;
 18  
 import org.apache.tapestry5.BindingConstants;
 19  
 import org.apache.tapestry5.annotations.Cached;
 20  
 import org.apache.tapestry5.ioc.util.BodyBuilder;
 21  
 import org.apache.tapestry5.model.MutableComponentModel;
 22  
 import org.apache.tapestry5.services.*;
 23  
 
 24  
 import static java.lang.reflect.Modifier.PRIVATE;
 25  
 import java.util.List;
 26  
 
 27  
 /**
 28  
  * Caches method return values for methods annotated with {@link Cached}.
 29  
  */
 30  
 public class CachedWorker implements ComponentClassTransformWorker
 31  
 {
 32  
     private final BindingSource bindingSource;
 33  
 
 34  
     public CachedWorker(BindingSource bindingSource)
 35  62
     {
 36  62
         this.bindingSource = bindingSource;
 37  62
     }
 38  
 
 39  
     public void transform(ClassTransformation transformation, MutableComponentModel model)
 40  
     {
 41  820
         List<TransformMethodSignature> methods = transformation.findMethodsWithAnnotation(Cached.class);
 42  820
         if (methods.isEmpty())
 43  812
             return;
 44  
 
 45  8
         for (TransformMethodSignature method : methods)
 46  
         {
 47  12
             if (method.getReturnType().equals("void"))
 48  2
                 throw new IllegalArgumentException(TransformMessages.cachedMethodMustHaveReturnValue(method));
 49  
 
 50  10
             if (method.getParameterTypes().length != 0)
 51  2
                 throw new IllegalArgumentException(TransformMessages.cachedMethodsHaveNoParameters(method));
 52  
 
 53  8
             String propertyName = method.getMethodName();
 54  
 
 55  
             // add a property to store whether or not the method has been called
 56  8
             String fieldName = transformation.addField(PRIVATE, method.getReturnType(), propertyName);
 57  8
             String calledField = transformation.addField(PRIVATE, "boolean", fieldName + "$called");
 58  
 
 59  8
             Cached once = transformation.getMethodAnnotation(method, Cached.class);
 60  8
             String bindingField = null;
 61  8
             String bindingValueField = null;
 62  8
             boolean watching = once.watch().length() > 0;
 63  
 
 64  8
             if (watching)
 65  
             {
 66  
                 // add fields to store the binding and the value
 67  2
                 bindingField = transformation.addField(PRIVATE, Binding.class.getCanonicalName(),
 68  
                                                        fieldName + "$binding");
 69  2
                 bindingValueField = transformation.addField(PRIVATE, "java.lang.Object", fieldName + "$bindingValue");
 70  
 
 71  2
                 String bindingSourceField = transformation.addInjectedField(BindingSource.class,
 72  
                                                                             fieldName + "$bindingsource",
 73  
                                                                             bindingSource);
 74  
 
 75  2
                 String body = String.format("%s = %s.newBinding(\"Watch expression\", %s, \"%s\", \"%s\");",
 76  
                                             bindingField,
 77  
                                             bindingSourceField,
 78  
                                             transformation.getResourcesFieldName(),
 79  
                                             BindingConstants.PROP,
 80  
                                             once.watch());
 81  
 
 82  2
                 transformation.extendMethod(TransformConstants.CONTAINING_PAGE_DID_LOAD_SIGNATURE, body);
 83  
             }
 84  
 
 85  8
             BodyBuilder b = new BodyBuilder();
 86  
 
 87  
             // on cleanup, reset the field values
 88  8
             b.begin();
 89  
 
 90  8
             if (!TransformUtils.isPrimitive(method.getReturnType()))
 91  2
                 b.addln("%s = null;", fieldName);
 92  8
             b.addln("%s = false;", calledField);
 93  
 
 94  8
             if (watching)
 95  2
                 b.addln("%s = null;", bindingValueField);
 96  
 
 97  8
             b.end();
 98  
 
 99  
             // TAPESTRY-2338: Cleanup at page detach, not render cleanup.  In an Ajax request, the rendering
 100  
             // objects may reference properties of components that don't render and so won't execute the
 101  
             // PostCleanupRender phase.
 102  
 
 103  8
             transformation.extendMethod(TransformConstants.CONTAINING_PAGE_DID_DETACH_SIGNATURE, b.toString());
 104  
 
 105  
             // prefix the existing method to cache the result
 106  8
             b.clear();
 107  8
             b.begin();
 108  
 
 109  
             // if it has been called and watch is set and the old value is the same as the new value then return
 110  
             // get the old value and cache it
 111  
             /* NOTE: evaluates the binding twice when checking the new value.
 112  
                 * this is probably not a problem because in most cases properties
 113  
                 * that are being watched are not expensive operations. plus, we
 114  
                 * never guaranteed that it would be called exactly once when
 115  
                 * watching.
 116  
                 */
 117  8
             if (watching)
 118  
             {
 119  2
                 b.addln("if (%s && %s == %s.get()) return %s;",
 120  
                         calledField, bindingValueField, bindingField, fieldName);
 121  2
                 b.addln("%s = %s.get();", bindingValueField, bindingField);
 122  
             }
 123  
             else
 124  
             {
 125  6
                 b.addln("if (%s) return %s;", calledField, fieldName);
 126  
             }
 127  
 
 128  8
             b.addln("%s = true;", calledField);
 129  8
             b.end();
 130  8
             transformation.prefixMethod(method, b.toString());
 131  
 
 132  
             // cache the return value
 133  8
             b.clear();
 134  8
             b.begin();
 135  8
             b.addln("%s = $_;", fieldName);
 136  8
             b.end();
 137  8
             transformation.extendExistingMethod(method, b.toString());
 138  8
         }
 139  4
     }
 140  
 }