Class StandardInjector
- java.lang.Object
-
- org.int4.dirk.core.StandardInjector
-
- All Implemented Interfaces:
CandidateRegistry,Injector,InstanceResolver
public class StandardInjector extends java.lang.Object implements Injector
A standard implementation ofInjectorprovided with the framework.
-
-
Constructor Summary
Constructors Constructor Description StandardInjector(java.util.Collection<InjectionTargetExtension<?,?>> injectionTargetExtensions, java.util.Collection<TypeRegistrationExtension> typeRegistrationExtensions, java.util.List<ScopeResolver> scopeResolvers, InjectorStrategy strategy, boolean autoDiscovery)Constructs a new instance.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description booleancontains(java.lang.reflect.Type type, java.lang.Object... qualifiers)Returnstruewhen the given type with the given qualifiers is present, otherwisefalse.CandidateRegistrygetCandidateRegistry()Returns aCandidateRegistry, which can be shared instead of this class to share only methods that can be used to register and remove objects.<T> TgetInstance(java.lang.Class<T> cls, java.lang.Object... qualifiers)Returns an instance of the given class matching the given criteria (if any) in which all dependencies are injected.<T> TgetInstance(java.lang.reflect.Type type, java.lang.Object... qualifiers)Returns an instance of the givenTypematching the given criteria (if any) in which all dependencies are injected.InstanceResolvergetInstanceResolver()Returns anInstanceResolver, which can be shared instead of this class to share only methods that can be used to instantiate objects.<T> java.util.List<T>getInstances(java.lang.Class<T> cls, java.lang.Object... qualifiers)Returns all instances of the given class matching the given criteria (if any) in which all dependencies are injected.<T> java.util.List<T>getInstances(java.lang.reflect.Type type, java.lang.Object... qualifiers)Returns all instances of the givenTypematching the given criteria (if any) in which all dependencies are injected.voidregister(java.lang.reflect.Type concreteType)Registers aType, and all its derived candidates if any, if all its dependencies can be resolved and it would not cause existing registered types to have ambiguous dependencies as a result of registering the given type.voidregister(java.util.Collection<java.lang.reflect.Type> concreteTypes)Registers the givenTypes, and all their derived candidates if any, if all their dependencies can be resolved and it would not cause existing registered types to have ambiguous dependencies as a result of registering the given types.voidregisterInstance(java.lang.Object instance, java.lang.annotation.Annotation... qualifiers)Registers an instance, and all its derived candidates if any, as a singleton if it would not cause existing registered types to have ambiguous dependencies as a result.voidremove(java.lang.reflect.Type concreteType)Removes the givenType, and all its derived candidates if any, if doing so would not result maintains all invariants for the remaining registered types.voidremove(java.util.Collection<java.lang.reflect.Type> concreteTypes)Removes the givenTypes, all their derived candidates if any, if doing so would not result maintains all invariants for the remaining registered types.voidremoveInstance(java.lang.Object instance, java.lang.annotation.Annotation... qualifiers)Removes an instance, and all its derived candidates if any, if doing so would not result in broken dependencies in the remaining registered types.
-
-
-
Constructor Detail
-
StandardInjector
public StandardInjector(java.util.Collection<InjectionTargetExtension<?,?>> injectionTargetExtensions, java.util.Collection<TypeRegistrationExtension> typeRegistrationExtensions, java.util.List<ScopeResolver> scopeResolvers, InjectorStrategy strategy, boolean autoDiscovery)
Constructs a new instance.- Parameters:
injectionTargetExtensions- a collection ofInjectionTargetExtensions, cannot benullor containnullbut can be emptytypeRegistrationExtensions- a collection ofTypeRegistrationExtensions, cannot benullor containnullbut can be emptyscopeResolvers- a list ofScopeResolvers, cannot benullor containnullbut can be emptystrategy- anInjectorStrategy, cannot benullautoDiscovery-trueif the injector should automatically register (auto discover) types encountered during instantiation that have not been explicitly registered, or {code false} to allow manual registration only
-
-
Method Detail
-
getInstanceResolver
public InstanceResolver getInstanceResolver()
Description copied from interface:InjectorReturns anInstanceResolver, which can be shared instead of this class to share only methods that can be used to instantiate objects.- Specified by:
getInstanceResolverin interfaceInjector- Returns:
- an
InstanceResolver, nevernull
-
getCandidateRegistry
public CandidateRegistry getCandidateRegistry()
Description copied from interface:InjectorReturns aCandidateRegistry, which can be shared instead of this class to share only methods that can be used to register and remove objects.- Specified by:
getCandidateRegistryin interfaceInjector- Returns:
- a
CandidateRegistry, nevernull
-
getInstance
public <T> T getInstance(java.lang.reflect.Type type, java.lang.Object... qualifiers) throws UnsatisfiedResolutionException, AmbiguousResolutionException, CreationException, ScopeNotActiveExceptionDescription copied from interface:InstanceResolverReturns an instance of the givenTypematching the given criteria (if any) in which all dependencies are injected. The instance returned can either be an existing instance or newly created depending on its scope.- Specified by:
getInstancein interfaceInstanceResolver- Type Parameters:
T- the type of the instance- Parameters:
type- the type of the instance required, cannot benullqualifiers- optional list of qualifier annotations, eitherAnnotationorClass<? extends Annotation>- Returns:
- an instance of the given class matching the given criteria, never
null - Throws:
UnsatisfiedResolutionException- when no matching instance was available or could be createdAmbiguousResolutionException- when multiple matching instances were availableCreationException- when an error occurred during creation of a matching instanceScopeNotActiveException- when the scope for the produced type is not active
-
getInstance
public <T> T getInstance(java.lang.Class<T> cls, java.lang.Object... qualifiers) throws UnsatisfiedResolutionException, AmbiguousResolutionException, CreationException, ScopeNotActiveExceptionDescription copied from interface:InstanceResolverReturns an instance of the given class matching the given criteria (if any) in which all dependencies are injected. The instance returned can either be an existing instance or newly created depending on its scope.- Specified by:
getInstancein interfaceInstanceResolver- Type Parameters:
T- the type of the instance- Parameters:
cls- the class of the instance required, cannot benullqualifiers- optional list of qualifier annotations, eitherAnnotationorClass<? extends Annotation>- Returns:
- an instance of the given class matching the given criteria (if any)
- Throws:
UnsatisfiedResolutionException- when no matching instance was available or could be createdAmbiguousResolutionException- when multiple matching instances were availableCreationException- when an error occurred during creation of a matching instanceScopeNotActiveException- when the scope for the produced type is not active
-
getInstances
public <T> java.util.List<T> getInstances(java.lang.reflect.Type type, java.lang.Object... qualifiers) throws CreationExceptionDescription copied from interface:InstanceResolverReturns all instances of the givenTypematching the given criteria (if any) in which all dependencies are injected. When there are no matches, an empty set is returned. The instances returned can either be existing instances or newly created depending on their scope or a mix thereof.- Specified by:
getInstancesin interfaceInstanceResolver- Type Parameters:
T- the type of the instances- Parameters:
type- theTypeof the instances required, cannot benullqualifiers- optional list of qualifier annotations, eitherAnnotationorClass<? extends Annotation>- Returns:
- all instances of the given
Typematching the given criteria (if any), nevernull, can be empty - Throws:
CreationException- when an error occurred during creation of a matching instance
-
getInstances
public <T> java.util.List<T> getInstances(java.lang.Class<T> cls, java.lang.Object... qualifiers) throws CreationExceptionDescription copied from interface:InstanceResolverReturns all instances of the given class matching the given criteria (if any) in which all dependencies are injected. When there are no matches, an empty set is returned. The instances returned can either be existing instances or newly created depending on their scope or a mix thereof.- Specified by:
getInstancesin interfaceInstanceResolver- Type Parameters:
T- the type of the instances- Parameters:
cls- the class of the instances required, cannot benullqualifiers- optional list of qualifier annotations, eitherAnnotationorClass<? extends Annotation>- Returns:
- all instances of the given class matching the given criteria (if any), never
null, can be empty - Throws:
CreationException- when an error occurred during creation of a matching instance
-
contains
public boolean contains(java.lang.reflect.Type type, java.lang.Object... qualifiers)Description copied from interface:CandidateRegistryReturnstruewhen the given type with the given qualifiers is present, otherwisefalse.- Specified by:
containsin interfaceCandidateRegistry- Parameters:
type- a type to check for, cannot benullqualifiers- optional list of qualifier annotations, eitherAnnotationorClass<? extends Annotation>- Returns:
truewhen the given type with the given criteria is present, otherwisefalse
-
register
public void register(java.lang.reflect.Type concreteType) throws AutoDiscoveryException, DefinitionException, DependencyExceptionDescription copied from interface:CandidateRegistryRegisters aType, and all its derived candidates if any, if all its dependencies can be resolved and it would not cause existing registered types to have ambiguous dependencies as a result of registering the given type.If there are unresolvable dependencies, or registering this type would result in ambiguous dependencies for previously registered types, then this method will throw an exception.
- Specified by:
registerin interfaceCandidateRegistry- Parameters:
concreteType- the type to register, cannot benull- Throws:
AutoDiscoveryException- when auto discovery fails to find all required typesDefinitionException- when a definition problem was encounteredDependencyException- when dependencies between registered types cannot be resolved
-
register
public void register(java.util.Collection<java.lang.reflect.Type> concreteTypes) throws AutoDiscoveryException, DefinitionException, DependencyExceptionDescription copied from interface:CandidateRegistryRegisters the givenTypes, and all their derived candidates if any, if all their dependencies can be resolved and it would not cause existing registered types to have ambiguous dependencies as a result of registering the given types.If there are unresolvable dependencies, or registering these types would result in ambiguous dependencies for previously registered types, then this method will throw an exception.
- Specified by:
registerin interfaceCandidateRegistry- Parameters:
concreteTypes- a collection of types to register, cannot benullor containnulls- Throws:
AutoDiscoveryException- when auto discovery fails to find all required typesDefinitionException- when a definition problem was encounteredDependencyException- when dependencies between registered types cannot be resolved
-
registerInstance
public void registerInstance(java.lang.Object instance, java.lang.annotation.Annotation... qualifiers) throws DefinitionException, DependencyExceptionDescription copied from interface:CandidateRegistryRegisters an instance, and all its derived candidates if any, as a singleton if it would not cause existing registered types to have ambiguous dependencies as a result.If registering this instance would result in ambiguous dependencies for previously registered classes, then this method will throw an exception.
- Specified by:
registerInstancein interfaceCandidateRegistry- Parameters:
instance- the instance to register with the Injectorqualifiers- optional list of qualifiers for this instance- Throws:
DefinitionException- when a definition problem was encounteredDependencyException- when dependencies between registered types cannot be resolved
-
remove
public void remove(java.lang.reflect.Type concreteType) throws AutoDiscoveryException, DefinitionException, DependencyExceptionDescription copied from interface:CandidateRegistryRemoves the givenType, and all its derived candidates if any, if doing so would not result maintains all invariants for the remaining registered types.If there would be broken dependencies then the removal will fail and an exception is thrown.
- Specified by:
removein interfaceCandidateRegistry- Parameters:
concreteType- the type to remove, cannot benull- Throws:
AutoDiscoveryException- when auto discovery fails to find all required typesDefinitionException- when a definition problem was encounteredDependencyException- when dependencies between registered types cannot be resolved
-
remove
public void remove(java.util.Collection<java.lang.reflect.Type> concreteTypes) throws AutoDiscoveryException, DefinitionException, DependencyExceptionDescription copied from interface:CandidateRegistryRemoves the givenTypes, all their derived candidates if any, if doing so would not result maintains all invariants for the remaining registered types.If there would be broken dependencies then the removal will fail and an exception is thrown.
- Specified by:
removein interfaceCandidateRegistry- Parameters:
concreteTypes- a collection of types to remove, cannot benullor containnulls- Throws:
AutoDiscoveryException- when auto discovery fails to find all required typesDefinitionException- when a definition problem was encounteredDependencyException- when dependencies between registered types cannot be resolved
-
removeInstance
public void removeInstance(java.lang.Object instance, java.lang.annotation.Annotation... qualifiers) throws DefinitionException, DependencyExceptionDescription copied from interface:CandidateRegistryRemoves an instance, and all its derived candidates if any, if doing so would not result in broken dependencies in the remaining registered types.If there would be broken dependencies then the removal will fail and an exception is thrown.
- Specified by:
removeInstancein interfaceCandidateRegistry- Parameters:
instance- the instance to remove, cannot benullqualifiers- optional list of qualifiers for this instance- Throws:
DefinitionException- when a definition problem was encounteredDependencyException- when dependencies between registered types cannot be resolved
-
-