Toy example:
public class MyModule extends AbstractModule {
  private static final Foo foo;
  public MyModule(Foo foo) {
    this.foo = foo;
  }
  @Override
  public void configure() {
    bind(Bar.class).toProvider(new Provider<Bar>() {
      @Override public Bar get() {
        return foo.getBar();
      }
    });
  }
}
This lets me lazily invoke the .getBar() method of a user-provided Foo instance stored in a field of MyModule. However now the provider has its own dependencies - hence I need to define a non-anonymous class I specify an @Inject constructor on. Something like:
public class MyModule extends AbstractModule {
  private static final Foo foo;
  public MyModule(Foo foo) {
    this.foo = foo;
  }
  @Override
  public void configure() {
    bind(Bar.class).toProvider(BarProvider.class);
  }
  BarProvider implements Provider<Bar> {
    private Baz baz;
    @Inject BarProvider(Baz baz) {
      this.baz = baz;
    }
    @Override public Bar get() {
      return foo.getBar(baz);
    }
  }
}
Perfect! Except Guice doesn't like this...
Exception in thread "main" com.google.inject.CreationException: Unable to create injector, see the following errors:
1) Injecting into inner classes is not supported. Please use a 'static' class (top-level or nested) instead of com.example.MyModule$BarProvider.
So, I'm in a bind. I need to access both a field on the module and an injected type from a Provider class at the same time. Is there any way to do this?
Note: this toy example excludes some of the actual complexity - in particular the bind() statement is more involved, which is why I can't simply define an @Provides method.