@Nullable injection points

98 views
Skip to first unread message

limpb...@gmail.com

unread,
Aug 21, 2007, 4:51:48 PM8/21/07
to google-guice
I like how Guice won't inject null by default.

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

Robbie Vanbrabant

unread,
Aug 21, 2007, 5:04:19 PM8/21/07
to google...@googlegroups.com
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
 

Dhanji R. Prasanna

unread,
Aug 21, 2007, 6:36:30 PM8/21/07
to google...@googlegroups.com
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.

Robbie Vanbrabant

unread,
Aug 22, 2007, 3:56:52 AM8/22/07
to google...@googlegroups.com
Because that's for the whole injection point and not a single dependency.

Robbie

dk

unread,
Aug 31, 2007, 1:03:27 PM8/31/07
to google-guice
hi, i am reading this post and found it interesting.

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
>

Reply all
Reply to author
Forward
0 new messages