diff options
author | Tavian Barnes <tavianator@tavianator.com> | 2014-10-16 14:42:54 -0400 |
---|---|---|
committer | Tavian Barnes <tavianator@tavianator.com> | 2014-10-16 14:44:56 -0400 |
commit | bf31a32b09efc6d87651c00ed05fe7b523c3e4c7 (patch) | |
tree | b551888b1a5dbcbacefe667968515a3140a7b02a /sangria-contextual | |
parent | 05440aeaafe3214243153a5066fb12f66f895955 (diff) | |
download | sangria-bf31a32b09efc6d87651c00ed05fe7b523c3e4c7.tar.xz |
contextual: Avoid injecting the injector in ProviderKeyAdapter.
Diffstat (limited to 'sangria-contextual')
-rw-r--r-- | sangria-contextual/src/main/java/com/tavianator/sangria/contextual/ContextSensitiveBinder.java | 23 |
1 files changed, 10 insertions, 13 deletions
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<? extends ContextSensitiveProvider<? extends T>> key) { error.cancel(); - binder.bind(bindingKey).toProvider(new ProviderKeyAdapter<>(key, makeLinkedKey(key))); + Provider<? extends ContextSensitiveProvider<? extends T>> provider = binder.getProvider(makeUniqueLinkedKey(key)); + binder.bind(bindingKey).toProvider(new ProviderKeyAdapter<>(provider, key)); binder.bindListener(new BindingMatcher(bindingKey), new Trigger(bindingKey)); } - private <U> Key<U> makeLinkedKey(Key<U> key) { + /** + * For Binder#requireExplicitBindings() support. + */ + private <U> Key<U> makeUniqueLinkedKey(Key<U> key) { Key<U> linkedKey = Key.get(key.getTypeLiteral(), UniqueAnnotations.create()); binder.bind(linkedKey) .to(key); @@ -197,20 +200,14 @@ public class ContextSensitiveBinder { } private static class ProviderKeyAdapter<T> extends ProviderAdapter<T> implements ContextSensitiveProviderKeyBinding<T> { + private final Provider<? extends ContextSensitiveProvider<? extends T>> provider; private final Key<? extends ContextSensitiveProvider<? extends T>> providerKey; - private final Key<? extends ContextSensitiveProvider<? extends T>> linkedKey; - private Provider<? extends ContextSensitiveProvider<? extends T>> provider; ProviderKeyAdapter( - Key<? extends ContextSensitiveProvider<? extends T>> providerKey, - Key<? extends ContextSensitiveProvider<? extends T>> linkedKey) { + Provider<? extends ContextSensitiveProvider<? extends T>> provider, + Key<? extends ContextSensitiveProvider<? extends T>> providerKey) { + this.provider = provider; this.providerKey = providerKey; - this.linkedKey = linkedKey; - } - - @Inject - void inject(Injector injector) { - provider = injector.getProvider(linkedKey); } @Override |