简体   繁体   English

如何在 NetBeans 14 中禁用“不必要的 null 测试”警告?

[英]How to disable "unnecessary test for null" warning in NetBeans 14?

Short Version精简版

How do i disable the "unnecessary test for null" warning in NetBeans 14 IDE?如何禁用 NetBeans 14 IDE 中的“不必要的 null 测试”警告?

在此处输入图像描述

Long Version长版

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认为

  • because the o parameter was tagged as @NotNull o因为o参数被标记为@NotNull o
  • it must be impossible for o to be null o一定不可能null
  • so it must be that the if statement is unnecessary所以一定是if语句是不必要的

This is demonstrably false这显然是错误的

The @NotNull annotation is only an IDE hint, not a runtime guarantee. @NotNull注释只是一个 IDE 提示,不是运行时保证。

  • just because an argument to a method is tagged as @NotNull able仅仅因为方法的参数被标记为@NotNull可以
  • does not mean it cannot be 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方法失败 - 因为onull - 即使它被标记为@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 警告是错误的,所以我需要禁用它。

Research Effort研究工作

  1. I tried clicking the lightbulb, to hopefully configure the warning:我尝试单击灯泡,希望能配置警告:

在此处输入图像描述

but it only offers to configure null deference warnings - which i definitely want to keep .但它只提供配置null 尊重警告 -我绝对想保留

  1. I tried pressing Alt + Enter to bring up more options:我尝试按Alt + Enter以显示更多选项:

在此处输入图像描述

but nothing of value appears:但没有任何有价值的东西出现:

  1. I tried to let it bring me to the area to configure the Null dereferncing hint:我试图让它带我到该区域配置Null 取消引用提示:

在此处输入图像描述

but it definitely has nothing to do with *unnecessary test for null.但这绝对与*对 null 的不必要测试无关

  1. I tried searching for a hint or warning named "null" :我尝试搜索名为"null"的提示或警告:

在此处输入图像描述

but it's not there.但它不存在。

  1. I tried searching for a hint or warning named "unnecessary" :我尝试搜索名为“不必要”的提示或警告:

在此处输入图像描述

but it's not there.但它不存在。

  1. I tried searching for a hint or warning named "test" :我尝试搜索名为“test”的提示或警告:

在此处输入图像描述

but it's not there.但它不存在。

How to turn it off如何关闭它

Which brings me to my question:这让我想到了我的问题:

  • given that NetBeans IDE 14 has no way to turn off "unnecessary test for null" warning鉴于 NetBeans IDE 14 无法关闭“不必要的空值测试”警告
  • how do i turn off the "unnecessary test for null" warning in NetBeans IDE 14?如何关闭 NetBeans IDE 14 中的“不必要的 null 测试”警告?

Bonus Reading奖金阅读

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 中该代码的屏幕截图,其中显示:

  • The unwanted warning " Unnecessary test for null " is shown on line 22 in method doSomething() .方法doSomething()中的第 22 行显示了不需要的警告“对 null 的不必要测试”。
  • The annotation @SuppressWarnings("null") on line 27 prevents the unwanted warning " Unnecessary test for null " being shown on line 30 in the otherwise identical method doSomething2() .第 27 行的注释@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 警告的不必要测试

Workaround解决方法

As other people in other answers have noted:正如其他答案中的其他人所指出的那样:

  • the value can be null该值可以是 null
  • NetBeans is wrong thinking it cannot be null NetBeans 错误地认为它不可能是 null

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM