Summary: Is there any way to configure IntelliJ to not add errors to existing un-annotated code but NEVER allow a null to be passed into a @NonNull parameter?
Details: Eclipse actually has 3 "nullable" states, @NonNull, @Nullable and Unannotated.
IntelliJ appears to have 2 types of nullable, you treat Un-annotated as either @NonNull or @Nullable, Un-annotated does not have it's own behavior.
All three states are pretty important if you have a large existing project and want to start using the annotations.
Question: Am I missing a configuration option or are the annotations assumed to be an all-or-nothing in IntelliJ?
tl;dr: (why all three types are needed)
In eclipse the Un-annotated will act as:
- @NonNull when interacting with other Un-Annotated items
- @Nullable when being passed to something annotated with @NonNull
The first is needed so that warnings will not be created on existing un-annotated code like "str.length()"
The second is so that you will be warned if you attempt to pass an untyped potential null to a method annotated with @NonNull.
This combination allows you to slowly push annotations through your otherwise un-annotated code.
In Intellij an option called "Treat non-annotated members and parameters as @Nullable defines the behavior of un-annotated variables/code, if this is off they act as either @Non-Null, if it's on they are @Nullable.
Turning on the "Treat as @Nullable" option is great if you assume your entire codebase is annotated, but if you are working with existing code it will cause endless warnings in un-annotated cases like this:
public void test(String s) {
System.out.println(s.length());
}
public void callTest(String maybeNull) {
test(maybeNull);
}
If I use IntelliJ's setting specifying that un-annotated should be treated as Nullable, this un-annotated code gains a warning because s is nullable and must be null-checked before s.length() can be called. I don't want to see that kind of warning throughout my code until I'm ready to add the annotations.
On the other hand, If I assume the default settings then when I start to apply annotations by changing this line:
public void test(@NonNull String s) {
It does NOT cause the expected error in the caller because IntelliJ assumes that the un-annotated maybeNull should be treated as @NonNull. We absolutely want a warning here because maybeNull actually could be null--otherwise the annotations aren't doing anything for us.