Clover coverage report - Code Coverage for tapestry release 4.0.2
Coverage timestamp: Thu Apr 13 2006 10:52:06 EDT
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  3 _primitiveParser.put(boolean.class, "java.lang.Boolean.parseBoolean");
 50  3 _primitiveParser.put(short.class, "java.lang.Short.parseShort");
 51  3 _primitiveParser.put(int.class, "java.lang.Integer.parseInt");
 52  3 _primitiveParser.put(long.class, "java.lang.Long.parseLong");
 53  3 _primitiveParser.put(double.class, "java.lang.Double.parseDouble");
 54  3 _primitiveParser.put(float.class, "java.lang.Float.parseFloat");
 55    }
 56   
 57  3 public void performEnhancement(EnhancementOperation op, InjectSpecification spec)
 58    {
 59  3 String propertyName = spec.getProperty();
 60  3 String metaKey = spec.getObject();
 61   
 62  3 injectMetaValue(op, propertyName, metaKey, spec.getLocation());
 63    }
 64   
 65  3 public void injectMetaValue(EnhancementOperation op, String propertyName, String metaKey,
 66    Location location)
 67    {
 68  3 Defense.notNull(op, "op");
 69  3 Defense.notNull(propertyName, "propertyName");
 70  3 Defense.notNull(metaKey, "metaKey");
 71   
 72  3 Class propertyType = op.getPropertyType(propertyName);
 73   
 74  3 op.claimReadonlyProperty(propertyName);
 75   
 76  3 String sourceName = op
 77    .addInjectedField(SOURCE_NAME, ComponentPropertySource.class, _source);
 78   
 79  3 MethodSignature sig = new MethodSignature(propertyType, op
 80    .getAccessorMethodName(propertyName), null, null);
 81   
 82  3 String parser = (String) _primitiveParser.get(propertyType);
 83   
 84  3 if (parser != null)
 85    {
 86  1 addPrimitive(op, metaKey, propertyName, sig, sourceName, parser, location);
 87  1 return;
 88    }
 89   
 90  2 if (propertyType == char.class)
 91    {
 92  1 addCharacterPrimitive(op, metaKey, propertyName, sig, sourceName, location);
 93  1 return;
 94    }
 95   
 96  1 addObject(op, metaKey, propertyName, propertyType, sig, sourceName, location);
 97    }
 98   
 99  1 private void addPrimitive(EnhancementOperation op, String metaKey, String propertyName,
 100    MethodSignature sig, String sourceName, String parser, Location location)
 101    {
 102  1 BodyBuilder builder = new BodyBuilder();
 103  1 builder.begin();
 104  1 builder.addln(
 105    "java.lang.String meta = {0}.getComponentProperty(this, \"{1}\");",
 106    sourceName,
 107    metaKey);
 108  1 builder.addln("return {0}(meta);", parser);
 109  1 builder.end();
 110   
 111  1 op.addMethod(Modifier.PUBLIC, sig, builder.toString(), location);
 112    }
 113   
 114  1 private void addCharacterPrimitive(EnhancementOperation op, String metaKey,
 115    String propertyName, MethodSignature sig, String sourceName, Location location)
 116    {
 117  1 BodyBuilder builder = new BodyBuilder();
 118  1 builder.begin();
 119  1 builder.addln(
 120    "java.lang.String meta = {0}.getComponentProperty(this, \"{1}\");",
 121    sourceName,
 122    metaKey);
 123  1 builder.addln("return meta.charAt(0);");
 124  1 builder.end();
 125   
 126  1 op.addMethod(Modifier.PUBLIC, sig, builder.toString(), location);
 127    }
 128   
 129  1 private void addObject(EnhancementOperation op, String metaKey, String propertyName,
 130    Class propertyType, MethodSignature sig, String sourceName, Location location)
 131    {
 132  1 String valueConverterName = op.addInjectedField(
 133    "_$valueConverter",
 134    ValueConverter.class,
 135    _valueConverter);
 136  1 String classRef = op.getClassReference(propertyType);
 137   
 138  1 BodyBuilder builder = new BodyBuilder();
 139  1 builder.begin();
 140  1 builder.addln(
 141    "java.lang.String meta = {0}.getComponentProperty(this, \"{1}\");",
 142    sourceName,
 143    metaKey);
 144  1 builder.addln("return ({0}) {1}.coerceValue(meta, {2});", ClassFabUtils
 145    .getJavaClassName(propertyType), valueConverterName, classRef);
 146  1 builder.end();
 147   
 148  1 op.addMethod(Modifier.PUBLIC, sig, builder.toString(), location);
 149    }
 150   
 151  3 public void setSource(ComponentPropertySource source)
 152    {
 153  3 _source = source;
 154    }
 155   
 156  1 public void setValueConverter(ValueConverter valueConverter)
 157    {
 158  1 _valueConverter = valueConverter;
 159    }
 160    }