Null annotations and Inspect code... Follow
Answered
Hi all,
I would like to use the Null annotations from Jetbrains and use the Code inspection to find errors,
but it seems I'm doing something wrong because I got no meaning full warnings while I'm coding
and also none during Code inspection...
I'm quite new with IntelliJ so most probably I'm doing something wrong.
Here is a test class where I would expect to get some warnings while I'm typing them, but at the least
when I trigger a Code inspection:
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.Random;
public class TestNullable {
public void safeFoo(@NotNull Object aValue) {
aValue.hashCode();
}
public <T> @NotNull T ensureNotNull(@NotNull T aValue, String aMessage) {
if(aValue==null) {
// I would expect to get here a warning that I unnecessarily do check for null
// but I would also like to know how to do it nonetheless without getting a warnign
throw new IllegalArgumentException(aMessage);
}
return aValue;
}
public void unsafeFoo(@Nullable Object aValue) {
aValue.hashCode(); // I would have expected a warning or error here, because I provoke a NPE
}
public void callingFoo(@Nullable Object aValue) {
// I would expect an warning that I use a @Nullable object in place of a @NotNull object without checking for not nullness
unnecessaryNullCheck(aValue);
// same as above
safeFoo(aValue);
unsafeFoo(aValue);
}
public void unnecessaryNullCheck(@NotNull Object aValue) {
if(aValue!=null) {
System.out.println(aValue+" is not null");
}
}
public static Object generateRandomNull() {
return (new Random().nextBoolean())
? null
: new Object();
}
public static void main(String[] args) {
new TestNullable().callingFoo(generateRandomNull());
}
}
Best regards,
Dieter
Please sign in to leave a comment.
Please go to File | Settings | Editor | Inspections | "Constant conditions and exceptions". Is the inspection enabled? If you press "Configure annotations" in its settings, do you see jetbrains @Nullable/@NotNull annotations in the lists?
Hi Peter, thanks for the answer. I checked that I had the settings and I indeed had them as described.
But then I fiddled with the IDE around, I for example downloaded the intellij-samples project from github and had the same problem.
But after I closed IntelliJ and restartet it, it worked again. I have no clue what happened. But now the @Null annotations are working.
Thanks for your support.