Can I make IntelliJ @NonNull/Nullable annotations act like Eclipse
Asked Answered
M

0

3

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:

  1. @NonNull when interacting with other Un-Annotated items
  2. @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.

Munn answered 19/1, 2018 at 20:53 Comment(3)
I got "Argument 'mightBeNull' might be null" warning in the provided example. What IDEA version do you use? Do you use default inspections profile with only one change for "Treat non-annotated members and parameters as @Nullable"?Complexion
@Complexion I'm Using 2016.2.4 --but the point is that the code I wrote should not give a warning--that would leave warnings all over my code! It should only report a warning (Actually an error would be preferred) if the NonNull is added to the test method signature, then it MUST report an error unless the variable being passed is annotated NonNull or proven not null.Munn
Rewrote the question to be a bit more clear (I hope)Munn

© 2022 - 2024 — McMap. All rights reserved.