Reputation: 1461
This sample program exhibits the behavior I do not understand:
public class Main {
public static String foo(String value) {
return "length: " + value.length();
}
public static void main(String[] args) {
System.out.println(foo(null));
}
}
The runtime behavior of this problem is clear: It throws a NullPointerException. What is not clear, however, is the warning I get from IntelliJ:
Passing 'null' argument to parameter annotated as @NotNull
Main
@Contract(pure = true)
@NotNull
public static String foo(
@NotNull String value
)
The Contract and NotNull annotations are obviously not in my code. I cannot tell which package they are from since IntelliJ does not support "go to declaration" in the warning tooltip, and I cannot use the normal "go to declaration" because, well, the annotations do not exist in the code.
This is a plain new project without any Maven/Gradle build script nor any included libraries, just to rule out any "magic" compile-time plugins that could try to add annotations.
This leaves the possibility that @NotNull adds itself. But how? Does it have some meta-annotations that trigger its own annotation processor which adds them to my code? Sadly I cannot even try to find out because I don't which package they are from.
What exactly is happening here?
Upvotes: 0
Views: 1749
Reputation: 273565
These are annotations inferred by IntelliJ IDEA. The IDE sees that you are doing value.length()
, so it infers that value
cannot be null. See also the blog post that introduced this feature.
You should be able to see a gutter icon looking like a "@". If you hover over it, it will show you want annotations are inferred:
Notice that there is also @Contract
in addition to @NotNull
. See this post for more about @Contract
.
If you don't see this, go to Settings -> Editor -> General -> Gutter Icons and turn them on. There are "Inferred nullability annotations" and "Inferred contract annotations".
You can inspect what the actual annotation types of these inferred annotations are, by going into Settings -> Editor -> Inspections -> Java -> Probable bugs -> Nullability and data flow problems,
Go to the "Options" section on the right, and you'll see a "Configure Annotations" button. Note that you may need to scroll down.
Upon clicking the button, you can see and choose the fully-qualified name for the nullable annotations.
IntelliJ supports such annotations from many different libraries. If you don't have any installed, you'll still have the built-in org.jetbrains.annotations.NotNull
.
Also in the Inspections section of the settings, you can disable/enable the reporting of warnings such as the one that you're getting.
See also the IntelliJ docs on this.
Upvotes: 4