Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow injected member to declare that null is acceptable #112

Closed
gissuebot opened this issue Jul 7, 2014 · 8 comments
Closed

Allow injected member to declare that null is acceptable #112

gissuebot opened this issue Jul 7, 2014 · 8 comments

Comments

@gissuebot
Copy link

From kevinb9n on June 06, 2007 11:37:41

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.

Original issue: http://code.google.com/p/google-guice/issues/detail?id=112

@gissuebot
Copy link
Author

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
Copy link
Author

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
Copy link
Author

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:

  • 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.

Status: Started
Owner: limpbizkit
Cc: kevinb9n

@gissuebot
Copy link
Author

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
Copy link
Author

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 to
persuade IntelliJ to do likewise...

@gissuebot
Copy link
Author

From crazyboblee on August 27, 2007 11:19:32

Yeah, @Nullable would be a lot less useful w/o runtime retention.

@gissuebot
Copy link
Author

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
Copy link
Author

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 our
best bet in the longrun.

For now, Guice will recognize any annotation whose simplename is "Nullable".

Status: Fixed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant