But in my app, I have an object that I'd like to inject that is
sometimes legitimately null. Currently my best option is to
create a cumbersome NullableReference and inject that
instead:
public interface NullableReference<T> {
T get();
boolean isNull();
}
I propose changing Guice to inject null, but only if the
injection point is annotated as @Nullable.
http://groups.google.com/group/jsr-305/web/proposed-annotations
http://www.jetbrains.com/idea/documentation/howto.html
For example, suppose I have this code:
public class CodeSearch {
@Inject
public CodeSearch(@Nullable SourceFile sourceFile,
SearchProvider searchProvider) {
...
}
}
Then Guice will not explode if the sourceFile instance is null.
It will continue to explode as usual if SearchProvider is null.
Details...
1 - which @Nullable to use? Such annotations are already
provided by IntelliJ and JSR 305. My preference would be to
support any annotation with runtime retention whose simple
name is "Nullable".
2 - impact on injector state. This would legitimize null, so
now scopes would have to cache null just like any other value.
Similarly, bind(Foo.class).toInstance(null) should be allowed.
3 - sometimes there's no injection point. For example, in
a direct call to Injector.getInstance(Class). What happens today?
Making this explode rather than returning a null result is fine.
Cheers,
Jesse
So there are three levels of control right?
1. inject versus no-inject
2. inject null allowed for all dependencies at an inject point
versus inject null not allowed at all
3. inject null allowed for some dependencies and not the others
1 is already supported by guice 1.0; I think guice should at least
support 2 as well.
On Aug 22, 2:56 am, "Robbie Vanbrabant" <robbie.vanbrab...@gmail.com>
wrote:
> Because that's for the whole injection point and not a single dependency.
>
> Robbie
>
> On 8/22/07, Dhanji R. Prasanna <dha...@gmail.com> wrote:
>
>
>
> > Why not @Inject(optional=true) ?
>
> > I dont agree that null is a valid dependency (it is the default value on
> > every reference in java anyway).
>
> > Dhanji.
>
> > On 8/22/07, Robbie Vanbrabant <robbie.vanbrab...@gmail.com> wrote:
>
> > > I agree that this is something that's missing. Take a look at this
> > > existing issue:
> > >http://code.google.com/p/google-guice/issues/detail?id=112
>
> > > Robbie
>