Class ContextInjectionFactory

java.lang.Object
org.eclipse.e4.core.contexts.ContextInjectionFactory

public final class ContextInjectionFactory extends Object
An injection factory is used to inject data and services from a context 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.

If annotations are supported by the runtime, matching of methods and fields to be injected is also performed using the annotations defined in packages jakarta.inject and org.eclipse.e4.core.di.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 jakarta.annotation.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 a context is disposed, the injection factory will attempt to notify all injected objects by calling methods with the jakarta.annotation.PreDestroy annotation. This class is not intended to be extended by clients.

Since:
1.3
Restriction:
This class is not intended to be instantiated by clients.
  • Method Details

    • inject

      public static void inject(Object object, IEclipseContext context) throws InjectionException
      Injects a context 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
      context - The context to obtain injected values from
      Throws:
      InjectionException - if an exception occurred while performing this operation
    • inject

      public static void inject(Object object, IEclipseContext context, IEclipseContext staticContext) throws InjectionException
      Injects a context into a domain object. See the class comment for details on the injection algorithm that is used.

      This method allows extra values that don't need to be tracked to be passed to the object using staticContext. If values for the same key present in both the context and the static context, the values from the static context are injected.

      Parameters:
      object - The object to perform injection on
      context - The context to obtain injected values from
      staticContext - The context containing extra values; not tracked
      Throws:
      InjectionException - if an exception occurred while performing this operation
      Since:
      1.7
      See Also:
    • invoke

      public static Object invoke(Object object, Class<? extends Annotation> qualifier, IEclipseContext context) throws InjectionException
      Call a method, injecting the parameters from the context.

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

      Parameters:
      object - The object to perform injection on
      qualifier - the annotation tagging method to be called
      context - The context to obtain injected values from
      Returns:
      the return value of the method call, might be null
      Throws:
      InjectionException - if an exception occurred while performing this operation
    • invoke

      public static Object invoke(Object object, Class<? extends Annotation> qualifier, IEclipseContext context, Object defaultValue) throws InjectionException
      Call a method, injecting the parameters from the context.

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

      Parameters:
      object - The object to perform injection on
      qualifier - the annotation tagging method to be called
      context - The context to obtain injected values from
      defaultValue - A value to be returned if the method cannot be called, might be null
      Returns:
      the return value of the method call, might be null
      Throws:
      InjectionException - if an exception occurred while performing this operation
    • invoke

      public static Object invoke(Object object, Class<? extends Annotation> qualifier, IEclipseContext context, IEclipseContext localContext, Object defaultValue) throws InjectionException
      Call a method, injecting the parameters from two contexts. This method is useful when the method needs to receive some values not present in the context. In this case a local context can be created and populated with additional values.

      If values for the same key present in both the context and the local context, the values from the local context are injected.

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

      Parameters:
      object - The object to perform injection on
      qualifier - the annotation tagging method to be called
      context - The context to obtain injected values from
      localContext - The context to obtain addition injected values from
      defaultValue - A value to be returned if the method cannot be called, might be null
      Returns:
      the return value of the method call, might be null
      Throws:
      InjectionException - if an exception occurred while performing this operation
    • uninject

      public static void uninject(Object object, IEclipseContext context) throws InjectionException
      Un-injects the context from the object.
      Parameters:
      object - The domain object previously injected with the context
      context - The context previously injected into the object
      Throws:
      InjectionException - if an exception occurred while performing this operation
    • make

      public static <T> T make(Class<T> clazz, IEclipseContext context) throws InjectionException
      Obtain an instance of the specified class and inject it with the context.

      Class'es scope dictates if a new instance of the class will be created, or existing instance will be reused.

      Parameters:
      clazz - The class to be instantiated
      context - The context to obtain injected values from
      Returns:
      an instance of the specified class
      Throws:
      InjectionException - if an exception occurred while performing this operation
      See Also:
      • Scope
      • Singleton
    • make

      public static <T> T make(Class<T> clazz, IEclipseContext context, IEclipseContext staticContext) throws InjectionException
      Obtain an instance of the specified class and inject it with the context. This method allows extra values that don't need to be tracked to be passed to the object using staticContext.

      If values for the same key present in both the context and the static context, the values from the static context are injected.

      Class'es scope dictates if a new instance of the class will be created, or existing instance will be reused.

      Parameters:
      clazz - The class to be instantiated
      context - The context to obtain injected values from
      staticContext - The context containing extra values; not tracked
      Returns:
      an instance of the specified class
      Throws:
      InjectionException - if an exception occurred while performing this operation
      See Also:
    • setDefault

      public static void setDefault(IEclipseContext context)
      Specifies context used by the injector to create its internal objects. Providing this context allows injector to become aware of higher-level constructs, such as application logging and synchronization.
      Parameters:
      context - the context to be used as a data source by the injector
      Since:
      1.2