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
Unclear inspection warning "NullableProblems" in IntelliJ
提问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:
截屏:
Adding @NotNull
before the argument doesn't help:
@NotNull
在参数之前添加没有帮助:
采纳答案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 null
and adds a @NotNull
annotation 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 Comparable
interface.
您的覆盖方法不包含此注释,因此它会覆盖此行为,使参数可以为空 - 违反Comparable
接口的约定。
You can solve this by adding @NotNull
before the parameter.
您可以通过@NotNull
在参数前添加来解决此问题。
You can also disable this inspection by pressing Alt+ Enter, selecting the warning in the popup menu and selecting Disable inspection
in the sub-menu.
您也可以通过按Alt+ Enter,在弹出菜单中选择警告并Disable inspection
在子菜单中选择来禁用此检查。
Check out the Web Helpand this threadfor more information about @NotNull
/ @NonNull
annotations.
回答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
回答by Jeeppp
Its because that you are overriding a method that does not have a @NotNull
annotation.
这是因为您正在覆盖一个没有@NotNull
注释的方法。
IntelliJ IDEA warns you if the overriding method does not have a @NotNull
annotation.
如果覆盖方法没有@NotNull
注释,IntelliJ IDEA 会警告您。