Which @NotNull Java annotation should I use?

前端 未结 22 2822
梦如初夏
梦如初夏 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:37

    Spring 5 has @NonNullApi at the package level. This seems like a convenient choice for a project that already has Spring dependencies. All fields, parameters and return values default to @NonNull and @Nullable can be applied in the few places that differ.

    File package-info.java:

    @org.springframework.lang.NonNullApi
    package com.acme;
    

    https://docs.spring.io/spring-data/commons/docs/current/reference/html/#repositories.nullability.annotations

提交回复
热议问题