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