简体   繁体   中英

@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 .

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.

警告

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

警告

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

严重

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.

  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.

The best that this can do is offer up warnings. It will not stop compilation from happening, since the annotations do not prohibit or preclude code compilation from taking place.

Be sure that you have the appropriate inspections enabled in your IDE, and be sure that you remain aware of what parameters you're passing into your method. The IDE can at best warn you, but it can't really stop you.

Alternatively, introduce a unit test to fail if that method receives a null parameter, and rely on that to ensure that you're not breaking code or expectations.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM