From bf31a32b09efc6d87651c00ed05fe7b523c3e4c7 Mon Sep 17 00:00:00 2001 From: Tavian Barnes Date: Thu, 16 Oct 2014 14:42:54 -0400 Subject: contextual: Avoid injecting the injector in ProviderKeyAdapter. --- .../sangria/contextual/ContextSensitiveBinder.java | 23 ++++++++++------------ 1 file changed, 10 insertions(+), 13 deletions(-) (limited to 'sangria-contextual') diff --git a/sangria-contextual/src/main/java/com/tavianator/sangria/contextual/ContextSensitiveBinder.java b/sangria-contextual/src/main/java/com/tavianator/sangria/contextual/ContextSensitiveBinder.java index fdc1ad0..2cbf9b7 100644 --- a/sangria-contextual/src/main/java/com/tavianator/sangria/contextual/ContextSensitiveBinder.java +++ b/sangria-contextual/src/main/java/com/tavianator/sangria/contextual/ContextSensitiveBinder.java @@ -25,7 +25,6 @@ import com.google.inject.AbstractModule; import com.google.inject.Binder; import com.google.inject.Binding; import com.google.inject.ConfigurationException; -import com.google.inject.Injector; import com.google.inject.Key; import com.google.inject.Provider; import com.google.inject.TypeLiteral; @@ -140,11 +139,15 @@ public class ContextSensitiveBinder { public void toContextSensitiveProvider(Key> key) { error.cancel(); - binder.bind(bindingKey).toProvider(new ProviderKeyAdapter<>(key, makeLinkedKey(key))); + Provider> provider = binder.getProvider(makeUniqueLinkedKey(key)); + binder.bind(bindingKey).toProvider(new ProviderKeyAdapter<>(provider, key)); binder.bindListener(new BindingMatcher(bindingKey), new Trigger(bindingKey)); } - private Key makeLinkedKey(Key key) { + /** + * For Binder#requireExplicitBindings() support. + */ + private Key makeUniqueLinkedKey(Key key) { Key linkedKey = Key.get(key.getTypeLiteral(), UniqueAnnotations.create()); binder.bind(linkedKey) .to(key); @@ -197,20 +200,14 @@ public class ContextSensitiveBinder { } private static class ProviderKeyAdapter extends ProviderAdapter implements ContextSensitiveProviderKeyBinding { + private final Provider> provider; private final Key> providerKey; - private final Key> linkedKey; - private Provider> provider; ProviderKeyAdapter( - Key> providerKey, - Key> linkedKey) { + Provider> provider, + Key> providerKey) { + this.provider = provider; this.providerKey = providerKey; - this.linkedKey = linkedKey; - } - - @Inject - void inject(Injector injector) { - provider = injector.getProvider(linkedKey); } @Override -- cgit v1.2.3