You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Allow injected member to declare that null is acceptable #112
We must allow for injection of null. User demand is loud and clear, and if
you don't like it, you don't have to use it.
It would be great to just expand the meaning of @Inject(optional=true), but
that leaves you no way for some method/constructor parameters to be
nullable while others aren't.
Probably we should introduce a new annotation like @CanBeNull, and every
place of injection that is willing to accept a null must use this
annotation. We could also require that any Provider that might return null
must also use this annotation on its get() method, but I'm not sure whether
we could reasonably enforce that types provided by can-be-null providers
can only be injected into can-be-null injection points. That may be trying
to be too smart, and it may not always make sense.
Implemented!
Now injection points can be annotated as @Nullable, and they can be injected with null without error.
Consequences:
bind(Foo.class).toInstance(null) is not an error anymore. This violated some other assumptions about
constants, but I've cleaned those up.
scopes should support null as a regular value. Code like "if (map.get(key) == null)" may be problematic.
Guice doesn't include its own @Nullable annotation, but can piggyback on any @Nullable annotation type
whose simplename is "Nullable".
My implementation adds a new method, InternalContext#sanitize() that all InternalFactories should call on
their return values. The method is a no-op for non-null values.
I'm going to try this out with the large Guice-using codebase that I have access to, to try and find other
potential issues.
Uh oh, it looks like org.jetbrains.annotations.Nullable doesn't have runtime retention. This means that the code
doesn't work with that version of Nullable. Uh-oh...
Activity
gissuebot commentedon Jul 7, 2014
From robbie.vanbrabant on July 16, 2007 07:45:06
Another suggestion:
bind(SomeObject.class)
.annotatedWith(CanBeNull.class)
.toProvider(NullProvider.class);
That way we can choose the annotation, or even leave it out.
gissuebot commentedon Jul 7, 2014
From limpbizkit on August 21, 2007 17:16:52
What about just reusing
@Nullable from JSR-305/IntelliJ ? http://groups.google.com/group/google-guice/browse_frm/thread/1581ec0c1fb4cc68/c3b894b7a92207b4 Just give me the go-ahead, I'd love to implement this (and I need it).gissuebot commentedon Jul 7, 2014
From limpbizkit on August 25, 2007 01:29:58
Implemented!
Now injection points can be annotated as
@Nullable, and they can be injected with null without error.Consequences:
constants, but I've cleaned those up.
@Nullable annotation, but can piggyback on any@Nullable annotation typewhose simplename is "Nullable".
My implementation adds a new method, InternalContext#sanitize() that all InternalFactories should call on
their return values. The method is a no-op for non-null values.
I'm going to try this out with the large Guice-using codebase that I have access to, to try and find other
potential issues.
Status: Started
Owner: limpbizkit
Cc: kevinb9n
gissuebot commentedon Jul 7, 2014
From limpbizkit on August 26, 2007 00:13:05
Uh oh, it looks like org.jetbrains.annotations.Nullable doesn't have runtime retention. This means that the code
doesn't work with that version of Nullable. Uh-oh...
gissuebot commentedon Jul 7, 2014
From limpbizkit on August 27, 2007 10:56:56
I've posted to the JSR 305 lists describing how Guice benefits from runtime retention
for
@Nullable. Currently I believe they're swaying towards runtime retention anyway: http://groups.google.com/group/jsr-305/browse_thread/thread/99cb455eedf21801 Hopefully they commit to runtime retention! And if they do, we should be able topersuade IntelliJ to do likewise...
gissuebot commentedon Jul 7, 2014
From crazyboblee on August 27, 2007 11:19:32
Yeah,
@Nullable would be a lot less useful w/o runtime retention.gissuebot commentedon Jul 7, 2014
From limpbizkit on May 30, 2008 00:05:14
Posted to Jetbrains' forum: http://intellij.net/forums/thread.jspa?threadID=275503
Labels: -1.1 Milestone-Release2.0
gissuebot commentedon Jul 7, 2014
From limpbizkit on June 04, 2008 23:43:54
If anyone's interested, we've got JetBrains' blessing to patch the
@Nullable annotation: http://intellij.net/forums/thread.jspa?messageID=5215656 At a risk of offending users of other IDEs, we could post this on our downloads page. JSR-305 is probably ourbest bet in the longrun.
For now, Guice will recognize any annotation whose simplename is "Nullable".
Status: Fixed