1 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 import java.util.Arrays ; 22 import java.util.HashSet ; 23 import java.util.List ; 24 import java.util.Iterator ; 25 26 40 public class BasicComponentParameter 41 implements Parameter, Serializable { 42 43 46 public static final BasicComponentParameter BASIC_DEFAULT = new BasicComponentParameter(); 47 48 private Object componentKey; 49 50 55 public BasicComponentParameter(Object componentKey) { 56 this.componentKey = componentKey; 57 } 58 59 62 public BasicComponentParameter() { 63 } 64 65 72 public boolean isResolvable(PicoContainer container, ComponentAdapter adapter, Class expectedType) { 73 return resolveAdapter(container, adapter, expectedType) != null; 74 } 75 76 public Object resolveInstance(PicoContainer container, ComponentAdapter adapter, Class expectedType) 77 throws PicoInstantiationException { 78 final ComponentAdapter componentAdapter = resolveAdapter(container, adapter, expectedType); 79 if (componentAdapter != null) { 80 return container.getComponentInstance(componentAdapter.getComponentKey()); 81 } 82 return null; 83 } 84 85 public void verify(PicoContainer container, ComponentAdapter adapter, Class expectedType) throws PicoIntrospectionException { 86 final ComponentAdapter componentAdapter = resolveAdapter(container, adapter, expectedType); 87 if (componentAdapter == null) { 88 final HashSet set = new HashSet (); 89 set.add(Arrays.asList(new Class [] {expectedType})); 90 throw new UnsatisfiableDependenciesException(adapter, set); 91 } 92 componentAdapter.verify(container); 93 } 94 95 100 public void accept(final PicoVisitor visitor) { 101 visitor.visitParameter(this); 102 } 103 104 private ComponentAdapter resolveAdapter(PicoContainer container, ComponentAdapter adapter, Class expectedType) { 105 106 final ComponentAdapter result = getTargetAdapter(container, expectedType,adapter); 107 if (result == null) { 108 return null; 109 } 110 111 if (!expectedType.isAssignableFrom(result.getComponentImplementation())) { 112 if (expectedType.isPrimitive()) { 114 try { 115 final Field field = result.getComponentImplementation().getField("TYPE"); 116 final Class type = (Class ) field.get(result.getComponentInstance(null)); 117 if (expectedType.isAssignableFrom(type)) { 118 return result; 119 } 120 } catch (NoSuchFieldException e) { 121 } catch (IllegalArgumentException e) { 122 } catch (IllegalAccessException e) { 123 } catch (ClassCastException e) { 124 } 125 } 126 return null; 127 } 128 return result; 129 } 130 131 private ComponentAdapter getTargetAdapter(PicoContainer container, Class expectedType, ComponentAdapter excludeAdapter) { 132 if (componentKey != null) { 133 return container.getComponentAdapter(componentKey); 135 } else if(excludeAdapter == null) { 136 return container.getComponentAdapterOfType(expectedType); 137 } else { 138 Object excludeKey = excludeAdapter.getComponentKey(); 139 ComponentAdapter byKey = container.getComponentAdapter(expectedType); 140 if(byKey != null ) { 141 if( byKey.getComponentKey().equals(excludeKey)) { 142 return null; 143 } 144 return byKey; 145 } 146 List found = container.getComponentAdaptersOfType(expectedType); 147 ComponentAdapter exclude = null; 148 ComponentAdapter work; 149 for(Iterator iterator = found.iterator(); iterator.hasNext();) { 150 work = (ComponentAdapter) iterator.next(); 151 if( work.getComponentKey().equals(excludeKey)) { 152 exclude = work; 153 } 154 } 155 found.remove(exclude); 156 if(found.size() == 0) { 157 if( container.getParent() != null) { 158 return container.getParent().getComponentAdapterOfType(expectedType); 159 } else { 160 return null; 161 } 162 } else if(found.size() == 1) { 163 return (ComponentAdapter)found.get(0); 164 } else { 165 Class [] foundClasses = new Class [found.size()]; 166 for (int i = 0; i < foundClasses.length; i++) { 167 foundClasses[i] = ((ComponentAdapter) found.get(i)).getComponentImplementation(); 168 } 169 throw new AmbiguousComponentResolutionException(expectedType, foundClasses); 170 } 171 } 172 } 173 } 174 | Popular Tags |