java IntelliJ 中不明确的检查警告“NullableProblems”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31242459/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 18:19:31  来源:igfitidea点击:

Unclear inspection warning "NullableProblems" in IntelliJ

javaintellij-ideacode-inspection

提问by traveh

Why am I getting a warning from the "NullableProblems" inspection in IntelliJ on this:

为什么我会从 IntelliJ 中的“NullableProblems”检查中收到警告:

public class Test implements Comparable<Test> {
    @Override
    public int compareTo(Test o) {
        return 0;
    }
}

I'm using IntelliJ 14.1.4 and compiling with Java 1.7

我正在使用 IntelliJ 14.1.4 并使用 Java 1.7 进行编译

Screenshot:

截屏:

enter image description here

在此处输入图片说明

Adding @NotNullbefore the argument doesn't help:

@NotNull在参数之前添加没有帮助:

enter image description here

在此处输入图片说明

采纳答案by Darek Kay

From Comparable.compareTo:

来自Comparable.compareTo

@throws NullPointerException if the specified object is null

So IntelliJ knows, that the object should not be nulland adds a @NotNullannotation automatically:

所以 IntelliJ 知道,对象不应该是null自动添加@NotNull注释:

IntelliJ IDEA will look carefully at SDK and libraries bytecode and will infer these annotations automatically so that they can later be used to analyze source code to spot places where you overlooked null.

IntelliJ IDEA 将仔细查看 SDK 和库字节码,并自动推断这些注释,以便以后可以使用它们来分析源代码以发现您忽略 null 的地方。

Your overriden method doesn't include this annotation, so it overridesthis behavior making the parameter nullable - against the contract of the Comparableinterface.

您的覆盖方法不包含此注释,因此它会覆盖此行为,使参数可以为空 - 违反Comparable接口的约定。

You can solve this by adding @NotNullbefore the parameter.

您可以通过@NotNull在参数前添加来解决此问题。

You can also disable this inspection by pressing Alt+ Enter, selecting the warning in the popup menu and selecting Disable inspectionin the sub-menu.

您也可以通过按Alt+ Enter,在弹出菜单中选择警告并Disable inspection在子菜单中选择来禁用此检查。

Check out the Web Helpand this threadfor more information about @NotNull/ @NonNullannotations.

查看Web 帮助此线程以获取有关@NotNull/@NonNull注释的更多信息。

回答by magiccrafter

This can be globally configured in IntelliJ IDEA easily and for me personally is the recommended way. If you want you can add your own annotations.
i.e. javax.validation.constraints.NotNull

这可以轻松地在 IntelliJ IDEA 中全局配置,对我个人来说是推荐的方式。如果需要,您可以添加自己的注释。
IEjavax.validation.constraints.NotNull

Path to the setting:
Settings > Editor > Inspections > @NotNull/@Nullable problems > Configure annotations

设置路径:
Settings > Editor > Inspections > @NotNull/@Nullable problems > Configure annotations

Some screenshots: enter image description hereenter image description here

一些截图: 在此处输入图片说明在此处输入图片说明

回答by Jeeppp

Its because that you are overriding a method that does not have a @NotNullannotation.

这是因为您正在覆盖一个没有@NotNull注释的方法。

IntelliJ IDEA warns you if the overriding method does not have a @NotNullannotation.

如果覆盖方法没有@NotNull注释,IntelliJ IDEA 会警告您。