View Javadoc
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 public ComponentSpecification(ComponentFactory componentFactory, final Object componentKey, final Class comp, Parameter[] parameters) { 27 this.componentFactory = componentFactory; 28 this.componentKey = componentKey; 29 this.componentImplementation = comp; 30 this.parameters = parameters; 31 } 32 33 public ComponentSpecification(ComponentFactory componentFactory, Object componentKey, Class comp) throws PicoIntrospectionException { 34 this.componentFactory = componentFactory; 35 this.componentKey = componentKey; 36 this.componentImplementation = comp; 37 38 parameters = new Parameter[componentFactory.getDependencies(comp).length]; 39 for (int i = 0; i < parameters.length; i++) { 40 parameters[i] = createDefaultParameter(); 41 } 42 } 43 44 protected Parameter createDefaultParameter() { 45 return new ComponentParameter(); 46 } 47 48 public Object getComponentKey() { 49 return componentKey; 50 } 51 52 public Class getComponentImplementation() { 53 return componentImplementation; 54 } 55 56 public Object instantiateComponent(ComponentRegistry componentRegistry) 57 throws PicoInitializationException { 58 Class[] dependencyTypes = componentFactory.getDependencies(componentImplementation); 59 Object[] dependencies = new Object[dependencyTypes.length]; 60 for (int i = 0; i < dependencies.length; i++) { 61 dependencies[i] = parameters[i].resolve(componentRegistry, this, dependencyTypes[i]); 62 } 63 return componentFactory.createComponent(this, dependencies); 64 } 65 66 public static boolean isAssignableFrom(Class actual, Class requested) { 67 if (actual == Integer.TYPE || actual == Integer.class) { 68 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 return actual.isAssignableFrom(requested); 74 } 75 76 public void addConstantParameterBasedOnType(Class parameter, Object arg) throws PicoIntrospectionException { 77 // TODO this is an ugly hack and the feature should simply be removed 78 Class[] dependencies = componentFactory.getDependencies(componentImplementation); 79 for (int i = 0; i < dependencies.length; i++) { 80 81 if (isAssignableFrom(dependencies[i], parameter) && !(parameters[i] instanceof ConstantParameter)) { 82 parameters[i] = new ConstantParameter(arg); 83 return; 84 } 85 } 86 87 throw new RuntimeException("No such parameter " + parameter + " in " + Arrays.asList(dependencies)); 88 } 89 90 public Parameter[] getParameters() { 91 return parameters; 92 } 93 94 public boolean equals(Object object) { 95 if (object == null || !getClass().equals(object.getClass())) { 96 return false; 97 } 98 ComponentSpecification other = (ComponentSpecification) object; 99 100 return getComponentKey().equals(other.getComponentKey()) && 101 getComponentImplementation().equals(other.getComponentImplementation()) && 102 Arrays.asList(getParameters()).equals(Arrays.asList(other.getParameters())); 103 } 104 }

This page was automatically generated by Maven