Interface IInjector


public interface IInjector
An injector is used to inject data from the object supplier into a domain object. The injection will try to find fields and methods in the user objects that correspond to the names of the services present in the context. Subsequent changes to the context after injection will cause the affected items to be re-injected into the object. Thus the object will remain synchronized with the context once it has been injected.

Matching of methods and fields to be injected is performed using the annotations defined in packages jakarta.inject and org.eclipse.e4.core.services.annotations.

The injection of values is generally done as a number of calls. User objects that want to finalize the injected data (for instance, to perform calculations based on multiple injected values) can place such calculations in a method with the PostConstruct annotation.

When injecting values, all fields are injected prior to injection of methods. When values are removed from the context or the context is disposed, injection of null values occurs in the reverse order: methods and then fields. As a result, injection methods can safely make use of injected field values. The order in which methods are injected is undefined, so injection methods should not rely on other injection methods having been run already. Methods and field on superclasses are injected before methods in fields on the subclasses.

When supplier is disposed, the injector will attempt to notify all injected objects by calling methods with the PreDestroy annotation.

Since:
1.7
Restriction:
This interface is not intended to be implemented by clients.
Restriction:
This interface is not intended to be extended by clients.
  • Field Details

    • NOT_A_VALUE

      static final Object NOT_A_VALUE
      Methods may return this to indicate that the requested object was not found wherever null can be a valid return value.
  • Method Details

    • inject

      void inject(Object object, PrimaryObjectSupplier objectSupplier) throws InjectionException
      Injects data from the supplier into a domain object. See the class comment for details on the injection algorithm that is used.
      Parameters:
      object - the object to perform injection on
      objectSupplier - primary object supplier for the injection
      Throws:
      InjectionException - if an exception occurred while performing this operation
    • inject

      void inject(Object object, PrimaryObjectSupplier objectSupplier, PrimaryObjectSupplier staticSupplier) throws InjectionException
      Injects data from the supplier into a domain object. See the class comment for details on the injection algorithm that is used.

      If values for the same key present in both the object supplier and the static supplier, the values from the static supplier are injected. Injected values from the static supplier are not tracked and no links between the static supplier and the object are established.

      Parameters:
      object - the object to perform injection on
      objectSupplier - primary object supplier for the injection
      staticSupplier - additional object supplier for the injection, changes in injected values are not tracked
      Throws:
      InjectionException - if an exception occurred while performing this operation
      Since:
      1.7
    • uninject

      void uninject(Object object, PrimaryObjectSupplier objectSupplier) throws InjectionException
      Un-injects the supplier from the object.
      Parameters:
      object - the domain object previously injected with the supplier's data
      objectSupplier - primary object supplier for the injection
      Throws:
      InjectionException - if an exception occurred while performing this operation
    • invoke

      Object invoke(Object object, Class<? extends Annotation> qualifier, PrimaryObjectSupplier objectSupplier) throws InjectionException
      Call the annotated method on an object, injecting the parameters from the supplier.

      If no matching method is found on the class, an InjectionException will be thrown.

      Parameters:
      object - the object on which the method should be called
      qualifier - the annotation tagging method to be called
      objectSupplier - primary object supplier
      Returns:
      the return value of the method call, might be null
      Throws:
      InjectionException - if an exception occurred while performing this operation
    • invoke

      Object invoke(Object object, Class<? extends Annotation> qualifier, Object defaultValue, PrimaryObjectSupplier objectSupplier) throws InjectionException
      Call the annotated method on an object, injecting the parameters from the supplier.

      If no matching method is found on the class, the defaultValue will be returned.

      Parameters:
      object - the object on which the method should be called
      qualifier - the annotation tagging method to be called
      defaultValue - a value to be returned if the method cannot be called, might be null
      objectSupplier - primary object supplier
      Returns:
      the return value of the method call, might be null
      Throws:
      InjectionException - if an exception occurred while performing this operation
    • invoke

      Object invoke(Object object, Class<? extends Annotation> qualifier, Object defaultValue, PrimaryObjectSupplier objectSupplier, PrimaryObjectSupplier localSupplier) throws InjectionException
      Call the annotated method on an object, injecting the parameters from the suppliers.

      If no matching method is found on the class, the defaultValue will be returned.

      Parameters:
      object - the object on which the method should be called
      qualifier - the annotation tagging method to be called
      defaultValue - a value to be returned if the method cannot be called, might be null
      objectSupplier - primary object supplier
      localSupplier - primary object supplier, values override objectSupplier
      Returns:
      the return value of the method call, might be null
      Throws:
      InjectionException - if an exception occurred while performing this operation
    • make

      <T> T make(Class<T> clazz, PrimaryObjectSupplier objectSupplier) throws InjectionException
      Obtain an instance of the specified class and inject it with the data from the supplier.
      Type Parameters:
      T - the type of the object to be created
      Parameters:
      clazz - the class to be instantiated
      objectSupplier - primary object supplier for the injection
      Returns:
      an instance of the specified class
      Throws:
      InjectionException - if an exception occurred while performing this operation
      See Also:
      • Scope
      • Singleton
    • make

      <T> T make(Class<T> clazz, PrimaryObjectSupplier objectSupplier, PrimaryObjectSupplier staticSupplier) throws InjectionException
      Obtain an instance of the specified class and inject it with the data from the supplier.

      If values for the same key present in both the object supplier and the static supplier, the values from the static supplier are injected. Injected values from the static supplier are not tracked and no links between the static supplier and the object are established.

      Type Parameters:
      T - the type of the object to be created
      Parameters:
      clazz - the class to be instantiated
      objectSupplier - primary object supplier for the injection
      staticSupplier - additional object supplier for the injection, changes in injected values are not tracked
      Returns:
      an instance of the specified class
      Throws:
      InjectionException - if an exception occurred while performing this operation
      See Also:
      • Scope
      • Singleton
    • addBinding

      IBinding addBinding(Class<?> clazz)
      Creates a binding for the specified class and adds it to the injector.
      Parameters:
      clazz - the class that the injector should be aware of
      Returns:
      binding for the specified class
    • addBinding

      IBinding addBinding(IBinding binding)
      Adds binding to the injector.
      Parameters:
      binding - the binding to add to the injector
      Returns:
      the binding added to the injector
    • setDefaultSupplier

      void setDefaultSupplier(PrimaryObjectSupplier objectSupplier)
      Specifies object supplier to inject internally created objects, such as extended object suppliers.
      Parameters:
      objectSupplier - the object supplier used to inject objects implicitly created by this injector
      Since:
      1.2