Class ChildContext

java.lang.Object
be.imgn.inject.context.ChildContext
All Implemented Interfaces:
Context

public final class ChildContext extends Object implements Context
A child Context: nearest-ancestor local bindings first (walking up through any intermediate ChildContexts), then the parent. Unscoped parent beans are rebuilt at this — the deepest — context so their dependencies see the full child binding stack. A bean of a child-local scope (one with no Scope service) is built once by the nearest context of the chain that hosts its scope, this one included, which caches it; its dependencies resolve through that host. Other scoped beans and everything without a BeanDefinition anchor to the parent unchanged.
  • Constructor Details

    • ChildContext

      public ChildContext(Context parent, Map<Key<?>, Provider<?>> bindings)
      Creates a child context over parent.
      Parameters:
      parent - the parent context resolutions fall back to
      bindings - the child's own bindings, by key
      Throws:
      IllegalStateException - if parent is closed
    • ChildContext

      public ChildContext(Context parent, Map<Key<?>, Provider<?>> bindings, Class<? extends Annotation> scope)
      Creates a child context over parent that hosts the child-local scope: it builds and caches the instances of that scope's beans resolved through it or its descendants.
      Parameters:
      parent - the parent context resolutions fall back to
      bindings - the child's own bindings, by key
      scope - the child-local scope this context hosts
      Throws:
      IllegalArgumentException - if scope is handled by a Scope service, or if the root of the chain is not a DefaultContext
      IllegalStateException - if parent is closed
      ScopeResolutionException - if a bean of scope depends directly on a bean of another child-local scope that no context of the chain hosts; the message lists every such dependency
      UnsatisfiedDependencyException - if a bean of scope depends on a key that neither the root, nor this context, nor one of its parents binds; the message lists every such dependency
  • Method Details

    • setInjector

      public void setInjector(Injector injector)
      Attaches the Injector this context resolves Key.of(Injector.class) to. Called once by the child injector that owns this context, immediately after construction.
      Parameters:
      injector - the owning injector
      Throws:
      IllegalStateException - if an injector is already attached
    • getProvider

      public <T> Provider<T> getProvider(Key<T> key)
      Description copied from interface: Context
      Returns the provider bound to key.
      Specified by:
      getProvider in interface Context
      Type Parameters:
      T - the bound type
      Parameters:
      key - the binding key
      Returns:
      the provider bound to key
    • createScoped

      public <T> T createScoped(ScopedProduct product, @Nullable Consumer<? super T> preDestroy, Function<? super Context, ? extends T> instance)
      Description copied from interface: Context
      Creates the instance of a child-local assisted product, and caches it in the nearest context of the chain that hosts its scope, by its key. Generated assisted factories call this for a recipe with a child-local scope.

      The default rejects every call, so minimal Context implementations need not host scopes.

      Specified by:
      createScoped in interface Context
      Type Parameters:
      T - the product type
      Parameters:
      product - the product to create
      preDestroy - the product's @PreDestroy callback, run when its host closes, or null
      instance - builds the instance through the context it is given: the host's, so the product's dependencies resolve where it is cached
      Returns:
      the created instance
    • getBeanDefinition

      public Optional<BeanDefinition> getBeanDefinition(Key<?> key)
      Description copied from interface: Context
      Returns the definition bound to key, if any.

      Introspection only; the default is empty so minimal Context implementations need not supply it.

      Specified by:
      getBeanDefinition in interface Context
      Parameters:
      key - the binding key, qualified or not
      Returns:
      the definition bound to key, if any
    • getBeanDefinitionsByStereotype

      public List<BeanDefinition> getBeanDefinitionsByStereotype(Class<? extends Annotation> stereotype)
      Description copied from interface: Context
      Returns every registered definition whose bean is annotated with an annotation carrying stereotype.
      Specified by:
      getBeanDefinitionsByStereotype in interface Context
      Parameters:
      stereotype - the meta-annotation to filter by, such as QualifyingAnnotation.class
      Returns:
      every registered definition whose bean is annotated with an annotation carrying stereotype
    • interceptor

      public Provider<MethodInterceptor> interceptor(Class<? extends Annotation> annotationType)
      Description copied from interface: Context
      Returns the provider for the interceptor handling annotationType.

      The default rejects every lookup, so minimal Context implementations need not supply an index.

      Specified by:
      interceptor in interface Context
      Parameters:
      annotationType - the intercepting annotation
      Returns:
      the provider for the interceptor handling annotationType
    • constructed

      public <T> T constructed(T instance)
      Description copied from interface: Context
      Reports a fully built instance to the ConstructionListeners, with the injector of this context. Generated factories call this for every instance they build. The default reports nothing.
      Specified by:
      constructed in interface Context
      Type Parameters:
      T - the instance type
      Parameters:
      instance - the fully built instance
      Returns:
      instance
    • close

      public void close()
      Closes this child context: rejects further resolution through it, including through the providers it returned, closes its live child contexts, newest first, then destroys the instances of its hosted scope (invoking each factory's @PreDestroy) in reverse creation order. Failures are collected as suppressed exceptions on the first one thrown, so every child and every instance is still given a chance to close or destroy. Never closes the parent. Idempotent.
      Specified by:
      close in interface Context