google / google/guice

Binding SPI is a little strange when it comes to Provider bindings

Open
#870 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
Java
Stars
12.7k
Forks
1.7k
Avg merge
11m
Merged PRs (30d)
2

Description

```
@Test public void testProviderSpi() {
Module providesModule = new AbstractModule() {
@Override protected void configure() {
bind(Key.get(Object.class)).toInstance(new Object());
}
};

Injector rootInjector = Guice.createInjector(providesModule);
Set> keys = rootInjector.getAllBindings().keySet();
rootInjector.getExistingBinding(new Key>() {});
Set> keys2 = rootInjector.getAllBindings().keySet();
assertThat(Sets.difference(keys2, keys)).isEmpty();
}
```

This test fails with:
java.lang.AssertionError: Not true that <[Key[type=com.google.inject.Provider, annotation=[none]]]> is empty

This is because of special whitelisting that getExistingBinding does when it comes to allocating provider bindings.

> There's some weirdness internally in that Guice is perfectly happy caching bindings for Provider> if someone happens to call getInstance(Key>) too. Seems like it'd be better to handle Providers by using Binding.getProvider(). But those structures generally predate my involvement, and I've never had time or a reason to dig through it and/or clean things up.
> @sameb

So maybe there is some potential for improvement on the representation of provider bindings.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.