Skip to content

Instantly share code, notes, and snippets.

@gissuebot
Created July 7, 2014 17:53
Show Gist options
  • Save gissuebot/9fa78e71f6b098ecc1e4 to your computer and use it in GitHub Desktop.
Save gissuebot/9fa78e71f6b098ecc1e4 to your computer and use it in GitHub Desktop.
Migrated attachment for Guice issue 38, comment 21
### Eclipse Workspace Patch 1.0
#P guice2
Index: src/com/google/inject/InjectorImpl.java
===================================================================
--- src/com/google/inject/InjectorImpl.java (revision 1001)
+++ src/com/google/inject/InjectorImpl.java (working copy)
@@ -23,6 +23,7 @@
import com.google.inject.internal.Errors;
import com.google.inject.internal.ErrorsException;
import com.google.inject.internal.ImmutableList;
+import com.google.inject.internal.ImmutableMap;
import com.google.inject.internal.ImmutableSet;
import com.google.inject.internal.InstanceBindingImpl;
import com.google.inject.internal.InternalContext;
@@ -658,6 +659,12 @@
public Map<Key<?>, Binding<?>> getBindings() {
return state.getExplicitBindingsThisLevel();
}
+
+ public Map<Key<?>, Binding<?>> getJustInTimeBindings() {
+ synchronized(state.lock()) {
+ return new ImmutableMap.Builder<Key<?>, Binding<?>>().putAll(jitBindings).build();
+ }
+ }
private static class BindingsMultimap {
final Map<TypeLiteral<?>, List<Binding<?>>> multimap = Maps.newHashMap();
Index: src/com/google/inject/InjectorBuilder.java
===================================================================
--- src/com/google/inject/InjectorBuilder.java (revision 1001)
+++ src/com/google/inject/InjectorBuilder.java (working copy)
@@ -245,6 +245,9 @@
public Map<Key<?>, Binding<?>> getBindings() {
return this.delegateInjector.getBindings();
}
+ public Map<Key<?>, Binding<?>> getJustInTimeBindings() {
+ return this.delegateInjector.getJustInTimeBindings();
+ }
public <T> Binding<T> getBinding(Key<T> key) {
return this.delegateInjector.getBinding(key);
}
Index: test/com/google/inject/spi/SpiBindingsTest.java
===================================================================
--- test/com/google/inject/spi/SpiBindingsTest.java (revision 1001)
+++ test/com/google/inject/spi/SpiBindingsTest.java (working copy)
@@ -33,6 +33,7 @@
import com.google.inject.internal.Lists;
import com.google.inject.name.Names;
import java.lang.reflect.Constructor;
+import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
@@ -380,4 +381,71 @@
private static class D extends C {
@Inject public D(Injector unused) { }
}
+
+ private static interface JIT_A {}
+ private static class JIT_B implements JIT_A {}
+ private static class JIT_C {}
+ @Singleton private static class JIT_D {}
+
+ public void testGetJitBindings() {
+ Injector injector = Guice.createInjector(new AbstractModule() {
+ protected void configure() {
+ bind(JIT_A.class).to(JIT_B.class);
+ }
+ });
+
+ // assert implicit constructor binding is created when doing bind(..).to(..)
+ Collection<Binding<?>> jitBindings1 = injector.getJustInTimeBindings().values();
+ assertEquals(1, jitBindings1.size());
+ jitBindings1.iterator().next().acceptVisitor(new FailingElementVisitor() {
+ @Override public <T> Void visit(Binding<T> binding) {
+ assertEquals(Key.get(JIT_B.class), binding.getKey());
+ binding.acceptScopingVisitor(new FailingBindingScopingVisitor() {
+ @Override public Void visitNoScoping() {
+ return null;
+ }
+ });
+ return null;
+ }
+ });
+
+ // assert implicit binding is created when retrieving new provider,
+ // and that prior retrieved bindings do not modify.
+ injector.getProvider(JIT_C.class);
+ List<Binding<?>> jitBindings2 = Lists.newArrayList(injector.getJustInTimeBindings().values());
+ assertEquals(1, jitBindings1.size()); // assert original list didn't change.
+ assertEquals(2, jitBindings2.size()); // new list contains it.
+ jitBindings2.removeAll(jitBindings1);
+ assertEquals(1, jitBindings1.size()); // after removing JIT_B, only 1 left.
+ jitBindings2.get(0).acceptVisitor(new FailingElementVisitor() {
+ @Override public <T> Void visit(Binding<T> binding) {
+ assertEquals(Key.get(JIT_C.class), binding.getKey());
+ binding.acceptScopingVisitor(new FailingBindingScopingVisitor() {
+ @Override public Void visitNoScoping() {
+ return null;
+ }
+ });
+ return null;
+ }
+ });
+
+ // assert scope is set properly on JIT bindings.
+ injector.getProvider(JIT_D.class);
+ List<Binding<?>> jitBindings3 = Lists.newArrayList(injector.getJustInTimeBindings().values());
+ jitBindings3.removeAll(jitBindings2);
+ jitBindings3.removeAll(jitBindings1);
+ assertEquals(1, jitBindings3.size());
+ jitBindings3.get(0).acceptVisitor(new FailingElementVisitor() {
+ @Override public <T> Void visit(Binding<T> binding) {
+ assertEquals(Key.get(JIT_D.class), binding.getKey());
+ binding.acceptScopingVisitor(new FailingBindingScopingVisitor() {
+ @Override public Void visitScope(Scope scope) {
+ assertSame(Scopes.SINGLETON, scope);
+ return null;
+ }
+ });
+ return null;
+ }
+ });
+ }
}
Index: test/com/google/inject/InjectorTest.java
===================================================================
--- test/com/google/inject/InjectorTest.java (revision 1001)
+++ test/com/google/inject/InjectorTest.java (working copy)
@@ -27,6 +27,9 @@
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.atomic.AtomicReference;
+
+import com.google.inject.internal.Iterables;
+
import junit.framework.TestCase;
/**
@@ -122,9 +125,15 @@
assertNotSerializable(injector);
assertNotSerializable(injector.getProvider(String.class));
assertNotSerializable(injector.getBinding(String.class));
- for (Binding<?> binding : injector.getBindings().values()) {
+ assertTrue(injector.getBindings().size() > 0);
+ assertTrue(injector.getJustInTimeBindings().size() > 0);
+ for (Binding<?> binding :
+ Iterables.concat(injector.getBindings().values(),
+ injector.getJustInTimeBindings().values())) {
assertNotSerializable(binding);
}
+
+
}
static class IntegerWrapper {
Index: src/com/google/inject/Injector.java
===================================================================
--- src/com/google/inject/Injector.java (revision 1001)
+++ src/com/google/inject/Injector.java (working copy)
@@ -100,6 +100,18 @@
* <p>This method is part of the Guice SPI and is intended for use by tools and extensions.
*/
Map<Key<?>, Binding<?>> getBindings();
+
+ /**
+ * Returns all just-in-time bindings. Just-in-time bindings are also referred to as
+ * implicit or synthetic bindings. These are bindings that the user did not bind explicitly,
+ * but Guice implicitly creates in order to satisfy an injection.
+ *
+ * <p>The returned map does not include bindings inherited from a {@link #getParent() parent
+ * injector}, should one exist.
+ *
+ * <p>This method is part of the Guice SPI and is intended for use by tools and extensions.
+ */
+ Map<Key<?>, Binding<?>> getJustInTimeBindings();
/**
* Returns the binding for the given injection key. This will be an explicit bindings if the key
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment