Why @Nonnull annotation checked at runtime?

别说谁变了你拦得住时间么 提交于 2019-12-17 17:53:24

问题


I have a function with following signature

public static String myFunction(@Nonnull String param)

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 supposed not to be checked at runtime.

Who actually throws the exception and why?

P.S. I run Tomcat server in debug mode from IntelliJ IDEA 2016.3 with Oracle JDK 1.8.0_102


回答1:


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:

Settings | Build, Execution, Deployment | Compiler | [x] Add runtime assertions for not-null-annotated methods and parameters.



来源:https://stackoverflow.com/questions/40847472/why-nonnull-annotation-checked-at-runtime

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!