From b8c1ca531321dbcee3ae237bad20fa8691b31f83 Mon Sep 17 00:00:00 2001 From: Tavian Barnes Date: Sat, 4 Oct 2014 12:24:51 -0400 Subject: contextual: Make ContextSensitiveBinder work with requireExplicitBindings(). Same as regular toProvider() bindings, an explicit binding for the provider is *not* required, unless to provider itself is injected somewhere. This is accomplished by generating a unique linked-key binding for each ContextSensitiveProvider. --- .../sangria/contextual/ContextSensitiveBinder.java | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) (limited to 'sangria-contextual/src/main/java/com/tavianator/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 6d5e502..fdc1ad0 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 @@ -40,6 +40,7 @@ import com.google.inject.spi.ProviderWithExtensionVisitor; import com.google.inject.spi.ProvisionListener; import com.tavianator.sangria.core.DelayedError; +import com.tavianator.sangria.core.UniqueAnnotations; /** * A binder for {@link ContextSensitiveProvider}s. @@ -139,10 +140,17 @@ public class ContextSensitiveBinder { public void toContextSensitiveProvider(Key> key) { error.cancel(); - binder.bind(bindingKey).toProvider(new ProviderKeyAdapter<>(key)); + binder.bind(bindingKey).toProvider(new ProviderKeyAdapter<>(key, makeLinkedKey(key))); binder.bindListener(new BindingMatcher(bindingKey), new Trigger(bindingKey)); } + private Key makeLinkedKey(Key key) { + Key linkedKey = Key.get(key.getTypeLiteral(), UniqueAnnotations.create()); + binder.bind(linkedKey) + .to(key); + return linkedKey; + } + @Override public void toContextSensitiveProvider(ContextSensitiveProvider provider) { error.cancel(); @@ -190,15 +198,19 @@ public class ContextSensitiveBinder { private static class ProviderKeyAdapter extends ProviderAdapter implements ContextSensitiveProviderKeyBinding { private final Key> providerKey; + private final Key> linkedKey; private Provider> provider; - ProviderKeyAdapter(Key> providerKey) { + ProviderKeyAdapter( + Key> providerKey, + Key> linkedKey) { this.providerKey = providerKey; + this.linkedKey = linkedKey; } @Inject void inject(Injector injector) { - provider = injector.getProvider(providerKey); + provider = injector.getProvider(linkedKey); } @Override -- cgit v1.2.3