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