Author: limpbizkit
Date: Thu Jun 4 12:33:42 2009
New Revision: 996
Modified:
trunk/src/com/google/inject/InjectorBuilder.java
trunk/test/com/google/inject/EagerSingletonTest.java
Log:
Band-aid for issue 373. I'm not particularly thrilled about needing to
explicitly check for linked bindings, but it's the least invasive code I
could come up with.
The real underlying problem is that we don't eagerly create instances at
any time other than injector creation.
http://groups.google.com/group/google-guice-dev/browse_thread/thread/62357fb484298855
Modified: trunk/src/com/google/inject/InjectorBuilder.java
==============================================================================
--- trunk/src/com/google/inject/InjectorBuilder.java (original)
+++ trunk/src/com/google/inject/InjectorBuilder.java Thu Jun 4 12:33:42
2009
@@ -22,6 +22,7 @@
import com.google.inject.internal.ImmutableSet;
import com.google.inject.internal.InternalContext;
import com.google.inject.internal.Iterables;
+import com.google.inject.internal.LinkedBindingImpl;
import com.google.inject.internal.Stopwatch;
import com.google.inject.spi.Dependency;
import java.util.Collection;
@@ -189,7 +190,7 @@
(Collection)
injector.state.getExplicitBindingsThisLevel().values(),
injector.jitBindings.values()));
for (final BindingImpl<?> binding : candidateBindings) {
- if (binding.getScoping().isEagerSingleton(stage)) {
+ if (isEagerSingleton(injector, binding, stage)) {
try {
injector.callInContext(new ContextualCallable<Void>() {
Dependency<?> dependency = Dependency.get(binding.getKey());
@@ -212,6 +213,22 @@
}
}
}
+ }
+
+ private boolean isEagerSingleton(InjectorImpl injector, BindingImpl<?>
binding, Stage stage) {
+ if (binding.getScoping().isEagerSingleton(stage)) {
+ return true;
+ }
+
+ // handle a corner case where a child injector links to a binding in a
parent injector, and
+ // that binding is singleton. We won't catch this otherwise because we
only iterate the child's
+ // bindings.
+ if (binding instanceof LinkedBindingImpl) {
+ Key<?> linkedBinding = ((LinkedBindingImpl<?>)
binding).getLinkedKey();
+ return isEagerSingleton(injector,
injector.getBinding(linkedBinding), stage);
+ }
+
+ return false;
}
/** {...@link Injector} exposed to users in {...@link Stage#TOOL}. */
Modified: trunk/test/com/google/inject/EagerSingletonTest.java
==============================================================================
--- trunk/test/com/google/inject/EagerSingletonTest.java (original)
+++ trunk/test/com/google/inject/EagerSingletonTest.java Thu Jun 4
12:33:42 2009
@@ -23,10 +23,13 @@
*/
public class EagerSingletonTest extends TestCase {
- public void testJustInTimeEagerSingletons() {
+ @Override public void setUp() {
A.instanceCount = 0;
B.instanceCount = 0;
C.instanceCount = 0;
+ }
+
+ public void testJustInTimeEagerSingletons() {
Guice.createInjector(Stage.PRODUCTION, new AbstractModule() {
protected void configure() {
// create a just-in-time binding for A
@@ -47,6 +50,23 @@
assertEquals(1, C.instanceCount);
}
+ public void testJustInTimeSingletonsAreNotEager() {
+ Injector injector = Guice.createInjector(Stage.PRODUCTION);
+ injector.getProvider(B.class);
+ assertEquals(0, B.instanceCount);
+ }
+
+ public void testChildEagerSingletons() {
+ Injector parent = Guice.createInjector(Stage.PRODUCTION);
+ parent.createChildInjector(new AbstractModule() {
+ @Override protected void configure() {
+ bind(D.class).to(C.class);
+ }
+ });
+
+ assertEquals(1, C.instanceCount);
+ }
+
@Singleton
static class A {
static int instanceCount = 0;
@@ -64,8 +84,10 @@
}
@Singleton
- static class C {
+ static class C implements D {
static int instanceCount = 0;
int instanceId = instanceCount++;
}
+
+ private static interface D {}
}
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"google-guice-dev" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/google-guice-dev?hl=en
-~----------~----~----~----~------~----~------~--~---