Interface InstanceResolver

  • All Known Subinterfaces:
    Injector
    All Known Implementing Classes:
    StandardInjector

    public interface InstanceResolver
    Provides methods to resolve types and classes to injected instances.

    All methods support filtering by qualifier annotation, by providing either an Annotation instance (obtainable via Annotations#of(Class)) or by providing an annotation Class directly (for marker annotations only). Annotations should be qualifier annotations or no matches will be found.

    Filtering by generic type is possible by creating a corresponding TypeLiteral.

    Examples:

    getInstance(Database.class)
    would return a Database instance.
    getInstance(new TypeLiteral<Provider<Database>>() {})
    would return a Provider for a Database instance.
    getInstances(Vehicle.class, Red.class)
    or
    getInstances(Vehicle.class, Annotations.of(Red.class))
    would return all Vehicles instances annotated with the @Red qualifier annotation.
    getInstance(String.class, Annotations.of(Named.class, Map.of("value", "config.url"))
    would return a String instance which was registered with a Named annotation with value "config.url".
    • Method Summary

      All Methods Instance Methods Abstract Methods 
      Modifier and Type Method Description
      <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​(TypeLiteral<T> typeLiteral, java.lang.Object... qualifiers)
      Returns an instance of the type specified by the given TypeLiteral matching the given criteria (if any) in which all dependencies are injected.
      <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​(TypeLiteral<T> typeLiteral, java.lang.Object... qualifiers)
      Returns all instances of the type specified by the given TypeLiteral matching the given criteria (if any) in which all dependencies are injected.
    • Method Detail

      • getInstance

        <T> T getInstance​(TypeLiteral<T> typeLiteral,
                          java.lang.Object... qualifiers)
                   throws UnsatisfiedResolutionException,
                          AmbiguousResolutionException,
                          CreationException,
                          ScopeNotActiveException
        Returns an instance of the type specified by the given TypeLiteral 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.
        Type Parameters:
        T - the type of the instance
        Parameters:
        typeLiteral - specifies the type of the instance required, cannot be null
        qualifiers - optional list of qualifier annotations, either Annotation or Class<? 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 created
        AmbiguousResolutionException - when multiple matching instances were available
        CreationException - when an error occurred during creation of a matching instance
        ScopeNotActiveException - when the scope for the produced type is not active
      • getInstance

        <T> T getInstance​(java.lang.Class<T> cls,
                          java.lang.Object... qualifiers)
                   throws UnsatisfiedResolutionException,
                          AmbiguousResolutionException,
                          CreationException,
                          ScopeNotActiveException
        Returns 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.
        Type Parameters:
        T - the type of the instance
        Parameters:
        cls - the class of the instance required, cannot be null
        qualifiers - optional list of qualifier annotations, either Annotation or Class<? 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 created
        AmbiguousResolutionException - when multiple matching instances were available
        CreationException - when an error occurred during creation of a matching instance
        ScopeNotActiveException - when the scope for the produced type is not active
      • getInstances

        <T> java.util.List<T> getInstances​(TypeLiteral<T> typeLiteral,
                                           java.lang.Object... qualifiers)
                                    throws CreationException
        Returns all instances of the type specified by the given TypeLiteral 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.
        Type Parameters:
        T - the type of the instances
        Parameters:
        typeLiteral - specifies 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

        <T> java.util.List<T> getInstances​(java.lang.Class<T> cls,
                                           java.lang.Object... qualifiers)
                                    throws CreationException
        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.
        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