Define a @NotNull getter for a @Nullable field

I commonly have a field that may be null, and a getter method that tests whether the field is null, and if so, initializes it. For example

@Nullable private PrintStream outputDestination = null;

@NotNull public PrintStream getOutputDestination() {
  if (outputDestination == null) {
    return (outputDestination = System.err);
  } else {
    return outputDestination;
}

The "Constant conditions" inspection (oddly named...) complains that I have a NotNull getter for a Nullable field. But it seems a perfectly reasonable thing to do. The only way I can get rid of the warning is to rename the field, which seems a bit unfriendly. It would be nice if these warnings could be individually suppressed like many other inspection warnings, but this does not appear to be possible.

1 comment
Comment actions Permalink

Please vote for http://youtrack.jetbrains.net/issue/IDEA-52606.

--
Nikolay Chashnikov
Software Developer
JetBrains, Inc
http://www.jetbrains.com
"Develop with pleasure!"

0

Please sign in to leave a comment.