Clover coverage report - Code Coverage for tapestry release 4.0-alpha-2
Coverage timestamp: Thu May 5 2005 09:57:44 EDT
file stats: LOC: 256   Methods: 16
NCLOC: 148   Classes: 1
30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover
 
 Source file Conditionals Statements Methods TOTAL
EnhanceUtils.java 100% 82.8% 68.8% 81.2%
coverage coverage
 1   
 // Copyright 2004, 2005 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.tapestry.enhance;
 16   
 
 17   
 import java.lang.reflect.Modifier;
 18   
 import java.util.HashMap;
 19   
 import java.util.Map;
 20   
 
 21   
 import org.apache.hivemind.service.ClassFabUtils;
 22   
 import org.apache.hivemind.service.MethodSignature;
 23   
 import org.apache.hivemind.util.Defense;
 24   
 import org.apache.tapestry.IBinding;
 25   
 import org.apache.tapestry.IRequestCycle;
 26   
 import org.apache.tapestry.engine.IPageLoader;
 27   
 import org.apache.tapestry.event.PageEvent;
 28   
 import org.apache.tapestry.spec.IComponentSpecification;
 29   
 
 30   
 /**
 31   
  * Convienience methods needed by various parts of the enhancement subsystem.
 32   
  * 
 33   
  * @author Howard M. Lewis Ship
 34   
  * @since 4.0
 35   
  */
 36   
 public class EnhanceUtils
 37   
 {
 38   
     public static final MethodSignature FINISH_LOAD_SIGNATURE = new MethodSignature(void.class,
 39   
             "finishLoad", new Class[]
 40   
             { IRequestCycle.class, IPageLoader.class, IComponentSpecification.class }, null);
 41   
 
 42   
     public static final MethodSignature PAGE_DETACHED_SIGNATURE = new MethodSignature(void.class,
 43   
             "pageDetached", new Class[]
 44   
             { PageEvent.class }, null);
 45   
 
 46   
     public static final MethodSignature CLEANUP_AFTER_RENDER_SIGNATURE = new MethodSignature(
 47   
             void.class, "cleanupAfterRender", new Class[]
 48   
             { IRequestCycle.class }, null);
 49   
 
 50  1491
     public static String createMutatorMethodName(String propertyName)
 51   
     {
 52  1491
         return "set" + upcase(propertyName);
 53   
     }
 54   
 
 55  518
     public static String createAccessorMethodName(String propertyName)
 56   
     {
 57  518
         return "get" + upcase(propertyName);
 58   
     }
 59   
 
 60  2009
     private static String upcase(String name)
 61   
     {
 62  2009
         return name.substring(0, 1).toUpperCase() + name.substring(1);
 63   
     }
 64   
 
 65  844
     public static void createSimpleAccessor(EnhancementOperation op, String fieldName,
 66   
             String propertyName, Class propertyType)
 67   
     {
 68  844
         String methodName = op.getAccessorMethodName(propertyName);
 69   
 
 70  844
         op.addMethod(
 71   
                 Modifier.PUBLIC,
 72   
                 new MethodSignature(propertyType, methodName, null, null),
 73   
                 "return " + fieldName + ";");
 74   
     }
 75   
 
 76  255
     public static void createSimpleMutator(EnhancementOperation op, String fieldName,
 77   
             String propertyName, Class propertyType)
 78   
     {
 79  255
         String methodName = createMutatorMethodName(propertyName);
 80   
 
 81  255
         op.addMethod(Modifier.PUBLIC, new MethodSignature(void.class, methodName, new Class[]
 82   
         { propertyType }, null), fieldName + " = $1;");
 83   
     }
 84   
 
 85   
     /**
 86   
      * Returns the correct class for a property to be enhanced into a class. If a type name is
 87   
      * non-null, then it is converted to a Class. If the class being enhanced defines a property,
 88   
      * then the type must be an exact match (this is largely a holdover from Tapestry 3.0, where the
 89   
      * type had to be provided in the specification). If the type name is null, then the value
 90   
      * returned is the type of the existing property (if such a property exists), or
 91   
      * java.lang.Object is no property exists.
 92   
      * 
 93   
      * @param op
 94   
      *            the enhancement operation, which provides most of this logic
 95   
      * @param propertyName
 96   
      *            the name of the property (the property may or may not exist)
 97   
      * @param definedTypeName
 98   
      *            the type indicated for the property, may be null to make the return value match
 99   
      *            the type of an existing property.
 100   
      */
 101   
 
 102  1331
     public static Class extractPropertyType(EnhancementOperation op, String propertyName,
 103   
             String definedTypeName)
 104   
     {
 105  1331
         Defense.notNull(op, "op");
 106  1331
         Defense.notNull(propertyName, "propertyName");
 107   
 
 108  1331
         if (definedTypeName != null)
 109   
         {
 110  109
             Class propertyType = op.convertTypeName(definedTypeName);
 111   
 
 112  107
             op.validateProperty(propertyName, propertyType);
 113   
 
 114  107
             return propertyType;
 115   
         }
 116   
 
 117  1222
         Class propertyType = op.getPropertyType(propertyName);
 118   
 
 119  1222
         return propertyType == null ? Object.class : propertyType;
 120   
     }
 121   
 
 122   
     // The following methods are actually invoked from fabricated methods in
 123   
     // enhanced classes.
 124   
 
 125  315
     public static boolean toBoolean(IBinding binding)
 126   
     {
 127  315
         Boolean wrapped = (Boolean) binding.getObject(Boolean.class);
 128   
 
 129  314
         return wrapped.booleanValue();
 130   
     }
 131   
 
 132  0
     public static byte toByte(IBinding binding)
 133   
     {
 134  0
         Byte wrapped = (Byte) binding.getObject(Byte.class);
 135   
 
 136  0
         return wrapped.byteValue();
 137   
     }
 138   
 
 139  0
     public static char toChar(IBinding binding)
 140   
     {
 141  0
         Character wrapped = (Character) binding.getObject(Character.class);
 142   
 
 143  0
         return wrapped.charValue();
 144   
     }
 145   
 
 146  0
     public static short toShort(IBinding binding)
 147   
     {
 148  0
         Short wrapped = (Short) binding.getObject(Short.class);
 149   
 
 150  0
         return wrapped.shortValue();
 151   
     }
 152   
 
 153  14
     public static int toInt(IBinding binding)
 154   
     {
 155  14
         Integer wrapped = (Integer) binding.getObject(Integer.class);
 156   
 
 157  14
         return wrapped.intValue();
 158   
     }
 159   
 
 160  0
     public static long toLong(IBinding binding)
 161   
     {
 162  0
         Long wrapped = (Long) binding.getObject(Long.class);
 163   
 
 164  0
         return wrapped.longValue();
 165   
     }
 166   
 
 167  0
     public static float toFloat(IBinding binding)
 168   
     {
 169  0
         Float wrapped = (Float) binding.getObject(Float.class);
 170   
 
 171  0
         return wrapped.floatValue();
 172   
     }
 173   
 
 174  2
     public static double toDouble(IBinding binding)
 175   
     {
 176  2
         Double wrapped = (Double) binding.getObject(Double.class);
 177   
 
 178  2
         return wrapped.doubleValue();
 179   
     }
 180   
 
 181   
     /**
 182   
      * Used to unwrap primitive types inside the accessor method. In each case, the binding is in a
 183   
      * variable named "binding", and {0} will be the actual type of the property. The Map is keyed
 184   
      * on the primtive type.
 185   
      */
 186   
 
 187   
     private static Map _unwrappers = new HashMap();
 188   
 
 189   
     static
 190   
     {
 191  1
         _unwrappers.put(boolean.class, "toBoolean");
 192  1
         _unwrappers.put(byte.class, "toByte");
 193  1
         _unwrappers.put(char.class, "toChar");
 194  1
         _unwrappers.put(short.class, "toShort");
 195  1
         _unwrappers.put(int.class, "toInt");
 196  1
         _unwrappers.put(long.class, "toLong");
 197  1
         _unwrappers.put(float.class, "toFloat");
 198  1
         _unwrappers.put(double.class, "toDouble");
 199   
     }
 200   
 
 201   
     /**
 202   
      * Returns the name of the static method, within EnhanceUtils, used to unwrap a binding to a
 203   
      * primitive type. Returns null if the type is not a primitve.
 204   
      */
 205   
 
 206  1141
     public static String getUnwrapperMethodName(Class type)
 207   
     {
 208  1141
         return (String) _unwrappers.get(type);
 209   
     }
 210   
 
 211   
     /**
 212   
      * Builds a Javassist expression for unwrapping a binding's value to a type (either primitive or
 213   
      * a class type).
 214   
      * 
 215   
      * @param op
 216   
      *            the enhancement operation
 217   
      * @param bindingName
 218   
      *            the name of the field (or an expression) that will evaluate to the binding from
 219   
      *            which a value will be extracted.
 220   
      * @param valueType
 221   
      *            the type of value to be extracted from the binding.
 222   
      */
 223   
 
 224  1141
     public static String createUnwrapExpression(EnhancementOperation op, String bindingName,
 225   
             Class valueType)
 226   
     {
 227  1141
         StringBuffer buffer = new StringBuffer();
 228   
 
 229  1141
         String unwrapper = getUnwrapperMethodName(valueType);
 230   
 
 231  1141
         if (unwrapper == null)
 232   
         {
 233  832
             String propertyTypeRef = op.getClassReference(valueType);
 234   
 
 235  832
             buffer.append("(");
 236  832
             buffer.append(ClassFabUtils.getJavaClassName(valueType));
 237  832
             buffer.append(") ");
 238  832
             buffer.append(bindingName);
 239  832
             buffer.append(".getObject(");
 240  832
             buffer.append(propertyTypeRef);
 241  832
             buffer.append(")");
 242   
         }
 243   
         else
 244   
         {
 245  309
             buffer.append(EnhanceUtils.class.getName());
 246  309
             buffer.append(".");
 247  309
             buffer.append(unwrapper);
 248  309
             buffer.append("(");
 249  309
             buffer.append(bindingName);
 250  309
             buffer.append(")");
 251   
         }
 252   
 
 253  1141
         return buffer.toString();
 254   
     }
 255   
 
 256   
 }