Class StandardInjector

    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      boolean contains​(java.lang.reflect.Type type, java.lang.Object... qualifiers)
      Returns true when the given type with the given qualifiers is present, otherwise false.
      CandidateRegistry getCandidateRegistry()
      Returns a CandidateRegistry, which can be shared instead of this class to share only methods that can be used to register and remove objects.
      <T> T getInstance​(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> T getInstance​(java.lang.reflect.Type type, java.lang.Object... qualifiers)
      Returns an instance of the given Type matching the given criteria (if any) in which all dependencies are injected.
      InstanceResolver getInstanceResolver()
      Returns an InstanceResolver, 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 given Type matching the given criteria (if any) in which all dependencies are injected.
      void register​(java.lang.reflect.Type concreteType)
      Registers a Type, 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.
      void register​(java.util.Collection<java.lang.reflect.Type> concreteTypes)
      Registers the given Types, 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.
      void registerInstance​(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.
      void remove​(java.lang.reflect.Type concreteType)
      Removes the given Type, and all its derived candidates if any, if doing so would not result maintains all invariants for the remaining registered types.
      void remove​(java.util.Collection<java.lang.reflect.Type> concreteTypes)
      Removes the given Types, all their derived candidates if any, if doing so would not result maintains all invariants for the remaining registered types.
      void removeInstance​(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.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • 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 of InjectionTargetExtensions, cannot be null or contain null but can be empty
        typeRegistrationExtensions - a collection of TypeRegistrationExtensions, cannot be null or contain null but can be empty
        scopeResolvers - a list of ScopeResolvers, cannot be null or contain null but can be empty
        strategy - an InjectorStrategy, cannot be null
        autoDiscovery - true if 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

      • getInstances

        public <T> java.util.List<T> getInstances​(java.lang.reflect.Type type,
                                                  java.lang.Object... qualifiers)
                                           throws CreationException
        Description copied from interface: InstanceResolver
        Returns all instances of the given Type 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:
        getInstances in interface InstanceResolver
        Type Parameters:
        T - the type of the instances
        Parameters:
        type - the Type of the instances required, cannot be null
        qualifiers - optional list of qualifier annotations, either Annotation or Class<? extends Annotation>
        Returns:
        all instances of the given Type matching the given criteria (if any), never null, 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 CreationException
        Description copied from interface: InstanceResolver
        Returns 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:
        getInstances in interface InstanceResolver
        Type Parameters:
        T - the type of the instances
        Parameters:
        cls - the class of the instances required, cannot be null
        qualifiers - optional list of qualifier annotations, either Annotation or Class<? 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: CandidateRegistry
        Returns true when the given type with the given qualifiers is present, otherwise false.
        Specified by:
        contains in interface CandidateRegistry
        Parameters:
        type - a type to check for, cannot be null
        qualifiers - optional list of qualifier annotations, either Annotation or Class<? extends Annotation>
        Returns:
        true when the given type with the given criteria is present, otherwise false
      • register

        public void register​(java.lang.reflect.Type concreteType)
                      throws AutoDiscoveryException,
                             DefinitionException,
                             DependencyException
        Description copied from interface: CandidateRegistry
        Registers a Type, 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:
        register in interface CandidateRegistry
        Parameters:
        concreteType - the type to register, cannot be null
        Throws:
        AutoDiscoveryException - when auto discovery fails to find all required types
        DefinitionException - when a definition problem was encountered
        DependencyException - when dependencies between registered types cannot be resolved
      • register

        public void register​(java.util.Collection<java.lang.reflect.Type> concreteTypes)
                      throws AutoDiscoveryException,
                             DefinitionException,
                             DependencyException
        Description copied from interface: CandidateRegistry
        Registers the given Types, 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:
        register in interface CandidateRegistry
        Parameters:
        concreteTypes - a collection of types to register, cannot be null or contain nulls
        Throws:
        AutoDiscoveryException - when auto discovery fails to find all required types
        DefinitionException - when a definition problem was encountered
        DependencyException - when dependencies between registered types cannot be resolved
      • registerInstance

        public void registerInstance​(java.lang.Object instance,
                                     java.lang.annotation.Annotation... qualifiers)
                              throws DefinitionException,
                                     DependencyException
        Description copied from interface: CandidateRegistry
        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.

        If registering this instance would result in ambiguous dependencies for previously registered classes, then this method will throw an exception.

        Specified by:
        registerInstance in interface CandidateRegistry
        Parameters:
        instance - the instance to register with the Injector
        qualifiers - optional list of qualifiers for this instance
        Throws:
        DefinitionException - when a definition problem was encountered
        DependencyException - when dependencies between registered types cannot be resolved
      • remove

        public void remove​(java.util.Collection<java.lang.reflect.Type> concreteTypes)
                    throws AutoDiscoveryException,
                           DefinitionException,
                           DependencyException
        Description copied from interface: CandidateRegistry
        Removes the given Types, 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:
        remove in interface CandidateRegistry
        Parameters:
        concreteTypes - a collection of types to remove, cannot be null or contain nulls
        Throws:
        AutoDiscoveryException - when auto discovery fails to find all required types
        DefinitionException - when a definition problem was encountered
        DependencyException - when dependencies between registered types cannot be resolved
      • removeInstance

        public void removeInstance​(java.lang.Object instance,
                                   java.lang.annotation.Annotation... qualifiers)
                            throws DefinitionException,
                                   DependencyException
        Description copied from interface: CandidateRegistry
        Removes 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:
        removeInstance in interface CandidateRegistry
        Parameters:
        instance - the instance to remove, cannot be null
        qualifiers - optional list of qualifiers for this instance
        Throws:
        DefinitionException - when a definition problem was encountered
        DependencyException - when dependencies between registered types cannot be resolved