IntelliJ IDEA抱怨@NotNull参数的空检查

时间:2022-08-08 11:45:03

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

我想在我的项目中使用Jetbrains @ Nullable / @NotNull Annotations。

IntelliJ IDEA抱怨@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.

我有一个带有@NotNull字段的类。构造函数自然不接受null,而是抛出异常。当然,这个构造函数的参数也可以用@NotNull注释。

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

为什么IntelliJ IDEA会抱怨空检?文件说明:

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

使用NotNull声明的元素声明null值被禁止返回(对于方法),传递给(参数)并保持(局部变量和字段)。

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?

但是我仍然必须在运行时检查空值,以防构建系统不理解Annotation并接受像new Car(null)这样的语句。我错了吗?

2 个解决方案

#1


5  

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.

如果使用JetBrains @NotNull注释,运行时断言将被添加到已编译的字节码中,以保证null不会在那里传递。那么在源代码中编写相同的检查是没有意义的。这种方法对我们很有效。

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.

如果您使用其他注释或者只是不想检测字节码,可以通过按Alt + Enter,“编辑检查设置”并选中“忽略断言语句”来禁用此特定警告。这些条件语句被IDE视为断言。

#2


1  

If the parameter for the constructor is Not Null, Then there is no point of checking for null value with and if statement.

如果构造函数的参数是Not Null,那么使用和if语句检查null值是没有意义的。

And As we know that whatever value is held by parameter Engine is not null, this check will always be false.

而且我们知道参数Engine保持的值不为null,此检查将始终为false。

So the check that is shown in your screenshot will make complete sense. If you really don't want to see the null check inspection, then disable it from the preferences section.

因此,屏幕截图中显示的检查将完全合理。如果您确实不希望看到空检查检查,请从首选项部分禁用它。

In short, The value you are checking is not null inside the constructor and the IDE is aware of it.

简而言之,您检查的值在构造函数中不为null,IDE知道它。

#1


5  

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.

如果使用JetBrains @NotNull注释,运行时断言将被添加到已编译的字节码中,以保证null不会在那里传递。那么在源代码中编写相同的检查是没有意义的。这种方法对我们很有效。

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.

如果您使用其他注释或者只是不想检测字节码,可以通过按Alt + Enter,“编辑检查设置”并选中“忽略断言语句”来禁用此特定警告。这些条件语句被IDE视为断言。

#2


1  

If the parameter for the constructor is Not Null, Then there is no point of checking for null value with and if statement.

如果构造函数的参数是Not Null,那么使用和if语句检查null值是没有意义的。

And As we know that whatever value is held by parameter Engine is not null, this check will always be false.

而且我们知道参数Engine保持的值不为null,此检查将始终为false。

So the check that is shown in your screenshot will make complete sense. If you really don't want to see the null check inspection, then disable it from the preferences section.

因此,屏幕截图中显示的检查将完全合理。如果您确实不希望看到空检查检查,请从首选项部分禁用它。

In short, The value you are checking is not null inside the constructor and the IDE is aware of it.

简而言之,您检查的值在构造函数中不为null,IDE知道它。