1 10 package org.picocontainer.defaults; 11 12 import org.picocontainer.Parameter; 13 import org.picocontainer.PicoContainer; 14 import org.picocontainer.PicoIntrospectionException; 15 import org.picocontainer.PicoVisitor; 16 17 import java.lang.reflect.Constructor ; 18 import java.lang.reflect.InvocationTargetException ; 19 import java.lang.reflect.Modifier ; 20 21 33 public abstract class InstantiatingComponentAdapter extends AbstractComponentAdapter { 34 35 protected transient Guard verifyingGuard; 36 37 protected transient Parameter[] parameters; 38 39 protected boolean allowNonPublicClasses; 40 41 protected static abstract class Guard extends ThreadLocalCyclicDependencyGuard { 42 protected PicoContainer guardedContainer; 43 protected void setArguments(PicoContainer container) { 44 this.guardedContainer = container; 45 } 46 } 47 48 57 protected InstantiatingComponentAdapter(Object componentKey, Class componentImplementation, Parameter[] parameters, boolean allowNonPublicClasses) { 58 super(componentKey, componentImplementation); 59 checkConcrete(); 60 61 this.parameters = parameters; 62 this.allowNonPublicClasses = allowNonPublicClasses; 63 } 64 65 private void checkConcrete() throws NotConcreteRegistrationException { 66 boolean isAbstract = (getComponentImplementation().getModifiers() & Modifier.ABSTRACT) == Modifier.ABSTRACT; 68 if (getComponentImplementation().isInterface() || isAbstract) { 69 throw new NotConcreteRegistrationException(getComponentImplementation()); 70 } 71 } 72 73 79 protected Parameter[] createDefaultParameters(Class [] parameters) { 80 Parameter[] componentParameters = new Parameter[parameters.length]; 81 for (int i = 0; i < parameters.length; i++) { 82 componentParameters[i] = ComponentParameter.DEFAULT; 83 } 84 return componentParameters; 85 } 86 87 public void verify(final PicoContainer container) throws PicoIntrospectionException { 88 if (verifyingGuard == null) { 89 verifyingGuard = new Guard() { 90 public Object run() { 91 final Constructor constructor = getGreediestSatisfiableConstructor(guardedContainer); 92 final Class [] parameterTypes = constructor.getParameterTypes(); 93 final Parameter[] currentParameters = parameters != null ? parameters : createDefaultParameters(parameterTypes); 94 for (int i = 0; i < currentParameters.length; i++) { 95 currentParameters[i].verify(container, InstantiatingComponentAdapter.this, parameterTypes[i]); 96 } 97 return null; 98 } 99 }; 100 } 101 verifyingGuard.setArguments(container); 102 verifyingGuard.observe(getComponentImplementation()); 103 } 104 105 public void accept(PicoVisitor visitor) { 106 super.accept(visitor); 107 if (parameters != null) { 108 for (int i = 0; i < parameters.length; i++) { 109 parameters[i].accept(visitor); 110 } 111 } 112 } 113 114 124 protected Object newInstance(Constructor constructor, Object [] parameters) throws InstantiationException , IllegalAccessException , InvocationTargetException { 125 if (allowNonPublicClasses) { 126 constructor.setAccessible(true); 127 } 128 return constructor.newInstance(parameters); 129 } 130 131 142 protected abstract Constructor getGreediestSatisfiableConstructor(PicoContainer container) throws PicoIntrospectionException, UnsatisfiableDependenciesException, AmbiguousComponentResolutionException, AssignabilityRegistrationException, NotConcreteRegistrationException; 143 } 144 | Popular Tags |