Coverage Report - org.apache.tapestry5.internal.transform.PropertyWorker
 
Classes in this File Line Coverage Branch Coverage Complexity
PropertyWorker
100%
13/13
83%
5/6
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.annotations.Property;
 18  
 import org.apache.tapestry5.ioc.internal.util.InternalUtils;
 19  
 import org.apache.tapestry5.model.MutableComponentModel;
 20  
 import org.apache.tapestry5.services.ClassTransformation;
 21  
 import org.apache.tapestry5.services.ComponentClassTransformWorker;
 22  
 import org.apache.tapestry5.services.TransformMethodSignature;
 23  
 
 24  
 import java.lang.reflect.Modifier;
 25  
 
 26  
 /**
 27  
  * Provides the getter and setter methods. The methods are added as "existing", meaning that field access to them will
 28  
  * be transformed as necessary by other annotations. This worker needs to be scheduled before any worker that might
 29  
  * delete a field.
 30  
  *
 31  
  * @see org.apache.tapestry5.annotations.Property
 32  
  */
 33  58
 public class PropertyWorker implements ComponentClassTransformWorker
 34  
 {
 35  
     public void transform(ClassTransformation transformation, MutableComponentModel model)
 36  
     {
 37  812
         for (String fieldName : transformation.findFieldsWithAnnotation(Property.class))
 38  
         {
 39  230
             Property annotation = transformation.getFieldAnnotation(fieldName, Property.class);
 40  
 
 41  230
             String propertyName = InternalUtils.capitalize(InternalUtils.stripMemberName(fieldName));
 42  
 
 43  230
             String fieldType = transformation.getFieldType(fieldName);
 44  
 
 45  230
             if (annotation.read())
 46  
             {
 47  230
                 TransformMethodSignature getter
 48  
                         = new TransformMethodSignature(Modifier.PUBLIC | Modifier.FINAL, fieldType,
 49  
                                                        "get" + propertyName,
 50  
                                                        null, null);
 51  
 
 52  230
                 transformation.addTransformedMethod(getter, "return " + fieldName + ";");
 53  
             }
 54  
 
 55  228
             if (annotation.write())
 56  
             {
 57  156
                 TransformMethodSignature setter
 58  
                         = new TransformMethodSignature(Modifier.PUBLIC | Modifier.FINAL, "void", "set" + propertyName,
 59  
                                                        new String[] {fieldType}, null);
 60  
 
 61  156
                 transformation.addTransformedMethod(setter, fieldName + " = $1;");
 62  
             }
 63  
 
 64  
             // The field is NOT claimed, because we want annotation for the fields to operate.
 65  228
         }
 66  810
     }
 67  
 }