10

I often have code like this:

protected @Nullable Value value;

public boolean hasValue() { return value != null; }

the problem with this is that when I do null checks like so:

if (!hasValue()) throw...
return value.toString();

then IntelliJ will warn me about a possible NPE

whereas

if (value != null) throw...
return value.toString();

avoids this warning.

is there a way to decorate my hasValue() method so that IntelliJ knows it does a null check? and won't display the warning?

Andremoniy
  • 34,031
  • 20
  • 135
  • 241
ycomp
  • 8,316
  • 19
  • 57
  • 95
  • Is the `hasValue` really that simple? – Sotirios Delimanolis Feb 09 '17 at 01:43
  • 2
    what about the `@suppresswarnings( nullcheck )` annotation ? – blurfus Feb 09 '17 at 01:47
  • @SotiriosDelimanolis yes – ycomp Feb 09 '17 at 01:56
  • What's the version of Intellij-Idea that you use? My 14th version doesn't show me this warning (in this particular case). – Andremoniy Feb 09 '17 at 01:56
  • @Andremoniy It is the 2016.3.4. Oh.. I see the problem I use `@Nullable` to decorate the value field - if I removed that, it goes away. – ycomp Feb 09 '17 at 02:00
  • You can use @NotNull to indicate that a parameter should never be null. IntelliJ can add bytecode verification to your method in these cases. – Sam Barnum Feb 09 '17 at 02:02
  • What is `@Nullable?` From which package? – Andremoniy Feb 09 '17 at 02:06
  • @Andremoniy `package org.jetbrains.annotations;` I like to decorate the fields that are set by the constructors with `@Nullable/@NotNull – ycomp Feb 09 '17 at 02:07
  • If you're going to throw anyway why go through this rigamarole instead of just letting the NPE go? if it's a violation of contract, that's perfectly fine. – pvg Feb 09 '17 at 02:07
  • @pvg better exception message, easily recognizable what happened in the logs (in this case, I don't always do that for all checks - but in this case it `hasValue()` is kind of like an API method) – ycomp Feb 09 '17 at 02:08
  • Hardly seems worth the lines of ceremony. Plus if it's happening, it's actually a bug right? so it's not like some condition that requires its own information message. – pvg Feb 09 '17 at 02:10

1 Answers1

4

Intellij-Jetbrains is very clever IDE and itself suggests you way for solving many problems.

Look at screenshot below. It suggests your five ways for removing this warning:

1) add assert value != null;

2) replace return statement with return value != null ? value.toString() : null;

3) surround with

    if (value != null) {
        return value.toString();
    }

4) suppress inspection for this particular statement by adding commentary:

//noinspection ConstantConditions
return value.toString();

5) add at least, as earlier @ochi suggested use @SuppressWarnings("ConstantConditions") annotation which can be used for method or for whole class.

To invoke this context menu use shortcut keys Alt+Enter (I think they are common for all OS).

enter image description here

Andremoniy
  • 34,031
  • 20
  • 135
  • 241
  • 4
    All of these techiques indeed prevent IntelliJ from issuing the warning. However, none of them answers the original question of ensuring that "IntelliJ knows it [hasValue] does a null check". Some other tools have more sophisticated code analysis and support a postcondition annotation [`@EnsuresNonNullIf`](https://checkerframework.org/api/org/checkerframework/checker/nullness/qual/EnsuresNonNullIf.html) that does what the original poster asked -- but unfortunately IntelliJ currently does not. – mernst Feb 10 '17 at 17:02