Clover coverage report - PicoContainer - 1.0-beta-1
Coverage timestamp: Thu Aug 14 2003 23:16:27 BST
file stats: LOC: 105   Methods: 10
NCLOC: 72   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
ComponentSpecification.java 83.3% 93.8% 100% 92.6%
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   
  * Idea by Rachel Davies, Original code by Aslak Hellesoy and Paul Hammant   *
 9   
  *****************************************************************************/
 10   
 
 11   
 package org.picocontainer.internals;
 12   
 
 13   
 import org.picocontainer.PicoInitializationException;
 14   
 import org.picocontainer.PicoIntrospectionException;
 15   
 
 16   
 import java.util.Arrays;
 17   
 import java.io.Serializable;
 18   
 
 19   
 public class ComponentSpecification implements Serializable {
 20   
 
 21   
     private final ComponentFactory componentFactory;
 22   
     private final Object componentKey;
 23   
     private final Class componentImplementation;
 24   
     private Parameter[] parameters;
 25   
 
 26  25
     public ComponentSpecification(ComponentFactory componentFactory, final Object componentKey, final Class comp, Parameter[] parameters) {
 27  25
         this.componentFactory = componentFactory;
 28  25
         this.componentKey = componentKey;
 29  25
         this.componentImplementation = comp;
 30  25
         this.parameters = parameters;
 31   
     }
 32   
 
 33  153
     public ComponentSpecification(ComponentFactory componentFactory, Object componentKey, Class comp) throws PicoIntrospectionException {
 34  153
         this.componentFactory = componentFactory;
 35  153
         this.componentKey = componentKey;
 36  153
         this.componentImplementation = comp;
 37   
 
 38  153
         parameters = new Parameter[componentFactory.getDependencies(comp).length];
 39  152
         for (int i = 0; i < parameters.length; i++) {
 40  86
             parameters[i] = createDefaultParameter();
 41   
         }
 42   
     }
 43   
 
 44  86
     protected Parameter createDefaultParameter() {
 45  86
         return new ComponentParameter();
 46   
     }
 47   
 
 48  452
     public Object getComponentKey() {
 49  452
         return componentKey;
 50   
     }
 51   
 
 52  363
     public Class getComponentImplementation() {
 53  363
         return componentImplementation;
 54   
     }
 55   
 
 56  126
     public Object instantiateComponent(ComponentRegistry componentRegistry)
 57   
             throws PicoInitializationException {
 58  126
         Class[] dependencyTypes = componentFactory.getDependencies(componentImplementation);
 59  126
         Object[] dependencies = new Object[dependencyTypes.length];
 60  126
         for (int i = 0; i < dependencies.length; i++) {
 61  76
             dependencies[i] = parameters[i].resolve(componentRegistry, this, dependencyTypes[i]);
 62   
         }
 63  121
         return componentFactory.createComponent(this, dependencies);
 64   
     }
 65   
 
 66  15
     public static boolean isAssignableFrom(Class actual, Class requested) {
 67  15
         if (actual == Integer.TYPE || actual == Integer.class) {
 68  6
             return requested == Integer.TYPE || requested == Integer.class;
 69   
         }
 70   
 
 71   
         // TODO handle the rest of the primitive types in the same way (Java really sucks concerning this!)
 72   
 
 73  9
         return actual.isAssignableFrom(requested);
 74   
     }
 75   
 
 76  7
     public void addConstantParameterBasedOnType(Class parameter, Object arg) throws PicoIntrospectionException {
 77   
         // TODO this is an ugly hack and the feature should simply be removed
 78  7
         Class[] dependencies = componentFactory.getDependencies(componentImplementation);
 79  11
         for (int i = 0; i < dependencies.length; i++) {
 80   
 
 81  11
             if (isAssignableFrom(dependencies[i], parameter) && !(parameters[i] instanceof ConstantParameter)) {
 82  7
                 parameters[i] = new ConstantParameter(arg);
 83  7
                 return;
 84   
             }
 85   
         }
 86   
 
 87  0
         throw new RuntimeException("No such parameter " + parameter + " in " + Arrays.asList(dependencies));
 88   
     }
 89   
 
 90  15
     public Parameter[] getParameters() {
 91  15
         return parameters;
 92   
     }
 93   
     
 94  7
     public boolean equals(Object object) {
 95  7
         if (object == null || !getClass().equals(object.getClass())) {
 96  0
             return false;
 97   
         }
 98  7
         ComponentSpecification other = (ComponentSpecification) object;
 99   
         
 100  7
         return getComponentKey().equals(other.getComponentKey()) &&
 101   
             getComponentImplementation().equals(other.getComponentImplementation()) &&
 102   
             Arrays.asList(getParameters()).equals(Arrays.asList(other.getParameters()));
 103   
     }
 104   
 }
 105