[英]How to disable "unnecessary test for null" warning in NetBeans 14?
How do i disable the "unnecessary test for null" warning in NetBeans 14 IDE?如何禁用 NetBeans 14 IDE 中的“不必要的 null 测试”警告?
NetBeans as a well-known bug 1 2 3 4 where it will erroneously tell you that a test for null
is unnecessary. NetBeans 作为一个众所周知的错误1 2 3 4它会错误地告诉您对
null
的测试是不必要的。 For example in the following code:例如在以下代码中:
import javax.validation.constraints.NotNull;
private void doSomething(@NotNull Object o) {
if (o == null) return;
//...do more stuff...
}
The IDE thinks IDE认为
o
parameter was tagged as @NotNull o
o
参数被标记为@NotNull o
o
to be null
o
一定不可能是null
if
statement is unnecessaryif
语句是不必要的The @NotNull annotation is only an IDE hint, not a runtime guarantee. @NotNull注释只是一个 IDE 提示,不是运行时保证。
null
null
You can prove this to yourself by passing null
to the doSomething
method.您可以通过将
null
传递给doSomething
方法来证明这一点。 (we can even write the test code so the IDE generates no hints or warnings at all:) (我们甚至可以编写测试代码,这样 IDE 就不会产生任何提示或警告:)
Object o = getTestValue();
doSomething(o);
private Object getTestValue()
{
Object o = null;
return o;
}
private void doSomething(@NotNull Object o) {
Objects.requireNonNull(value);
//...do more stuff...
}
And watch doSomething
method fail - because o
is null
- even though it is tagged @NotNull .并观察
doSomething
方法失败 - 因为o
是null
- 即使它被标记为@NotNull 。
Now, there may be other implementations of @NotNull
, or other compilers that add runtime checks.现在,可能有
@NotNull
的其他实现,或者添加运行时检查的其他编译器。 I'm not talking about those.我不是在谈论那些。 The NetBeans IDE 14 warning is wrong, so i need to disable it.
NetBeans IDE 14 警告是错误的,所以我需要禁用它。
but it only offers to configure null deference warnings - which i definitely want to keep .但它只提供配置null 尊重警告 -我绝对想保留。
but nothing of value appears:但没有任何有价值的东西出现:
but it definitely has nothing to do with *unnecessary test for null.但这绝对与*对 null 的不必要测试无关。
but it's not there.但它不存在。
but it's not there.但它不存在。
but it's not there.但它不存在。
Which brings me to my question:这让我想到了我的问题:
You can turn off the " Unnecessary test for null " warning using the Java annotation @SuppressWarnings("null")
.您可以使用 Java 注释
@SuppressWarnings("null")
关闭“ Unnecessary test for null ”警告。 That annotation is found in java.lang , and there is no need for an import.该注释位于java.lang中,不需要导入。
The OpenJDK Javadoc for SuppressWarnings for JDK 17 states : OpenJDK Javadoc for SuppressWarnings for JDK 17 状态:
Indicates that the named compiler warnings should be suppressed in the annotated element (and in all program elements contained in the annotated element) ... As a matter of style, programmers should always use this annotation on the most deeply nested element where it is effective.
指示应在带注释的元素(以及带注释的元素中包含的所有程序元素)中抑制命名的编译器警告...作为一种风格,程序员应始终在有效的嵌套最深的元素上使用此注释. If you want to suppress a warning in a particular method, you should annotate that method rather than its class.
如果您想在特定方法中抑制警告,您应该注释该方法而不是它的类。
From the linked documentation to section 9.6.4.5 of the Java Language Specification , @SuppressWarnings
appears to do exactly what you want, with my emphasis added:从链接文档到 Java Language Specification 的第 9.6.4.5 节
@SuppressWarnings
似乎完全符合您的要求,我的重点是:
9.6.4.5.
9.6.4.5。 @SuppressWarnings
@SuppressWarnings
Java compilers are increasingly capable of issuing helpful "lint-like" warnings.
Java 编译器越来越有能力发出有用的“类似 lint”的警告。 To encourage the use of such warnings, there should be some way to disable a warning in a part of the program when the programmer knows that the warning is inappropriate .
为了鼓励使用此类警告,当程序员知道警告不合适时,应该有一些方法可以在程序的一部分中禁用警告。
Here's sample code, based on that in the OP:这是基于 OP 中的示例代码:
package suppression;
import javax.validation.constraints.NotNull; // Jakarta EE 8
//import jakarta.validation.constraints.NotNull; // Jakarta EE 9
public class Suppression {
public static void main(String[] args) {
Suppression supp = new Suppression();
Object o = supp.getTestValue();
supp.doSomething(o);
supp.doSomething2(o);
}
Object getTestValue() {
Object o = null;
return o;
}
private void doSomething(@NotNull Object o) {
if (o == null) {
System.out.println("Object is null!");
}
}
@SuppressWarnings("null")
private void doSomething2(@NotNull Object o) {
if (o == null) {
System.out.println("Object is null!");
}
}
}
Here's a screenshot of that code in NetBeans 14 which shows:这是 NetBeans 14 中该代码的屏幕截图,其中显示:
doSomething()
.doSomething()
中的第 22 行显示了不需要的警告“对 null 的不必要测试”。@SuppressWarnings("null")
on line 27 prevents the unwanted warning " Unnecessary test for null " being shown on line 30 in the otherwise identical method doSomething2()
.@SuppressWarnings("null")
可防止在第 30 行以其他相同的方法doSomething2()
显示不需要的警告“ Unnecessary test for null ”。 The answer is: it cannot be done.答案是:做不到。
NetBeans provides no way to disable the unnecessary test for null warning. NetBeans 无法禁用对 null 警告的不必要测试。
As other people in other answers have noted:正如其他答案中的其他人所指出的那样:
The correct way to resolve the (incorrect) warning is to obfuscate the check for null.解决(不正确)警告的正确方法是混淆对 null 的检查。
Rather than calling:而不是调用:
if (customer == null) { ... }
Instead call:而是调用:
if (Object.isNull(customer)) { ... }
It is the same thing;这是同一件事; except this way NetBeans doesn't realize that you're testing the variable for
null
, and so doesn't warn you.除了这种方式 NetBeans 没有意识到您正在测试
null
的变量,因此不会警告您。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.