Class InjectionLayerProvider

java.lang.Object
eu.cloudnetservice.driver.inject.InjectionLayerProvider

@Internal final class InjectionLayerProvider extends Object
Internal utility to provide injection layers.
Since:
4.0
  • Field Details

  • Constructor Details

    • InjectionLayerProvider

      private InjectionLayerProvider()
  • Method Details

    • boot

      @NonNull public static @NonNull InjectionLayer<Injector> boot()
      Get the singleton boot injection layer.
      Returns:
      the boot injection layer.
    • ext

      @NonNull public static @NonNull InjectionLayer<Injector> ext()
      Returns the singleton injection layer which should be used for all kinds of external component injection (like plugins). The ext layer has all bindings of the boot layer present.
      Returns:
      the singleton ext injection layer.
    • specifiedChild

      Constructs a new child layer for the given parent layer. All bindings from the parent layer are still visible to the child layer, but not vise-versa. The returned child layer uses a specified injector and passes it to the given configurator in order to precisely configure bindings.

      The new layer has a binding for the layer type with the given name in order to request injection of the given layer. Note that injecting the injector of a layer still results in the child layer injector, even if no name is present.

      This type of layer should be used in a context when the constructed bindings of classes should be visible to the parent layer, while some bindings need to be specifically overridden. This can for example be a modular context, where the module description should only be visible to the current module, not to all modules.

      Parameters:
      parent - the parent layer to construct the child from.
      name - the name of the child layer.
      configurator - the configurator for the layer, for convince it also provides the specified injector.
      Returns:
      a new specified layer with the given parent layer.
      Throws:
      NullPointerException - if the given parent layer, name or configurator is null.
      IllegalArgumentException - if the given name is invalid.
    • child

      Constructs a new child layer for the given parent layer. All bindings from the parent layer are still visible to the child layer, but not vise-versa.

      The new layer has a binding for the layer type with the given name in order to request injection of the given layer. Note that injecting the injector of a layer still results in the child layer injector, even if no name is present.

      Parameters:
      parent - the parent layer to construct the child from.
      name - the name of the child layer.
      Returns:
      a new layer with the given parent layer.
      Throws:
      NullPointerException - if the given parent layer or name is null.
      IllegalArgumentException - if the given name is invalid.
    • fresh

      Provides a fresh injection layer, with no bindings configured to the returned injector, except for a binding to the layer itself with the provided name.
      Parameters:
      name - the name of the new injection layer.
      Returns:
      a new injection layer with no configured bindings.
      Throws:
      NullPointerException - if the given name is null.
      IllegalArgumentException - if the given name is invalid.
    • configuredLayer

      Provides a fresh injection layer, with no bindings configured to the returned injector. After construction the layer is passed to the given consumer to configure it.
      Parameters:
      name - the name of the injection layer.
      configurator - the configurator for the injection layer.
      Returns:
      a new, configured injection layer with no configured bindings.
      Throws:
      NullPointerException - if the given name or configurator is null.
      IllegalArgumentException - if the given name is invalid.
    • configuredLayer

      @NonNull private static <I extends Injector> @NonNull InjectionLayer<I> configuredLayer(@NonNull @NonNull String name, @NonNull I injector, @NonNull @NonNull Consumer<InjectionLayer<I>> configurator)
      Provides a fresh injection layer, with no bindings configured to the returned injector. After construction the layer is passed to the given consumer to configure it.
      Type Parameters:
      I - the type of injector to use for the layer.
      Parameters:
      name - the name of the injection layer.
      injector - the injector to use for the layer.
      configurator - the configurator for the injection layer.
      Returns:
      a new, configured injection layer with no configured bindings.
      Throws:
      NullPointerException - if the given name, injector or configurator is null.
      IllegalArgumentException - if the given name is invalid.
    • findLayerOf

      @NonNull public static @NonNull InjectionLayer<?> findLayerOf(@NonNull @NonNull Object object, @NonNull @NonNull InjectionLayer<?> def)
      Finds the injection layer associated with the given object or returns the given default injection layer. The following search rules apply (in order):
      1. If the given object is an InjectionLayerHolder, the layer stored in the holder is returned.
      2. If the given object has a layer associated in the layer registry, that layer is returned.
      3. If the given object is a class the associated class loader of the given class is checked.
      4. If the given object is not a class loader the loader of the object class is checked.
      5. If none of the above rules matches the given default layer is returned.
      Parameters:
      object - the object to get the associated injection layer of.
      def - the default layer to return if no layer can be found.
      Returns:
      the injection layer associated with the given object or the given default layer.
      Throws:
      NullPointerException - if the given object or default layer is null.
    • validateName

      private static void validateName(@NonNull @NonNull String name)
      Validates the given layer name, throwing an exception if the name is invalid.
      Parameters:
      name - the name to validate.
      Throws:
      NullPointerException - if the given name is null.
      IllegalArgumentException - if the given name is invalid.
    • registerBindings

      private static void registerBindings(@NonNull @NonNull Injector injector, @NonNull @NonNull jakarta.inject.Provider<InjectionLayer<?>> layer, @NonNull @NonNull String name, @NonNull @NonNull Consumer<UninstalledBinding<?>> register)