public class DynamicItem<T> extends Object
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.
Modifier and Type | Method and Description |
---|---|
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.
|
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.
|
T |
get()
Get the configured item, or null.
|
Extension<T> |
getEntry() |
String |
getPluginName()
Get the name of the plugin that has bound the configured item, or null.
|
static <T> void |
itemOf(com.google.inject.Binder binder,
Class<T> member)
Declare a singleton
DynamicItem<T> with a binder. |
static <T> void |
itemOf(com.google.inject.Binder binder,
com.google.inject.TypeLiteral<T> member)
Declare a singleton
DynamicItem<T> with a binder. |
static <T> DynamicItem<T> |
itemOf(Class<T> member,
T item)
Construct a single
DynamicItem<T> with a fixed value. |
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.
|
RegistrationHandle |
set(com.google.inject.Provider<T> impl,
String pluginName)
Set the element to provide.
|
RegistrationHandle |
set(T item,
String pluginName)
Set the element to provide.
|
public static <T> void itemOf(com.google.inject.Binder binder, Class<T> member)
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);
binder
- a new binder created in the module.member
- type of entry to store.public static <T> void itemOf(com.google.inject.Binder binder, com.google.inject.TypeLiteral<T> member)
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>>() {});
binder
- a new binder created in the module.member
- type of entry to store.public static <T> DynamicItem<T> itemOf(Class<T> member, T item)
DynamicItem<T>
with a fixed value.
Primarily useful for passing DynamicItem
s to constructors in tests.
member
- type of item.item
- item to store.public static <T> com.google.inject.binder.LinkedBindingBuilder<T> bind(com.google.inject.Binder binder, Class<T> type)
binder
- a new binder created in the module.type
- type of entry to store.public static <T> com.google.inject.binder.LinkedBindingBuilder<T> bind(com.google.inject.Binder binder, com.google.inject.TypeLiteral<T> type)
binder
- a new binder created in the module.type
- type of entry to store.public T get()
public String getPluginName()
public RegistrationHandle set(T item, String pluginName)
item
- the item to use. Must not be null.pluginName
- the name of the plugin providing the item.public RegistrationHandle set(com.google.inject.Provider<T> impl, String pluginName)
impl
- the item to add to the collection. Must not be null.pluginName
- name of the source providing the implementation.public ReloadableRegistrationHandle<T> set(com.google.inject.Key<T> key, com.google.inject.Provider<T> impl, String pluginName)
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.