Coverage Report - org.apache.tapestry5.internal.transform.EnvironmentalWorker
 
Classes in this File Line Coverage Branch Coverage Complexity
EnvironmentalWorker
94%
29/31
100%
6/6
0
 
 1  
 // Copyright 2006, 2007, 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.annotations.Environmental;
 18  
 import org.apache.tapestry5.ioc.services.Builtin;
 19  
 import org.apache.tapestry5.ioc.services.ClassFactory;
 20  
 import org.apache.tapestry5.model.MutableComponentModel;
 21  
 import org.apache.tapestry5.services.*;
 22  
 
 23  
 import java.lang.reflect.Modifier;
 24  
 import java.util.List;
 25  
 
 26  
 /**
 27  
  * Obtains a value from the {@link Environment} service based on the field type. This is triggered by the presence of
 28  
  * the {@link Environmental} annotation.
 29  
  */
 30  
 public class EnvironmentalWorker implements ComponentClassTransformWorker
 31  
 {
 32  
     private final Environment environment;
 33  
 
 34  
     private final ClassLoader classLoader;
 35  
 
 36  
     public EnvironmentalWorker(Environment environment, @Builtin ClassFactory servicesLayerClassFactory)
 37  58
     {
 38  58
         this.environment = environment;
 39  
 
 40  58
         classLoader = servicesLayerClassFactory.getClassLoader();
 41  58
     }
 42  
 
 43  
     public void transform(ClassTransformation transformation, MutableComponentModel model)
 44  
     {
 45  810
         List<String> names = transformation.findFieldsWithAnnotation(Environmental.class);
 46  
 
 47  810
         if (names.isEmpty())
 48  646
             return;
 49  
 
 50  
         // TODO: addInjectField should be smart about if the field has already been injected (with
 51  
         // the same type)
 52  
         // for this transformation, or the parent transformation.
 53  
 
 54  164
         String envField = transformation.addInjectedField(
 55  
                 Environment.class,
 56  
                 "environment",
 57  
                 environment);
 58  
 
 59  164
         for (String name : names)
 60  
         {
 61  296
             Environmental annotation = transformation.getFieldAnnotation(name, Environmental.class);
 62  
 
 63  296
             transformation.claimField(name, annotation);
 64  
 
 65  296
             String typeName = transformation.getFieldType(name);
 66  
 
 67  
             // TODO: Check for primitives
 68  
 
 69  
             // TAP5-417: Calls to javassist.runtime.Desc.getType() are showing up as method hot spots.
 70  
 
 71  296
             Class type = null;
 72  
 
 73  
             try
 74  
             {
 75  296
                 type = classLoader.loadClass(typeName);
 76  
             }
 77  0
             catch (ClassNotFoundException ex)
 78  
             {
 79  0
                 throw new RuntimeException(ex);
 80  296
             }
 81  
 
 82  
             // TAP5-417: Changed the code to use EnvironmentalAccess, which encapsulates
 83  
             // efficient caching.
 84  
 
 85  296
             String injectedTypeFieldName = transformation.addInjectedField(Class.class, "type", type);
 86  
 
 87  
             // First we need (at page attach) to acquire the closure for the type.
 88  
 
 89  296
             String accessFieldName = transformation.addField(Modifier.PRIVATE, EnvironmentalAccess.class.getName(),
 90  
                                                              name + "_access");
 91  
 
 92  296
             String attachBody = String.format("%s = %s.getAccess(%s);",
 93  
                                               accessFieldName, envField, injectedTypeFieldName);
 94  
 
 95  296
             transformation.extendMethod(TransformConstants.CONTAINING_PAGE_DID_ATTACH_SIGNATURE, attachBody);
 96  
 
 97  
             // Clear the closure field when the page detaches.  We'll get a new one when we next attach.
 98  
 
 99  296
             transformation.extendMethod(TransformConstants.CONTAINING_PAGE_DID_DETACH_SIGNATURE,
 100  
                                         accessFieldName + " = null;");
 101  
 
 102  
             // Now build a read method that invokes peek() or peekRequired() on the closure. The closure
 103  
             // is responsible for safe caching of the environmental value.
 104  
 
 105  296
             String methodName = transformation.newMemberName("environment_read", name);
 106  
 
 107  296
             TransformMethodSignature sig = new TransformMethodSignature(Modifier.PRIVATE, typeName, methodName, null,
 108  
                                                                         null);
 109  
 
 110  296
             String body = String.format(
 111  
                     "return ($r) %s.%s();",
 112  
                     accessFieldName,
 113  
                     annotation.value() ? "peekRequired" : "peek");
 114  
 
 115  296
             transformation.addMethod(sig, body);
 116  
 
 117  296
             transformation.replaceReadAccess(name, methodName);
 118  296
             transformation.makeReadOnly(name);
 119  296
             transformation.removeField(name);
 120  296
         }
 121  164
     }
 122  
 }