Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

@NotNull, @Nonnull etc. all don't work in IntelliJ IDEA

I have tried annotating a field with

  • org.checkerframework.checker.nullness.qual.NonNull
  • org.jetbrains.annotations.NotNull
  • javax.annotation.Nonnull

And in all cases, assigning a null to it generates no complaints from IntelliJ 2016.2.

public class GreetingController {
    @NotNull Integer x = 3;
    public void foo() { x = null; }
}

That all compiles fine according to IntelliJ.

This page from IntelliJ specifically states that "IntelliJ IDEA highlights the problems “on-the-fly”, so you can see the inspection results right in the editor." I have even copied the example code (public class TestNullable) into my editor and it produces no errors.

This other page from IntelliJ states you can change the annotations it responds to. So I chose javax.annotation.Nonnull and made sure that was the one I was using in my code, still no luck.

To be clear, what I'm hoping for, and what I understand should be provided, is that the editor window / compiler alerts me to the problem (I am not looking for a runtime check, NullPointerException already works fine at runtime.)

In case it didn't work in real time, I tried "Rebuild Project".

I'm sure this must work, what am I doing wrong?

I have uploaded an example of this not working here: ZIP download.

like image 604
Adrian Smith Avatar asked Dec 23 '22 18:12

Adrian Smith


2 Answers

As I can see from your screenshots and the sample project, IntelliJ IDEA does show you the warnings. Note that these warnings are shown by the code inspections which are running on the fly and will be displayed in the editor or in the Analyze | Inspect Code results. These warnings will not be displayed by the compiler.

warning

Note that you can configure the warnings highlighting if needed (for example add the underwave effect):

warning

You can also change the severity of the inspection (like to Error):

severity

You may also want to vote for this feature request:

  • IDEA-78625 Provide inspection severity level that will work like validation and abort compilation

As a bonus, pay attention to the javax.annotation.Nullable annotation, it may be not what you think it's for, see this comment and the documentation. For some years IntelliJ IDEA has incorrectly suggested to use this annotation, while the correct one for such cases would be javax.annotation.CheckForNull:

This annotation is useful mostly for overriding a Nonnull annotation. Static analysis tools should generally treat the annotated items as though they had no annotation, unless they are configured to minimize false negatives. Use CheckForNull to indicate that the element value should always be checked for a null value.

like image 199
CrazyCoder Avatar answered Dec 26 '22 11:12

CrazyCoder


  1. "Settings" > "Inspections" > "Probable Bugs" > "Constant conditions & exceptions"
  2. Tick the first option: "Suggest @NotNull annotation for methods that possibly return null and report nullable values passed to non-annotated parameters.
  3. Click "Configure Annotations". By default, Intellij will use their own annotations from org.jetbrains.annotation. I was using the more general (my own opinion) annotations from javax.annotation.
  4. I set Nullable to: javax.annotation.Nullable
  5. I set NotNUll to : javax.annotation.NotNull
  6. In order to set these new options, you must click them, then click the tiny checkmark button to the right to set it. Selecting the javax.annotation annotations then hitting "OK" will NOT lock in the new settings, you must use the checkbox button.

After successfully specifying javax.annotation.Nullable and javax.annotation.NotNull, the code correctly highlighted null problems.

like image 43
AuthorOfTheSurf Avatar answered Dec 26 '22 11:12

AuthorOfTheSurf