Class Injector

java.lang.Object
io.activej.inject.Injector
All Implemented Interfaces:
ResourceLocator

public final class Injector extends Object implements ResourceLocator
Injector is the main working component of the ActiveJ Inject.

It stores a trie of binding graphs and a cache of already made singletons.

Each injector is associated with exactly zero or one instance per Key.

Injector uses binding graph at the root of the trie to recursively create and then store instances of objects associated with some keys. Branches of the trie are used to enter scopes.

  • Method Details

    • useSpecializer

      public static void useSpecializer()
      Enables specialization of compiled bindings. Depends on ActiveJ-Specializer module
    • of

      public static Injector of(Module... modules)
      This constructor combines given modules and then compiles them.
    • of

      public static Injector of(@Nullable @Nullable Injector parent, Module... modules)
    • of

      public static Injector of(Trie<Scope,Map<Key<?>,Binding<?>>> bindings)
      This constructor is a shortcut for threadsafe compile with no instance overrides and no multibinders, transformers or generators.
    • compile

      public static Injector compile(@Nullable @Nullable Injector parent, Module module)
      This constructor threadsafely compiles given module, extracting bindings and their multibinders, transformers and generators from it, with no instance overrides
    • compile

      public static Injector compile(@Nullable @Nullable Injector parent, Scope[] scope, Trie<Scope,Map<Key<?>,Set<Binding<?>>>> bindingsMultimap, Multibinder<?> multibinder, BindingTransformer<?> transformer, BindingGenerator<?> generator)
      The most full-fledged compile method that allows you to create an Injector of any configuration.

      Note that any injector always sets a binding of Injector key to provide itself.

      Parameters:
      parent - parent injector that is called when this injector cannot fulfill the request
      scope - the scope of the injector, can be described as 'prefix of the root' of the binding trie, used when entering scopes
      bindingsMultimap - a trie of binding set graph with multiple possible conflicting bindings per key that are resolved as part of the compilation.
      multibinder - a multibinder that is called on every binding conflict (see Multibinders.combinedMultibinder(java.util.Map<io.activej.inject.Key<?>, io.activej.inject.binding.Multibinder<?>>))
      transformer - a transformer that is called on every binding once (see BindingTransformers.combinedTransformer(java.util.Map<io.activej.inject.KeyPattern<?>, java.util.Set<io.activej.inject.binding.BindingTransformer<?>>>))
      generator - a generator that is called on every missing binding (see BindingGenerators.combinedGenerator(java.util.Map<io.activej.inject.KeyPattern<?>, java.util.Set<io.activej.inject.binding.BindingGenerator<?>>>))
      See Also:
    • getInstance

      public <T> T getInstance(Key<T> key)
      Returns an instance for given key. At first call an instance is created once for non-transient bindings and next calls to this method will return the same instance.

      This method throws an exception if a binding was not bound for given key, or if a binding refused to make an instance for some reason (returned null).

      Specified by:
      getInstance in interface ResourceLocator
    • getInstanceOrNull

      @Nullable public <T> T getInstanceOrNull(Key<T> key)
      Same as getInstance(Key) except that it returns null instead of throwing an exception.
      Specified by:
      getInstanceOrNull in interface ResourceLocator
    • getInstanceProvider

      public <T> InstanceProvider<T> getInstanceProvider(Key<T> key)
      A shortcut for getInstance(new Key<InstanceProvider<T>>(){})
    • getInstanceProvider

      public <T> InstanceProvider<T> getInstanceProvider(Class<T> type)
      A shortcut for getInstanceProvider(Key.of(type))
      See Also:
    • getInstanceInjector

      public <T> InstanceInjector<T> getInstanceInjector(Key<T> key)
      A shortcut for getInstance(new Key<InstanceInjector<T>>(){})
    • getInstanceInjector

      public <T> InstanceInjector<T> getInstanceInjector(Class<T> type)
      A shortcut for getInstanceInjector(Key.of(type))
      See Also:
    • getOptionalDependency

      public <T> OptionalDependency<T> getOptionalDependency(Key<T> key)
      A shortcut for getInstance(new Key<OptionalDependency<T>>(){})
    • getOptionalDependency

      public <T> OptionalDependency<T> getOptionalDependency(Class<T> type)
      A shortcut for getOptionalDependency(Key.of(type))
      See Also:
    • createEagerInstances

      public void createEagerInstances()
    • peekInstance

      @Nullable public <T> T peekInstance(Key<T> key)
      This method returns an instance only if it already was created by a getInstance(io.activej.inject.Key<T>) call before, it does not trigger instance creation.
    • peekInstance

      @Nullable public <T> T peekInstance(Class<T> type)
      This method returns an instance only if it already was created by a getInstance(io.activej.inject.Key<T>) call before, it does not trigger instance creation.
      See Also:
    • hasInstance

      public boolean hasInstance(Key<?> key)
      This method checks if an instance for this key was created by a getInstance(io.activej.inject.Key<T>) call before.
    • hasInstance

      public boolean hasInstance(Class<?> type)
      This method checks if an instance for this key was created by a getInstance(io.activej.inject.Key<T>) call before.
      See Also:
    • peekInstances

      public Map<Key<?>,Object> peekInstances()
      This method returns a copy of the injector cache - a map of all already created non-transient instances at the current scope.
    • putInstance

      public <T> void putInstance(Key<T> key, T instance)
      This method puts an instance into a cache slot of given key, meaning that already existing instance would be replaced or a binding would never be actually called.

      Use this at your own risk, this allows high control over the injector, but can be easily abused.

    • putInstance

      public <T> void putInstance(Class<T> key, T instance)
      This method puts an instance into a cache slot of given key, meaning that already existing instance would be replaced or a binding would never be actually called.

      Use this at your own risk, this allows high control over the injector, but can be easily abused.

      See Also:
    • getBinding

      @Nullable public @Nullable Binding<?> getBinding(Class<?> type)
    • getBinding

      @Nullable public @Nullable Binding<?> getBinding(Key<?> key)
    • hasBinding

      public boolean hasBinding(Key<?> key)
      This method returns true if a binding was bound for given key.
    • hasBinding

      public boolean hasBinding(Class<?> type)
      This method returns true if a binding was bound for given class key.
      See Also:
    • enterScope

      public Injector enterScope(Scope scope)
      Creates an injector that operates on a binding graph at a given prefix (scope) of the binding graph trie and this injector as its parent.
    • getParent

      @Nullable public @Nullable Injector getParent()
    • getScope

      public Scope[] getScope()
    • getBindings

      public Map<Key<?>,Binding<?>> getBindings()
      This method returns bindings for current scope

      Note that this method expensive to call repeatedly

    • getBindingsTrie

      public Trie<Scope,Map<Key<?>,Binding<?>>> getBindingsTrie()
      This method returns a trie of bindings

      Note that this method expensive to call repeatedly

    • toString

      public String toString()
      Overrides:
      toString in class Object