Clover coverage report - Code Coverage for tapestry release 4.0-alpha-3
Coverage timestamp: Mon May 16 2005 09:05:49 EDT
file stats: LOC: 144   Methods: 6
NCLOC: 96   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
InjectMetaWorker.java 100% 100% 100% 100%
coverage
 1   
 // Copyright 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.BodyBuilder;
 22   
 import org.apache.hivemind.service.ClassFabUtils;
 23   
 import org.apache.hivemind.service.MethodSignature;
 24   
 import org.apache.tapestry.coerce.ValueConverter;
 25   
 import org.apache.tapestry.services.ComponentPropertySource;
 26   
 import org.apache.tapestry.spec.InjectSpecification;
 27   
 
 28   
 /**
 29   
  * Injects meta data obtained via {@link org.apache.tapestry.services.ComponentPropertySource}
 30   
  * (meaning that meta-data is searched for in the component's specification, then it's namespace
 31   
  * (library or application specification), then the global application properties.
 32   
  * 
 33   
  * @author Howard M. Lewis Ship
 34   
  * @since 4.0
 35   
  */
 36   
 public class InjectMetaWorker implements InjectEnhancementWorker
 37   
 {
 38   
     static final String SOURCE_NAME = "_$componentPropertySource";
 39   
 
 40   
     private ComponentPropertySource _source;
 41   
 
 42   
     private ValueConverter _valueConverter;
 43   
 
 44   
     private Map _primitiveParser = new HashMap();
 45   
 
 46   
     {
 47  3
         _primitiveParser.put(boolean.class, "java.lang.Boolean.valueOf");
 48  3
         _primitiveParser.put(short.class, "java.lang.Short.parseShort");
 49  3
         _primitiveParser.put(int.class, "java.lang.Integer.parseInt");
 50  3
         _primitiveParser.put(long.class, "java.lang.Long.parseLong");
 51  3
         _primitiveParser.put(double.class, "java.lang.Double.parseDouble");
 52  3
         _primitiveParser.put(float.class, "java.lang.Float.parseFloat");
 53   
     }
 54   
 
 55  3
     public void performEnhancement(EnhancementOperation op, InjectSpecification spec)
 56   
     {
 57  3
         String propertyName = spec.getProperty();
 58  3
         Class propertyType = op.getPropertyType(propertyName);
 59   
 
 60  3
         op.claimProperty(propertyName);
 61   
 
 62  3
         String sourceName = op.addInjectedField(SOURCE_NAME, _source);
 63   
 
 64  3
         MethodSignature sig = new MethodSignature(propertyType, op
 65   
                 .getAccessorMethodName(propertyName), null, null);
 66   
 
 67  3
         String parser = (String) _primitiveParser.get(propertyType);
 68   
 
 69  3
         if (parser != null)
 70   
         {
 71  1
             addPrimitive(op, spec, propertyName, sig, sourceName, parser);
 72  1
             return;
 73   
         }
 74   
 
 75  2
         if (propertyType == char.class)
 76   
         {
 77  1
             addCharacterPrimitive(op, spec, propertyName, sig, sourceName);
 78  1
             return;
 79   
         }
 80   
 
 81  1
         addObject(op, spec, propertyName, propertyType, sig, sourceName);
 82   
 
 83   
     }
 84   
 
 85  1
     private void addPrimitive(EnhancementOperation op, InjectSpecification spec,
 86   
             String propertyName, MethodSignature sig, String sourceName, String parser)
 87   
     {
 88  1
         BodyBuilder builder = new BodyBuilder();
 89  1
         builder.begin();
 90  1
         builder.addln(
 91   
                 "java.lang.String meta = {0}.getComponentProperty(this, \"{1}\");",
 92   
                 sourceName,
 93   
                 spec.getObject());
 94  1
         builder.addln("return {0}(meta);", parser);
 95  1
         builder.end();
 96   
 
 97  1
         op.addMethod(Modifier.PUBLIC, sig, builder.toString());
 98   
     }
 99   
 
 100  1
     private void addCharacterPrimitive(EnhancementOperation op, InjectSpecification spec,
 101   
             String propertyName, MethodSignature sig, String sourceName)
 102   
     {
 103  1
         BodyBuilder builder = new BodyBuilder();
 104  1
         builder.begin();
 105  1
         builder.addln(
 106   
                 "java.lang.String meta = {0}.getComponentProperty(this, \"{1}\");",
 107   
                 sourceName,
 108   
                 spec.getObject());
 109  1
         builder.addln("return meta.charAt(0);");
 110  1
         builder.end();
 111   
 
 112  1
         op.addMethod(Modifier.PUBLIC, sig, builder.toString());
 113   
     }
 114   
 
 115  1
     private void addObject(EnhancementOperation op, InjectSpecification spec, String propertyName,
 116   
             Class propertyType, MethodSignature sig, String sourceName)
 117   
     {
 118  1
         String valueConverterName = op.addInjectedField("_$valueConverter", _valueConverter);
 119  1
         String classRef = op.getClassReference(propertyType);
 120   
 
 121  1
         BodyBuilder builder = new BodyBuilder();
 122  1
         builder.begin();
 123  1
         builder.addln(
 124   
                 "java.lang.String meta = {0}.getComponentProperty(this, \"{1}\");",
 125   
                 sourceName,
 126   
                 spec.getObject());
 127  1
         builder.addln("return ({0}) {1}.coerceValue(meta, {2});", ClassFabUtils
 128   
                 .getJavaClassName(propertyType), valueConverterName, classRef);
 129  1
         builder.end();
 130   
 
 131  1
         op.addMethod(Modifier.PUBLIC, sig, builder.toString());
 132   
     }
 133   
 
 134  3
     public void setSource(ComponentPropertySource source)
 135   
     {
 136  3
         _source = source;
 137   
     }
 138   
 
 139  1
     public void setValueConverter(ValueConverter valueConverter)
 140   
     {
 141  1
         _valueConverter = valueConverter;
 142   
     }
 143   
 }
 144