Clover coverage report - Ledge Container - SNAPSHOT
Coverage timestamp: Fri Nov 17 2006 05:16:03 CET
file stats: LOC: 170   Methods: 7
NCLOC: 95   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
StringParameter.java 100% 91.3% 71.4% 88.9%
coverage coverage
 1    //
 2    // Copyright (c) 2003, Caltha - Gajda, Krzewski, Mach, Potempski Sp.J.
 3    // All rights reserved.
 4    //
 5    // Redistribution and use in source and binary forms, with or without modification,
 6    // are permitted provided that the following conditions are met:
 7    //
 8    // * Redistributions of source code must retain the above copyright notice,
 9    // this list of conditions and the following disclaimer.
 10    // * Redistributions in binary form must reproduce the above copyright notice,
 11    // this list of conditions and the following disclaimer in the documentation
 12    // and/or other materials provided with the distribution.
 13    // * Neither the name of the Caltha - Gajda, Krzewski, Mach, Potempski Sp.J.
 14    // nor the names of its contributors may be used to endorse or promote products
 15    // derived from this software without specific prior written permission.
 16    //
 17    // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 18    // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 19    // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 20    // IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
 21    // INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 22    // BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
 23    // OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 24    // WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 25    // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 26    // POSSIBILITY OF SUCH DAMAGE.
 27    //
 28   
 29    package org.objectledge.pico;
 30   
 31    import java.lang.reflect.Field;
 32   
 33    import org.nanocontainer.reflection.InvalidConversionException;
 34    import org.nanocontainer.reflection.StringToObjectConverter;
 35    import org.picocontainer.ComponentAdapter;
 36    import org.picocontainer.Parameter;
 37    import org.picocontainer.PicoContainer;
 38    import org.picocontainer.PicoInitializationException;
 39    import org.picocontainer.PicoIntrospectionException;
 40    import org.picocontainer.PicoVisitor;
 41   
 42    /**
 43    * A parameter that is intialized with a string, and is converted to the desired type on demand.
 44    *
 45    * <p>Created on Jan 8, 2004</p>
 46    * @author <a href="Rafal.Krzewski">rafal@caltha.pl</a>
 47    * @version $Id: StringParameter.java,v 1.4 2005/02/04 02:28:13 rafal Exp $
 48    */
 49    public class StringParameter
 50    implements Parameter
 51    {
 52    /** the string representation of the value */
 53    private String stringValue;
 54   
 55    /** the parmeter class */
 56    private Class parameterType;
 57   
 58    /**
 59    * Creates a new StringParamter instance.
 60    *
 61    * @param stringValue the textual value of the component.
 62    * @param parameterType the class of the parameter, or <code>null</code> to
 63    * determine dynamically.
 64    */
 65  3440 public StringParameter(String stringValue, Class parameterType)
 66    {
 67  3440 this.stringValue = stringValue;
 68  3440 this.parameterType = parameterType;
 69    }
 70   
 71    /**
 72    * Creates a new StringParamter instance.
 73    *
 74    * @param stringValue the textual value of the component.
 75    */
 76  0 public StringParameter(String stringValue)
 77    {
 78  0 this(stringValue, null);
 79    }
 80   
 81    /**
 82    * {@inheritDoc}
 83    */
 84  6106 public Object resolveInstance(PicoContainer container, ComponentAdapter adapter,
 85    Class expectedType)
 86    throws PicoInitializationException
 87    {
 88  6106 StringToObjectConverter converter = (StringToObjectConverter)container.
 89    getComponentInstance(StringToObjectConverter.class);
 90  6106 if(parameterType == null)
 91    {
 92  3698 return converter.convertTo(expectedType, stringValue);
 93    }
 94    else
 95    {
 96  2408 return converter.convertTo(parameterType, stringValue);
 97    }
 98    }
 99   
 100    /**
 101    * {@inheritDoc}
 102    */
 103  3956 public boolean isResolvable(PicoContainer container, ComponentAdapter adapter,
 104    Class expectedType)
 105    {
 106  3956 try
 107    {
 108  3956 verify(container, adapter, expectedType);
 109  3010 return true;
 110    }
 111    catch(PicoIntrospectionException e)
 112    {
 113  946 return false;
 114    }
 115    }
 116   
 117    /**
 118    * {@inheritDoc}
 119    */
 120  3956 public void verify(PicoContainer container, ComponentAdapter adapter, Class expectedType)
 121    throws PicoIntrospectionException
 122    {
 123  3956 if(parameterType != null && !expectedType.isAssignableFrom(parameterType)
 124    && !checkPrimitive(expectedType))
 125    {
 126  860 throw new PicoIntrospectionException(expectedType.getClass().getName()
 127    + " is not assignable from "
 128    + parameterType.getName());
 129    }
 130  3096 try
 131    {
 132  3096 resolveInstance(container, adapter, expectedType);
 133    }
 134    catch(InvalidConversionException e)
 135    {
 136  86 throw new PicoIntrospectionException("cannot resolve parameter from string", e);
 137    }
 138    }
 139   
 140  1204 private boolean checkPrimitive(Class expectedType)
 141    {
 142  1204 try
 143    {
 144  1204 if(expectedType.isPrimitive())
 145    {
 146  860 final Field field = parameterType.getField("TYPE");
 147  344 final Class type = (Class)field.get(null);
 148  344 return expectedType.isAssignableFrom(type);
 149    }
 150    }
 151    catch(NoSuchFieldException e)
 152    {
 153    // ignored
 154    }
 155    catch(IllegalAccessException e)
 156    {
 157    // ignored
 158    }
 159  860 return false;
 160    }
 161   
 162   
 163    /**
 164    * {@inheritDoc}
 165    */
 166  0 public void accept(PicoVisitor visitor)
 167    {
 168  0 visitor.visitParameter(this);
 169    }
 170    }