Clover coverage report - PicoContainer - 1.1
Coverage timestamp: Thu Nov 4 2004 11:55:45 CST
file stats: LOC: 140   Methods: 8
NCLOC: 73   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
BasicComponentParameter.java 87.5% 96.4% 100% 94.2%
coverage coverage
 1   
 /*****************************************************************************
 2   
  * Copyright (C) PicoContainer Organization. All rights reserved.            *
 3   
  * ------------------------------------------------------------------------- *
 4   
  * The software in this package is published under the terms of the BSD      *
 5   
  * style license a copy of which has been included with this distribution in *
 6   
  * the LICENSE.txt file.                                                     *
 7   
  *                                                                           *
 8   
  * Original code by                                                          *
 9   
  *****************************************************************************/
 10   
 package org.picocontainer.defaults;
 11   
 
 12   
 import org.picocontainer.ComponentAdapter;
 13   
 import org.picocontainer.Parameter;
 14   
 import org.picocontainer.PicoContainer;
 15   
 import org.picocontainer.PicoInstantiationException;
 16   
 import org.picocontainer.PicoIntrospectionException;
 17   
 import org.picocontainer.PicoVisitor;
 18   
 
 19   
 import java.io.Serializable;
 20   
 import java.lang.reflect.Field;
 21   
 
 22   
 
 23   
 /**
 24   
  * A BasicComponentParameter should be used to pass in a particular component as argument to a
 25   
  * different component's constructor. This is particularly useful in cases where several
 26   
  * components of the same type have been registered, but with a different key. Passing a
 27   
  * ComponentParameter as a parameter when registering a component will give PicoContainer a hint
 28   
  * about what other component to use in the constructor. This Parameter will never resolve
 29   
  * against a collecting type, that is not directly registered in the PicoContainer itself.
 30   
  * 
 31   
  * @author Jon Tirsén
 32   
  * @author Aslak Hellesøy
 33   
  * @author Jörg Schaible
 34   
  * @author Thomas Heller
 35   
  * @version $Revision: 1.4 $
 36   
  */
 37   
 public class BasicComponentParameter
 38   
         implements Parameter, Serializable {
 39   
     
 40   
     /**
 41   
      * <code>BASIC_DEFAULT</code> is an instance of BasicComponentParameter using the default constructor.
 42   
      */
 43   
     public static final BasicComponentParameter BASIC_DEFAULT = new BasicComponentParameter();
 44   
 
 45   
     private Object componentKey;
 46   
 
 47   
     /**
 48   
      * Expect a parameter matching a component of a specific key.
 49   
      * 
 50   
      * @param componentKey the key of the desired component
 51   
      */
 52  260
     public BasicComponentParameter(Object componentKey) {
 53  260
         this.componentKey = componentKey;
 54   
     }
 55   
 
 56   
     /**
 57   
      * Expect any paramter of the appropriate type.
 58   
      */
 59  68
     public BasicComponentParameter() {
 60   
     }
 61   
 
 62   
     /**
 63   
      * Check wether the given Parameter can be statisfied by the container.
 64   
      * 
 65   
      * @return <code>true</code> if the Parameter can be verified.
 66   
      * @see org.picocontainer.Parameter#isResolvable(org.picocontainer.PicoContainer,
 67   
      *           org.picocontainer.ComponentAdapter, java.lang.Class)
 68   
      */
 69  1640
     public boolean isResolvable(PicoContainer container, ComponentAdapter adapter, Class expectedType) {
 70  1640
         return resolveAdapter(container, adapter, expectedType) != null;
 71   
     }
 72   
 
 73  460
     public Object resolveInstance(PicoContainer container, ComponentAdapter adapter, Class expectedType)
 74   
             throws PicoInstantiationException {
 75  460
         final ComponentAdapter componentAdapter = resolveAdapter(container, adapter, expectedType);
 76  460
         if (componentAdapter != null) {
 77  392
             return container.getComponentInstance(componentAdapter.getComponentKey());
 78   
         }
 79  68
         return null;
 80   
     }
 81   
 
 82  14
     public void verify(PicoContainer container, ComponentAdapter adapter, Class expectedType) throws PicoIntrospectionException {
 83  14
         final ComponentAdapter componentAdapter = resolveAdapter(container, adapter, expectedType);
 84  14
         if (componentAdapter == null) {
 85  0
             throw new PicoIntrospectionException(expectedType.getName() + " is not resolvable");
 86   
         }
 87  14
         componentAdapter.verify(container);
 88   
     }
 89   
 
 90   
     /**
 91   
      * Visit the current {@link Parameter}.
 92   
      * 
 93   
      * @see org.picocontainer.Parameter#accept(org.picocontainer.PicoVisitor)
 94   
      */
 95  24
     public void accept(final PicoVisitor visitor) {
 96  24
         visitor.visitParameter(this);
 97   
     }
 98   
 
 99  2114
     private ComponentAdapter resolveAdapter(PicoContainer container, ComponentAdapter adapter, Class expectedType) {
 100   
 
 101  2114
         final ComponentAdapter result = getTargetAdapter(container, expectedType);
 102  2098
         if (result == null) {
 103  1062
             return null;
 104   
         }
 105   
 
 106   
         // can't depend on ourselves
 107  1036
         if (adapter != null && adapter.getComponentKey().equals(result.getComponentKey())) {
 108  112
             return null;
 109   
         }
 110   
 
 111  924
         if (!expectedType.isAssignableFrom(result.getComponentImplementation())) {
 112   
             // check for primitive value
 113  20
             if (expectedType.isPrimitive()) {
 114  6
                 try {
 115  6
                     final Field field = result.getComponentImplementation().getField("TYPE");
 116  4
                     final Class type = (Class) field.get(result.getComponentInstance(null));
 117  4
                     if (expectedType.isAssignableFrom(type)) {
 118  4
                         return result;
 119   
                     }
 120   
                 } catch (NoSuchFieldException e) {
 121   
                 } catch (IllegalArgumentException e) {
 122   
                 } catch (IllegalAccessException e) {
 123   
                 } catch (ClassCastException e) {
 124   
                 }
 125   
             }
 126  16
             return null;
 127   
         }
 128  904
         return result;
 129   
     }
 130   
 
 131  2114
     private ComponentAdapter getTargetAdapter(PicoContainer container, Class expectedType) {
 132  2114
         if (componentKey != null) {
 133   
             // key tells us where to look so we follow
 134  54
             return container.getComponentAdapter(componentKey);
 135   
         } else {
 136  2060
             return container.getComponentAdapterOfType(expectedType);
 137   
         }
 138   
     }
 139   
 }
 140