IntelliJ IDEA的抱怨为@NotNull参数空检查 [英] IntelliJ IDEA complains about null check for @NotNull parameter

查看:12617
本文介绍了IntelliJ IDEA的抱怨为@NotNull参数空检查的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用Jetbrains的@可空/ @ NOTNULL注释在我的项目。

I want to use Jetbrains @Nullable/@NotNull Annotations in my project.

我有一个@NotNull场的类。构造函数自然不接受而是抛出一个异常,而不是。当然,这种构造函数的参数也有@NotNull进行注释。

I have a class with a @NotNull field. The constructor naturally does not accept null but throws an exception instead. Of course the parameter of this constructor is also be annotated with @NotNull.

为什么IntelliJ IDEA的抱怨空检查?该文件指出:

Why does IntelliJ IDEA complain about the null-check? The documentation states:

与NOTNULL注释的元素声明空值禁止返回(对于方法),传递给(参数)并保持(局部变量和字段)。

An element annotated with NotNull claims null value is forbidden to return (for methods), pass to (parameters) and hold (local variables and fields).

但我还是在运行时检查空值的情况下,构建系统不理解的注释,并接受像新车(空)声明。难道我错了吗?

But I still have to check for null-values at runtime in case the build system does not understand the Annotation and accepts a statement like new Car(null). Am I wrong?

推荐答案

如果您使用JetBrains的@NotNull注释,运行时断言将被添加到您的编译后的字节code,它会保证空是没有来过。然后,它真的是没有意义的写在源$ C ​​$ C相同的检查。这种方式工作得很好适合我们。

If you use JetBrains @NotNull annotation, runtime assertions will be added to your compiled bytecode that'll guarantee that null is not passed there. Then it really makes no sense to write the same checks in the source code. This approach works quite well for us.

如果您使用其它注释或只是不想仪字节code,可以禁用通过pressing ALT +这个特殊的警示输入就可以了,编辑检查设置,选中忽略断言声明。这样的条件语句被视为由IDE断言。

If you use other annotations or just don't want to instrument the bytecode, you can disable this particular warning by pressing Alt+Enter on it, "Edit inspection settings" and checking "Ignore assert statements". Such conditional statements are treated as assertions by the IDE.

这篇关于IntelliJ IDEA的抱怨为@NotNull参数空检查的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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