Store Provider<T> in DynamicSet, DynamicMap

Instead of storing the object instance T, store the Provider<T>.

This enables Guice to provide singletons wrapped in a Provider, or to
dynamically create the object on each use. Dynamic creation enables
plugins to make a new listener instance for each event that is fired,
permitting the listener to then use instance member fields during the
event. How this works is up to the plugin, but binding with @Singleton
will still be the recommended method for registering listeners.

Change-Id: I83b22f3ac2214f45c3c298937146843a806bea2f
This commit is contained in:
Shawn O. Pearce
2012-05-16 09:22:44 -07:00
parent 9637774986
commit dc5ef8f266
7 changed files with 76 additions and 40 deletions

View File

@@ -197,7 +197,7 @@ public class PluginGuiceEnvironment {
DynamicSet<Object> set = (DynamicSet<Object>) e.getValue();
for (Binding<Object> b : bindings(src, type)) {
plugin.add(set.add(b.getKey(), b.getProvider().get()));
plugin.add(set.add(b.getKey(), b.getProvider()));
}
}
}
@@ -219,7 +219,7 @@ public class PluginGuiceEnvironment {
plugin.add(set.put(
plugin.getName(),
b.getKey(),
b.getProvider().get()));
b.getProvider()));
}
}
}
@@ -288,7 +288,7 @@ public class PluginGuiceEnvironment {
newPlugin.add(map.put(
newPlugin.getName(),
b.getKey(),
b.getProvider().get()));
b.getProvider()));
}
}
}
@@ -355,7 +355,7 @@ public class PluginGuiceEnvironment {
oi.remove();
replace(newPlugin, h2, b);
} else {
newPlugin.add(set.add(b.getKey(), b.getProvider().get()));
newPlugin.add(set.add(b.getKey(), b.getProvider()));
}
}
}
@@ -363,7 +363,7 @@ public class PluginGuiceEnvironment {
private static <T> void replace(Plugin newPlugin,
ReloadableRegistrationHandle<T> h, Binding<T> b) {
RegistrationHandle n = h.replace(b.getKey(), b.getProvider().get());
RegistrationHandle n = h.replace(b.getKey(), b.getProvider());
if (n != null){
newPlugin.add(n);
}