Here are small code snippets of methods marked with org.jetbrains.annotations.NotNull. I find errors brought up by SpotBugs for those methods to be inconsistent with what actually can happened within them.
// Here SpotBugs informs that method that was marked non null might return null.
@NotNull
public List<String> getList(String value) {
return Collections.emptyList();
}
In the first example SpotBugs insists that method marked with Nonnull might actually return null, while Collections.emptyList(); always return reference to static EmptyList object. I would like to point out that here annotation seems to be treated as contract that promises that method in question will not return null, which is fine I believe.
// Here SpotBugs informs about redundant null check.
@NotNull
public String returnNotNullString() {
return getString();
}
private String getString() {
return "Hello";
}
In a second example SpotBugs treats @NotNull annotation as a non null check and therefore, views it as redundant. And it is true that in outlined example there is no way that method in question will ever return null value, but I would expect SpotBugs to treat annotation in question as a contract marker. It is true that this annotation might lead to addition of notnull check, but only in code that was complied in Itellij IDEA. Further more I believe outcome of the first example (where annotation is treated as a contract) contradicts outcome of second example (where it is treated as a null check).
So my question is, what might be the reason for SpotBugs to treat org.jetbrains.annotations.NotNull in such a weird manner?