Adding a binding to the set of intercepted keys. This is so that InjectionController can fail on a mismatched substitute.

git-svn-id: https://google-guice.googlecode.com/svn/trunk@418 d779f126-a31b-0410-b53b-1d3aecad763e
diff --git a/extensions/commands/src/com/google/inject/commands/intercepting/InterceptingInjectorBuilder.java b/extensions/commands/src/com/google/inject/commands/intercepting/InterceptingInjectorBuilder.java
index 0214355..b62b6da 100644
--- a/extensions/commands/src/com/google/inject/commands/intercepting/InterceptingInjectorBuilder.java
+++ b/extensions/commands/src/com/google/inject/commands/intercepting/InterceptingInjectorBuilder.java
@@ -17,6 +17,7 @@
 package com.google.inject.commands.intercepting;
 
 import com.google.inject.*;
+import com.google.inject.name.Names;
 import com.google.inject.binder.LinkedBindingBuilder;
 import com.google.inject.binder.ScopedBindingBuilder;
 import static com.google.inject.internal.Objects.nonNull;
@@ -30,6 +31,9 @@
 /**
  * Builds an {@link Injector} that intercepts provision.
  *
+ * <p>The injector contains an extra binding for {@code Set<Key>} annotated
+ * with the name "Interceptable". This bound set contains all intercepted keys.
+ *
  * <h3>Limitations of the current implementation</h3>
  *
  * <p>All intercepted bindings must have binding targets - for example, a type
@@ -60,6 +64,17 @@
   private final Set<Key<?>> keysToIntercept = new HashSet<Key<?>>();
   private boolean tolerateUnmatchedInterceptions = false;
 
+  public InterceptingInjectorBuilder() {
+    // bind the keys to intercept
+    modules.add(new AbstractModule() {
+      protected void configure() {
+        bind(new TypeLiteral<Set<Key>>() {})
+            .annotatedWith(Names.named("Interceptable"))
+            .toInstance(Collections.<Key>unmodifiableSet(keysToIntercept));
+      }
+    });
+  }
+
   public InterceptingInjectorBuilder install(Module... modules) {
     this.modules.addAll(Arrays.asList(modules));
     return this;
diff --git a/extensions/commands/test/com/google/inject/commands/intercepting/InterceptingInjectorBuilderTest.java b/extensions/commands/test/com/google/inject/commands/intercepting/InterceptingInjectorBuilderTest.java
index 367e77d..4650227 100644
--- a/extensions/commands/test/com/google/inject/commands/intercepting/InterceptingInjectorBuilderTest.java
+++ b/extensions/commands/test/com/google/inject/commands/intercepting/InterceptingInjectorBuilderTest.java
@@ -17,13 +17,11 @@
 package com.google.inject.commands.intercepting;
 
 import com.google.inject.*;
+import com.google.inject.name.Names;
 import junit.framework.AssertionFailedError;
 import junit.framework.TestCase;
 
-import java.util.Collection;
-import java.util.ArrayList;
-import java.util.List;
-import java.util.LinkedList;
+import java.util.*;
 
 
 /**
@@ -188,4 +186,25 @@
     assertEquals(new ArrayList(), injector.getInstance(ArrayList.class));
   }
 
+  public void testBindingForSetOfInterceptedKeys() {
+    Module module = new AbstractModule() {
+      protected void configure() {
+        bind(ProvisionInterceptor.class).toInstance(failingInterceptor);
+        bind(List.class).to(LinkedList.class);
+        bind(Map.class).to(HashMap.class);
+        bind(Collection.class).to(ArrayList.class);
+      }
+    };
+
+    Injector injector = new InterceptingInjectorBuilder()
+        .intercept(List.class)
+        .intercept(Collection.class)
+        .install(module)
+        .build();
+
+    Set<Key> interceptableKeys = injector.getInstance(
+        Key.get(new TypeLiteral<Set<Key>>() {}, Names.named("Interceptable")));
+    assertEquals(new HashSet<Key>(Arrays.asList(Key.get(List.class), Key.get(Collection.class))),
+        interceptableKeys);
+  }
 }