Need some help to understand this error and possible way to solve it.
So I have a Type like this:
class Foo {
static final class Bar {}
@Inject
Foo(TypeA a, @Nullable Set<Bar> b) {}
}
In order to make Foo injectable, I need to fulfill dependency for params a and b, now I have no problem to provide Type a implementation, its not null, so I have a @Provides method in the packages Module class. But what about b? Its a nullable, how to write @Provides with a parameter that can be nullable? My guess is it shouldn't be required and Guice should understand the annotation... But something isn't working right, I got this error:
1) No implementation for java.util.Set<Foo$Bar>
was bound.
And here is the @Provides method (which didn't work atm)
@Provides @Nullable
public Foo provideFoo(TypeA a, @Nullable Set<Foo.Bar> b) {
return new Foo(a, b);
}
I know the nested class is not public nor the constructor, so maybe guice can't access it but even if I make them all public (which I don't really want) the error still remains... Is there something I am m开发者_开发百科issing?
There's a difference between not having any binding for Set<Bar>
and having a binding for Set<Bar>
that is null
.
You'd need an @Provides
method that returns a Set<Bar>
(that may be null
) or something like bind(Bar.class).toProvider(Providers.ofInstance(null))
in order to be able to provide the second parameter to Foo
's constructor.
Also note that your @Provides
method for Foo
is just calling the @Inject
annotated constructor directly, so it's not necessary at all. I'd also say it'd be preferable to bind to an empty Set<Bar>
rather than to null
.
精彩评论