为什么在运行时检查@Nonnull 注释? [英] Why @Nonnull annotation checked at runtime?
问题描述
我有一个带有以下签名的函数
I have a function with following signature
public static String myFunction(@Nonnull String param)
当我用 param 为 null 调用它时,我得到以下异常:
When I call it with param as null, I get the following exception:
Caused by: java.lang.IllegalArgumentException: Argument for @Nonnull parameter 'param' of com/MyClass.myFunction must not be null
at com.MyClass.$$$reportNull$$$0(MyClass.java)
javax.annotation.Nonnull 不应该在运行时检查.
javax.annotation.Nonnull supposed not to be checked at runtime.
实际上是谁抛出异常,为什么?
Who actually throws the exception and why?
附言我在 IntelliJ IDEA 2016.3 和 Oracle JDK 1.8.0_102 的调试模式下运行 Tomcat 服务器
P.S. I run Tomcat server in debug mode from IntelliJ IDEA 2016.3 with Oracle JDK 1.8.0_102
推荐答案
当您在 IntelliJ IDEA 中编译您的项目时,它会检测已编译类的字节码,以便为各种类型的 @Nonnull
添加运行时检查注释.此行为由选项控制:
When you compile your project in IntelliJ IDEA, it instruments the bytecode of compiled classes to add runtime checks for various flavors of @Nonnull
annotations. This behavior is controlled by the option:
设置|构建、执行、部署 |编译器 |[x] 为非空注解的方法和参数添加运行时断言.
Settings | Build, Execution, Deployment | Compiler | [x] Add runtime assertions for not-null-annotated methods and parameters.
这篇关于为什么在运行时检查@Nonnull 注释?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!