为什么在运行时检查@Nonnull注释? [英] Why @Nonnull annotation checked at runtime?

查看:2934
本文介绍了为什么在运行时检查@Nonnull注释?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有以下签名的函数

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?

PS我在IntelliJ IDEA 2016.3中以调试模式运行Tomcat服务器,使用Oracle JDK 1.8.0_102

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆