Which @NotNull Java annotation should I use?

前端 未结 22 3416
梦如初夏
梦如初夏 2020-11-22 02:44

I\'m looking to make my code more readable as well as use tooling like IDE code inspection and/or static code analysis (FindBugs and Sonar) to avoid NullPointerExceptions.

22条回答
  •  生来不讨喜
    2020-11-22 03:25

    If you're developing for android, you're somewhat tied to Eclipse (edit: at time of writing, not anymore), which has its own annotations. It's included in Eclipse 3.8+ (Juno), but disabled by default.

    You can enable it at Preferences > Java > Compiler > Errors/Warnings > Null analysis (collapsable section at the bottom).

    Check "Enable annotation-based null analysis"

    http://wiki.eclipse.org/JDT_Core/Null_Analysis#Usage has recommendations on settings. However, if you have external projects in your workspace (like the facebook SDK), they may not satisfy those recommendations, and you probably don't want to fix them with each SDK update ;-)

    I use:

    1. Null pointer access: Error
    2. Violation of null specification: Error (linked to point #1)
    3. Potential null pointer access: Warning (otherwise facebook SDK would have warnings)
    4. Conflict between null annotations and null inference: Warning (linked to point #3)

提交回复
热议问题