精简版
如何在 NetBeans 14 IDE 中禁用“不必要的 null 测试”警告?
长版
NetBeans 作为一个众所周知的错误 1 2 3 4 它会错误地告诉您不需要对 null
进行测试。例如在下面的代码中:
import javax.validation.constraints.NotNull;
private void doSomething(@NotNull Object o) {
if (o == null) return;
//...do more stuff...
}
IDE 思考
- 因为
o
参数被标记为 @NotNullo
o
必须不可能null
- 所以一定是
if
语句是不必要的
这显然是错误的
@NotNull 注解只是一个 IDE 提示,不是运行时保证。
- 仅仅因为方法的参数被标记为@NotNullable
- 并不意味着它不能为
null
您可以通过将 null
传递给 doSomething
方法来向自己证明这一点。 (我们甚至可以编写测试代码,这样 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...
}
然后观察 doSomething
方法失败 - 因为 o
是 null
- 即使它被标记为 @NotNull。
现在,可能还有 @NotNull
的其他实现,或者其他添加运行时检查的编译器。我不是在说那些。 NetBeans IDE 14 警告是错误的,所以我需要禁用它。
研究成果
- 我尝试点击灯泡,希望配置警告:
但它只提供配置null deference 警告 - 我绝对想保留。
- 我尝试按 Alt+Enter 调出更多选项:
但没有任何有值(value)的东西出现:
- 我试着让它把我带到配置Null dereferncing 提示的区域:
但它肯定与 *unnecessary test for null没有有关。
- 我尝试搜索名为“null”的提示或警告:
但它不在那里。
- 我尝试搜索名为“不必要的”的提示或警告:
但它不在那里。
- 我尝试搜索名为“test”的提示或警告:
但它不在那里。
如何关闭它
这让我想到了我的问题:
- 鉴于 NetBeans IDE 14 无法关闭“unnecessary test for null”警告
- 如何关闭 NetBeans IDE 14 中的“不必要的 null 测试” 警告?
红利阅读
- What does unnecessary test for null mean?
- Netbeans tells me to remove null comparison, but that breaks my code ("首先要注意的是:NetBeans 是错误的")
- BugZilla:Bug 226923 - Wrong warning 'the expression is never null'
- Bugzilla:Bug 262707 - Add configuration of Nullable/NonNull annotations. Use FQNs or allow to exclude FQNs.
最佳答案
您可以使用 Java 注释 @SuppressWarnings("null")
关闭“Unnecessary test for null”警告。该注释位于 java.lang 中,无需导入。
The OpenJDK Javadoc for SuppressWarnings for JDK 17 states :
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.
来自 the linked documentation to section 9.6.4.5 of the Java Language Specification , @SuppressWarnings
似乎完全按照您的意愿行事,我强调了这一点:
9.6.4.5. @SuppressWarnings
Java compilers are increasingly capable of issuing helpful "lint-like" warnings. 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.
这是基于 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!");
}
}
}
这是 NetBeans 14 中该代码的屏幕截图,其中显示:
- 不需要的警告“Unnecessary test for null”显示在方法
doSomething()
的第 22 行。 - 第 27 行的注解
@SuppressWarnings("null")
防止在第 30 行以其他相同的方法显示不需要的警告“Unnecessary test for null”doSomething2()
.
关于java - 如何在 NetBeans 14 中禁用 "unnecessary test for null"警告?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72972146/