Skip to content

Allow injected member to declare that null is acceptable #112

Closed

Description

@gissuebot

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

Activity

  1. gissuebot commented on Jul 7, 2014

    @gissuebot
    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.

  2. gissuebot commented on Jul 7, 2014

    @gissuebot
    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).

  3. gissuebot commented on Jul 7, 2014

    @gissuebot
    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

  4. gissuebot commented on Jul 7, 2014

    @gissuebot
    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...

  5. gissuebot commented on Jul 7, 2014

    @gissuebot
    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...

  6. gissuebot commented on Jul 7, 2014

    @gissuebot
    Author

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

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

  7. gissuebot commented on Jul 7, 2014

    @gissuebot
    Author
  8. gissuebot commented on Jul 7, 2014

    @gissuebot
    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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions