Package org.picocontainer.injectors
Class AbstractInjector<T>
- java.lang.Object
-
- org.picocontainer.adapters.AbstractAdapter<T>
-
- org.picocontainer.injectors.AbstractInjector<T>
-
- All Implemented Interfaces:
java.io.Serializable
,ComponentAdapter<T>
,ComponentMonitorStrategy
,Injector<T>
- Direct Known Subclasses:
CompositeInjector
,IterativeInjector
,SingleMemberInjector
public abstract class AbstractInjector<T> extends AbstractAdapter<T> implements Injector<T>
This ComponentAdapter will instantiate a new object for each call toComponentAdapter.getComponentInstance(PicoContainer, Type)
. That means that when used with a PicoContainer, getComponent will return a new object each time.- Author:
- Aslak Hellesøy, Paul Hammant, Jörg Schaible, Mauro Talevi
- See Also:
- Serialized Form
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
AbstractInjector.AmbiguousComponentResolutionException
Exception that is thrown as part of the introspection.static class
AbstractInjector.CyclicDependencyException
static class
AbstractInjector.NotConcreteRegistrationException
static class
AbstractInjector.UnsatisfiableDependenciesException
Exception thrown when some of the component's dependencies are not satisfiable.-
Nested classes/interfaces inherited from interface org.picocontainer.ComponentAdapter
ComponentAdapter.NOTHING
-
-
Field Summary
Fields Modifier and Type Field Description protected Parameter[]
parameters
The parameters to use for initialization.protected org.picocontainer.injectors.AbstractInjector.ThreadLocalCyclicDependencyGuard
verifyingGuard
The cycle guard for the verification.
-
Constructor Summary
Constructors Modifier Constructor Description protected
AbstractInjector(java.lang.Object componentKey, java.lang.Class<?> componentImplementation, Parameter[] parameters, ComponentMonitor monitor, boolean useNames)
Constructs a new ComponentAdapter for the given key and implementation.
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description void
accept(PicoVisitor visitor)
Accepts a visitor for this ComponentAdapter.protected java.lang.reflect.Type
box(java.lang.reflect.Type parameterType)
protected T
caughtIllegalAccessException(ComponentMonitor componentMonitor, java.lang.reflect.Constructor<T> constructor, java.lang.IllegalAccessException e, PicoContainer container)
inform monitor about access exception.protected java.lang.Object
caughtIllegalAccessException(ComponentMonitor componentMonitor, java.lang.reflect.Member member, java.lang.Object componentInstance, java.lang.IllegalAccessException e)
protected T
caughtInstantiationException(ComponentMonitor componentMonitor, java.lang.reflect.Constructor<T> constructor, java.lang.InstantiationException e, PicoContainer container)
inform monitor about component instantiation failureprotected T
caughtInvocationTargetException(ComponentMonitor componentMonitor, java.lang.reflect.Member member, java.lang.Object componentInstance, java.lang.reflect.InvocationTargetException e)
inform monitor about exception while instantiating componentprotected Parameter[]
createDefaultParameters(int length)
Create default parameters for the given types.java.lang.Object
decorateComponentInstance(PicoContainer container, java.lang.reflect.Type into, T instance)
A preexiting component instance can be injected into after instantiationT
getComponentInstance(PicoContainer container)
Retrieve the component instance.abstract T
getComponentInstance(PicoContainer container, java.lang.reflect.Type into)
Retrieve the component instance.java.lang.String
getDescriptor()
Get a string key descriptor of the component adapter for use in toString()protected T
newInstance(java.lang.reflect.Constructor<T> constructor, java.lang.Object[] parameters)
Instantiate an object with given parameters and respect the accessible flag.boolean
useNames()
void
verify(PicoContainer container)
Verify that all dependencies for this adapter can be satisfied.-
Methods inherited from class org.picocontainer.adapters.AbstractAdapter
changeMonitor, checkTypeCompatibility, currentMonitor, findAdapterOfType, getComponentImplementation, getComponentKey, getDelegate, toString
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface org.picocontainer.ComponentAdapter
findAdapterOfType, getComponentImplementation, getComponentKey, getDelegate
-
-
-
-
Field Detail
-
verifyingGuard
protected transient org.picocontainer.injectors.AbstractInjector.ThreadLocalCyclicDependencyGuard verifyingGuard
The cycle guard for the verification.
-
parameters
protected transient Parameter[] parameters
The parameters to use for initialization.
-
-
Constructor Detail
-
AbstractInjector
protected AbstractInjector(java.lang.Object componentKey, java.lang.Class<?> componentImplementation, Parameter[] parameters, ComponentMonitor monitor, boolean useNames)
Constructs a new ComponentAdapter for the given key and implementation.- Parameters:
componentKey
- the search key for this implementationcomponentImplementation
- the concrete implementationparameters
- the parameters to use for the initializationmonitor
- the component monitor used by this ComponentAdapter- Throws:
AbstractInjector.NotConcreteRegistrationException
- if the implementation is not a concrete classjava.lang.NullPointerException
- if one of the parameters isnull
-
-
Method Detail
-
useNames
public boolean useNames()
-
createDefaultParameters
protected Parameter[] createDefaultParameters(int length)
Create default parameters for the given types.- Parameters:
length
- parameter list length- Returns:
- the array with the default parameters.
-
verify
public void verify(PicoContainer container) throws PicoCompositionException
Description copied from interface:ComponentAdapter
Verify that all dependencies for this adapter can be satisfied. Normally, the adapter should verify this by checking that the associated PicoContainer contains all the needed dependencies.- Specified by:
verify
in interfaceComponentAdapter<T>
- Parameters:
container
- thePicoContainer
, that is used to resolve any possible dependencies of the instance.- Throws:
PicoCompositionException
- if one or more dependencies cannot be resolved.
-
getComponentInstance
public T getComponentInstance(PicoContainer container) throws PicoCompositionException
Description copied from interface:ComponentAdapter
Retrieve the component instance. This method will usually create a new instance each time it is called, but that is not required. For example,Cached
will always return the same instance.- Specified by:
getComponentInstance
in interfaceComponentAdapter<T>
- Overrides:
getComponentInstance
in classAbstractAdapter<T>
- Parameters:
container
- thePicoContainer
, that is used to resolve any possible dependencies of the instance.- Returns:
- the component instance.
- Throws:
PicoCompositionException
- if the component has dependencies which could not be resolved, or instantiation of the component lead to an ambigous situation within the container.
-
getComponentInstance
public abstract T getComponentInstance(PicoContainer container, java.lang.reflect.Type into) throws PicoCompositionException
Description copied from interface:ComponentAdapter
Retrieve the component instance. This method will usually create a new instance each time it is called, but that is not required. For example,Cached
will always return the same instance.- Specified by:
getComponentInstance
in interfaceComponentAdapter<T>
- Parameters:
container
- thePicoContainer
, that is used to resolve any possible dependencies of the instance.into
- the class that is about to be injected into. Use ComponentAdapter.NOTHING.class if this is not important to you.- Returns:
- the component instance.
- Throws:
PicoCompositionException
- if the component has dependencies which could not be resolved, or instantiation of the component lead to an ambiguous situation within the container.
-
decorateComponentInstance
public java.lang.Object decorateComponentInstance(PicoContainer container, java.lang.reflect.Type into, T instance)
Description copied from interface:Injector
A preexiting component instance can be injected into after instantiation- Specified by:
decorateComponentInstance
in interfaceInjector<T>
- Parameters:
container
- the container that can provide injectable dependenciesinstance
- the instance to- Returns:
-
accept
public void accept(PicoVisitor visitor)
Description copied from interface:ComponentAdapter
Accepts a visitor for this ComponentAdapter. The method is normally called by visiting aPicoContainer
, that cascades the visitor also down to all its ComponentAdapter instances.- Specified by:
accept
in interfaceComponentAdapter<T>
- Overrides:
accept
in classAbstractAdapter<T>
- Parameters:
visitor
- the visitor.
-
getDescriptor
public java.lang.String getDescriptor()
Description copied from interface:ComponentAdapter
Get a string key descriptor of the component adapter for use in toString()- Specified by:
getDescriptor
in interfaceComponentAdapter<T>
- Returns:
- the descriptor
-
newInstance
protected T newInstance(java.lang.reflect.Constructor<T> constructor, java.lang.Object[] parameters) throws java.lang.InstantiationException, java.lang.IllegalAccessException, java.lang.reflect.InvocationTargetException
Instantiate an object with given parameters and respect the accessible flag.- Parameters:
constructor
- the constructor to useparameters
- the parameters for the constructor- Returns:
- the new object.
- Throws:
java.lang.InstantiationException
java.lang.IllegalAccessException
java.lang.reflect.InvocationTargetException
-
caughtInstantiationException
protected T caughtInstantiationException(ComponentMonitor componentMonitor, java.lang.reflect.Constructor<T> constructor, java.lang.InstantiationException e, PicoContainer container)
inform monitor about component instantiation failure- Parameters:
componentMonitor
-constructor
-e
-container
-- Returns:
-
caughtIllegalAccessException
protected T caughtIllegalAccessException(ComponentMonitor componentMonitor, java.lang.reflect.Constructor<T> constructor, java.lang.IllegalAccessException e, PicoContainer container)
inform monitor about access exception.- Parameters:
componentMonitor
-constructor
-e
-container
-- Returns:
-
caughtInvocationTargetException
protected T caughtInvocationTargetException(ComponentMonitor componentMonitor, java.lang.reflect.Member member, java.lang.Object componentInstance, java.lang.reflect.InvocationTargetException e)
inform monitor about exception while instantiating component- Parameters:
componentMonitor
-member
-componentInstance
-e
-- Returns:
-
caughtIllegalAccessException
protected java.lang.Object caughtIllegalAccessException(ComponentMonitor componentMonitor, java.lang.reflect.Member member, java.lang.Object componentInstance, java.lang.IllegalAccessException e)
-
box
protected java.lang.reflect.Type box(java.lang.reflect.Type parameterType)
-
-