@NotNull 注解和 @NonNull

@NotNull 注解和@NonNull 注解 有很多不兼容的实现, 这个问题Which @NotNull Java annotation should I use?下列举了@NotNull的两个实现和@NonNull的5个实现。

@NotNull

org.jetbrains.annotations.NotNull

这是JetBrain的实现, 可以跟IDEA很好的兼容。在IDEA中对BEAN使用该注解可以在编辑阶段和运行阶段对Bean的空指针问题做检查。

import org.jetbrains.annotations.NotNull;

/**
 * @author longxingjian 
 * Created on 2020-12-31
 */
public class GuavaTest {
    private static Integer notNullMethod(@NotNull Integer integer){
        return integer;
    }

    public static void main(String[] args) {
        Integer a = null;
        Integer b = 1;
        System.out.println(notNullMethod(b));
        System.out.println(notNullMethod(a));
    }
}

编辑阶段检查:

编辑检查可能需要对IDEA进行配置才能生效,--> Editor | Inspections | Java | Probable bugs.

尝试运行检查:

运行时检查也可能需要配置才能生效:--> Build, Execution, Deployment | Compiler

原文地址:https://www.cnblogs.com/greatLong/p/14316894.html