public List getComponentInstancesOfType(Class componentType) { if (componentType == null) { return Collections.EMPTY_LIST; } Map adapterToInstanceMap = new HashMap(); for (Iterator iterator = componentAdapters.iterator(); iterator.hasNext();) { ComponentAdapter componentAdapter = (ComponentAdapter) iterator.next(); if (componentType.isAssignableFrom(componentAdapter.getComponentImplementation())) { Object componentInstance = getInstance(componentAdapter); adapterToInstanceMap.put(componentAdapter, componentInstance); // This is to ensure all are added. (Indirect dependencies will be added // from InstantiatingComponentAdapter). addOrderedComponentAdapter(componentAdapter); } } List result = new ArrayList(); for (Iterator iterator = orderedComponentAdapters.iterator(); iterator.hasNext();) { Object componentAdapter = iterator.next(); final Object componentInstance = adapterToInstanceMap.get(componentAdapter); if (componentInstance != null) { // may be null in the case of the "implicit" adapter // representing "this". result.add(componentInstance); } } return result; }
private Object getInstance(ComponentAdapter componentAdapter) { // check wether this is our adapter // we need to check this to ensure up-down dependencies cannot be followed final boolean isLocal = componentAdapters.contains(componentAdapter); if (isLocal) { PicoException firstLevelException = null; Object instance = null; try { instance = componentAdapter.getComponentInstance(this); } catch (PicoInitializationException e) { firstLevelException = e; } catch (PicoIntrospectionException e) { firstLevelException = e; } if (firstLevelException != null) { if (parent != null) { instance = parent.getComponentInstance(componentAdapter.getComponentKey()); if( instance != null ) { return instance; } } throw firstLevelException; } addOrderedComponentAdapter(componentAdapter); return instance; } else if (parent != null) { return parent.getComponentInstance(componentAdapter.getComponentKey()); } return null; }