Class DynamicItem<T>


  • public class DynamicItem<T>
    extends Object
    A single item that can be modified as plugins reload.

    DynamicItems are always mapped as singletons in Guice. Items store a Provider internally, and resolve the provider to an instance on demand. This enables registrations to decide between singleton and non-singleton members. If multiple plugins try to provide the same Provider, an exception is thrown.

    • Method Detail

      • itemOf

        public static <T> void itemOf​(com.google.inject.Binder binder,
                                      Class<T> member)
        Declare a singleton DynamicItem<T> with a binder.

        Items must be defined in a Guice module before they can be bound:

           DynamicItem.itemOf(binder(), Interface.class);
           DynamicItem.bind(binder(), Interface.class).to(Impl.class);
         
        Parameters:
        binder - a new binder created in the module.
        member - type of entry to store.
      • itemOf

        public static <T> void itemOf​(com.google.inject.Binder binder,
                                      com.google.inject.TypeLiteral<T> member)
        Declare a singleton DynamicItem<T> with a binder.

        Items must be defined in a Guice module before they can be bound:

           DynamicSet.itemOf(binder(), new TypeLiteral<Thing<Foo>>() {});
         
        Parameters:
        binder - a new binder created in the module.
        member - type of entry to store.
      • itemOf

        public static <T> DynamicItem<T> itemOf​(Class<T> member,
                                                T item)
        Construct a single DynamicItem<T> with a fixed value.

        Primarily useful for passing DynamicItems to constructors in tests.

        Parameters:
        member - type of item.
        item - item to store.
      • bind

        public static <T> com.google.inject.binder.LinkedBindingBuilder<T> bind​(com.google.inject.Binder binder,
                                                                                Class<T> type)
        Bind one implementation as the item using a unique annotation.
        Parameters:
        binder - a new binder created in the module.
        type - type of entry to store.
        Returns:
        a binder to continue configuring the new item.
      • bind

        public static <T> com.google.inject.binder.LinkedBindingBuilder<T> bind​(com.google.inject.Binder binder,
                                                                                com.google.inject.TypeLiteral<T> type)
        Bind one implementation as the item.
        Parameters:
        binder - a new binder created in the module.
        type - type of entry to store.
        Returns:
        a binder to continue configuring the new item.
      • get

        public T get()
        Get the configured item, or null.
        Returns:
        the configured item instance; null if no implementation has been bound to the item. This is common if no plugin registered an implementation for the type.
      • getPluginName

        public String getPluginName()
        Get the name of the plugin that has bound the configured item, or null.
        Returns:
        the name of the plugin that has bound the configured item; null if no implementation has been bound to the item. This is common if no plugin registered an implementation for the type.
      • set

        public RegistrationHandle set​(T item,
                                      String pluginName)
        Set the element to provide.
        Parameters:
        item - the item to use. Must not be null.
        pluginName - the name of the plugin providing the item.
        Returns:
        handle to remove the item at a later point in time.
      • set

        public RegistrationHandle set​(com.google.inject.Provider<T> impl,
                                      String pluginName)
        Set the element to provide.
        Parameters:
        impl - the item to add to the collection. Must not be null.
        pluginName - name of the source providing the implementation.
        Returns:
        handle to remove the item at a later point in time.
      • set

        public ReloadableRegistrationHandle<T> set​(com.google.inject.Key<T> key,
                                                   com.google.inject.Provider<T> impl,
                                                   String pluginName)
        Set the element that may be hot-replaceable in the future.
        Parameters:
        key - unique description from the item's Guice binding. This can be later obtained from the registration handle to facilitate matching with the new equivalent instance during a hot reload.
        impl - the item to set as our value right now. Must not be null.
        pluginName - the name of the plugin providing the item.
        Returns:
        a handle that can remove this item later, or hot-swap the item.