Author: limpbizkit
Date: Mon Sep 29 11:21:20 2008
New Revision: 626

Added:
    trunk/test/com/google/inject/EagerSingletonTest.java
Modified:
    trunk/src/com/google/inject/InjectorBuilder.java
    trunk/test/com/google/inject/BinderTestSuite.java

Log:
Creating a copy of the bindings-to-inject before looping over them. There  
was a problem where JIT-singletons were being added while we were looping,  
and this was causing ConcurrentModificationExceptions.

Modified: trunk/src/com/google/inject/InjectorBuilder.java
==============================================================================
--- trunk/src/com/google/inject/InjectorBuilder.java    (original)
+++ trunk/src/com/google/inject/InjectorBuilder.java    Mon Sep 29 11:21:20  
2008
@@ -32,6 +32,7 @@
  import com.google.inject.spi.InjectionPoint;
  import java.util.List;
  import java.util.Map;
+import java.util.Set;
  import java.util.logging.Logger;

  /**
@@ -183,8 +184,10 @@

    public void loadEagerSingletons() {
      // load eager singletons, or all singletons if we're in  
Stage.PRODUCTION.
-    for (final BindingImpl<?> binding
-        : Iterables.concat(injector.explicitBindings.values(),  
injector.jitBindings.values())) {
+    // Bindings discovered while we're binding these singletons are not be  
eager.
+    Set<BindingImpl<?>> candidateBindings = ImmutableSet.copyOf(
+        Iterables.concat(injector.explicitBindings.values(),  
injector.jitBindings.values()));
+    for (final BindingImpl<?> binding : candidateBindings) {
        if ((stage == Stage.PRODUCTION && binding.getScope() == SINGLETON)
            || binding.getLoadStrategy() == LoadStrategy.EAGER) {
          try {

Modified: trunk/test/com/google/inject/BinderTestSuite.java
==============================================================================
--- trunk/test/com/google/inject/BinderTestSuite.java   (original)
+++ trunk/test/com/google/inject/BinderTestSuite.java   Mon Sep 29 11:21:20  
2008
@@ -36,6 +36,9 @@
  import junit.framework.TestCase;
  import junit.framework.TestSuite;

+/**
+ * @author [EMAIL PROTECTED] (Jesse Wilson)
+ */
  public class BinderTestSuite {

    public static Test suite() {

Added: trunk/test/com/google/inject/EagerSingletonTest.java
==============================================================================
--- (empty file)
+++ trunk/test/com/google/inject/EagerSingletonTest.java        Mon Sep 29  
11:21:20 2008
@@ -0,0 +1,71 @@
+/**
+ * Copyright (C) 2008 Google Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.google.inject;
+
+import junit.framework.TestCase;
+
+/**
+ * @author [EMAIL PROTECTED] (Jesse Wilson)
+ */
+public class EagerSingletonTest extends TestCase {
+
+  public void testJustInTimeEagerSingletons() {
+    A.instanceCount = 0;
+    B.instanceCount = 0;
+    C.instanceCount = 0;
+    Guice.createInjector(Stage.PRODUCTION, new AbstractModule() {
+      protected void configure() {
+        // create a just-in-time binding for A
+        getProvider(A.class);
+
+        // create a just-in-time binding for C
+        requestInjection(new Object() {
+          @Inject void inject(Injector injector) {
+            injector.getInstance(C.class);
+          }
+        });
+      }
+    });
+
+    assertEquals(1, A.instanceCount);
+    assertEquals("Singletons discovered when creating singletons should  
not be built eagerly",
+        0, B.instanceCount);
+    assertEquals(1, C.instanceCount);
+  }
+
+  @Singleton
+  static class A {
+    static int instanceCount = 0;
+    int instanceId = instanceCount++;
+
+    @Inject A(Injector injector) {
+      injector.getProvider(B.class);
+    }
+  }
+
+  @Singleton
+  static class B {
+    static int instanceCount = 0;
+    int instanceId = instanceCount++;
+  }
+
+  @Singleton
+  static class C {
+    static int instanceCount = 0;
+    int instanceId = instanceCount++;
+  }
+}

--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to