java - 如何在 NetBeans 14 中禁用 "unnecessary test for null"警告?

标签 java netbeans null-check netbeans-14

精简版

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

enter image description here

长版

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 方法失败 - 因为 onull - 即使它被标记为 @NotNull

现在,可能还有 @NotNull 的其他实现,或者其他添加运行时检查的编译器。我不是在说那些。 NetBeans IDE 14 警告是错误的,所以我需要禁用它。

研究成果

  1. 我尝试点击灯泡,希望配置警告:

enter image description here

但它只提供配置null deference 警告 - 我绝对想保留

  1. 我尝试按 Alt+Enter 调出更多选项:

enter image description here

但没有任何有值(value)的东西出现:

  1. 我试着让它把我带到配置Null dereferncing 提示的区域:

enter image description here

但它肯定与 *unnecessary test for null没有有关。

  1. 我尝试搜索名为“null”的提示或警告:

enter image description here

但它不在那里。

  1. 我尝试搜索名为“不必要的”的提示或警告:

enter image description here

但它不在那里。

  1. 我尝试搜索名为“test”的提示或警告:

enter image description here

但它不在那里。

如何关闭它

这让我想到了我的问题:

  • 鉴于 NetBeans IDE 14 无法关闭“unnecessary test for null”警告
  • 如何关闭 NetBeans IDE 14 中的“不必要的 null 测试” 警告?

红利阅读

最佳答案

您可以使用 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 nulldoSomething2().

SuppressWarnings

关于java - 如何在 NetBeans 14 中禁用 "unnecessary test for null"警告?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72972146/

相关文章:

java - 属性 'fixed-rate' 不允许出现在元素 'int:poller' 中

tomcat - netbeans 7.1.3 tomcat 服务器位置无效

java - Java 中的空检查

kotlin - != null VS ?.let { } kotlin 中不可变变量的性能

c - NULL 检查多个文件指针的最佳/最快方法?

java - 生成自定义 Java Getter 和 Setter

java - 没有 Maven 的 Drools Kie 应用程序

java - 静态 Sprite 批处理?

java - 将远程 Mysql 连接到 Netbeans

java - 为什么 Netbeans 不能正确显示波斯语字符