Revision: 2cc8ce904aff
Author:   Christian Edward Gruber <[email protected]>
Date:     Thu May 16 10:53:11 2013
Log: Clear context during provision. Fixes http://code.google.com/p/google-guice/issues/detail?id=743

--------------
Manually Synced.
COMMIT=45102829

http://code.google.com/p/google-guice/source/detail?r=2cc8ce904aff

Modified:
 /core/src/com/google/inject/internal/ConstructorInjector.java
 /core/src/com/google/inject/internal/ProviderInternalFactory.java
 /core/test/com/google/inject/ProvisionListenerTest.java

=======================================
--- /core/src/com/google/inject/internal/ConstructorInjector.java Sun Feb 26 18:23:19 2012 +++ /core/src/com/google/inject/internal/ConstructorInjector.java Thu May 16 10:53:11 2013
@@ -94,7 +94,6 @@
         });
       }
     } finally {
-      constructionContext.removeCurrentReference();
       constructionContext.finishConstruction();
     }
   }
@@ -125,6 +124,8 @@
           : userException;
       throw errors.withSource(constructionProxy.getInjectionPoint())
           .errorInjectingConstructor(cause).toException();
+    } finally {
+      constructionContext.removeCurrentReference();
     }
   }
 }
=======================================
--- /core/src/com/google/inject/internal/ProviderInternalFactory.java Sun Feb 26 18:23:19 2012 +++ /core/src/com/google/inject/internal/ProviderInternalFactory.java Thu May 16 10:53:11 2013
@@ -46,7 +46,7 @@
       throws ErrorsException {
Class<?> expectedType = dependency.getKey().getTypeLiteral().getRawType(); final ConstructionContext<T> constructionContext = context.getConstructionContext(this);
-
+
     // We have a circular reference between constructors. Return a proxy.
     if (constructionContext.isConstructing()) {
       if (!allowProxy) {
=======================================
--- /core/test/com/google/inject/ProvisionListenerTest.java Wed May 15 18:39:15 2013 +++ /core/test/com/google/inject/ProvisionListenerTest.java Thu May 16 10:53:11 2013
@@ -37,6 +37,7 @@
 import java.util.List;
 import java.util.Set;
 import java.util.concurrent.atomic.AtomicBoolean;
+import java.util.concurrent.atomic.AtomicInteger;

 /**
  * Tests for {@link Binder#bindListener(Matcher, ProvisionListener...)}
@@ -636,4 +637,61 @@
ce.getMessage(), "Binding to core guice framework type is not allowed: Injector.");
     }
   }
+
+  public void testProvisionIsNotifiedAfterContextsClear() {
+    Injector injector = Guice.createInjector(new AbstractModule() {
+      @Override
+      protected void configure() {
+        bindListener(Matchers.any(), new ProvisionListener() {
+          @Override
+          public <T> void onProvision(ProvisionInvocation<T> provision) {
+            Object provisioned = provision.provision();
+            if (provisioned instanceof X) {
+              ((X)provisioned).init();
+            } else if (provisioned instanceof Y) {
+              X.createY = false;
+              ((Y)provisioned).init();
+            }
+          }
+        });
+      }
+    });
+
+    X.createY = true;
+    X x = injector.getInstance(X.class);
+    assertNotSame(x, x.y.x);
+ assertFalse("x.ID: " + x.ID + ", x.y.x.iD: " + x.y.x.ID, x.ID == x.y.x.ID);
+  }
+
+  private static class X {
+    final static AtomicInteger COUNTER = new AtomicInteger();
+    static boolean createY;
+
+    final int ID = COUNTER.getAndIncrement();
+    final Provider<Y> yProvider;
+    Y y;
+
+    @Inject X(Provider<Y> yProvider) {
+      this.yProvider = yProvider;
+    }
+
+    void init() {
+      if (createY) {
+        this.y = yProvider.get();
+      }
+    }
+  }
+
+  private static class Y {
+    final Provider<X> xProvider;
+    X x;
+
+    @Inject Y(Provider<X> xProvider) {
+      this.xProvider = xProvider;
+    }
+
+    void init() {
+      this.x = xProvider.get();
+    }
+  }
 }

--
You received this message because you are subscribed to the Google Groups 
"google-guice-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/google-guice-dev?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to