Interface InstanceResolver

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

    public interface InstanceResolver
    Provides methods to resolve Types to instances.

    All methods support filtering by qualifier annotation, by providing either an Annotation instance (obtainable via Annotations#of(Class)) or by providing a Class instance of <? extends Annotation>. Annotations should be qualifier annotations or no matches will be found.

    Methods that can return multiple instances also support a Predicate of Type to allow custom filtering.

    Filtering by generic type is possible by providing ParameterizedType or a WildcardType. There are various ways to construct such types, see for example org.int4.dirk.util.Types and org.int4.dirk.util.TypeReference.

    Examples:

    getInstance(Database.class)
    would return a Database instance.
    getInstance(Types.wildcardExtends(Database.class, Queryable.class))
    would return an object which implements or extends both Database and Queryable.
    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​(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.
      <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.
    • Method Detail

      • getInstance

        <T> T getInstance​(java.lang.reflect.Type type,
                          java.lang.Object... qualifiers)
                   throws UnsatisfiedResolutionException,
                          AmbiguousResolutionException,
                          CreationException,
                          ScopeNotActiveException
        Returns an instance of the given Type 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:
        type - 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​(java.lang.reflect.Type type,
                                           java.lang.Object... qualifiers)
                                    throws CreationException
        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.
        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

        <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