不带注释的参数覆盖@NotNull参数

huangapple go评论72阅读模式
英文:

Not annotated parameter overrides @NotNull parameter

问题

我正在使用带有启用的检查项“@NotNull/@Nullable problems”的IntelliJ IDEA 2020.2版本。在编写自定义ThreadFactory时,我会在传递给newThread方法的参数上收到IntelliJ IDEA检查警告:“Not annotated parameter overrides @NotNull parameter”:

@Override
public Thread newThread(Runnable runnable) {...}

java.util.concurrent.ThreadFactory接口未对runnable参数进行注释。该注释由IDEA的代码分析隐含表示。

我不想添加任何注释。除了禁用IntelliJ检查之外,我还能做些什么吗?(事实上,我会质疑为什么IDEA认为它不是空的,因为在那里传递空值并不会出错;你只会得到一个不运行runnable的线程。)

英文:

I'm using IntelliJ IDEA 2020.2 with the inspection "@NotNull/@Nullable problems" enabled. While writing a custom ThreadFactory, I get an IntelliJ IDEA inspection warning "Not annotated parameter overrides @NotNull parameter" on the argument to newThread:

@Override
public Thread newThread(Runnable runnable) {...}

The java.util.concurrent.ThreadFactory interface does not annotate runnable. The annotation is implied by IDEA's code analysis.

I don't want to add any annotation. Is there anything else I can do apart from disabling the IntelliJ inspection? (In fact, I'd question why IDEA thinks it's not null, as it wouldn't be an error to pass null there; you'd just get a thread that doesn't run the runnable.)

答案1

得分: 4

空值注释起源于JSR-305,而IntelliJ IDEA有自己的实现。

首先,IntelliJ IDEA有一个名为External Annotations的概念。它只是一个包含项目和JDK的空值注释的XML文件集合。您可以通过按下Command + ;(在macOS中)并导航到SDK、特定SDK和注释选项卡来找到外部配置:

打开所谓的jar文件,您可以在那里看到IDEA强制的默认注释:

因此,ThreadFactorynewThread方法已经被@NotNull注释。如果您覆盖了带有空值注释的方法并且没有提供空值注释,注释信息将被覆盖(即不再有空值注释信息可用),这正是IDEA警告报告的内容。实际上这是可以的,但您可以通过将您的newThread方法参数注释为@NotNull或者只是在SDK设置中删除jdkAnnotations.jar来消除警告。

实际上,从Spring中重写/实现方法也会产生类似的警告(例如,BeanPostProcessor),因为Spring通常在package-info.java文件中具有包级别的空值注释。

顺便提一下,JSR-305多年来一直处于休眠状态。在实际中存在多种实现:

  1. Jetbrains
  2. Spring
  3. com.google.code.findbugs:jsr305
  4. ...
英文:

The nullability annotations originate from JSR-305 and IntelliJ IDEA has its own implementation.

First, IntelliJ IDEA has a concept called External Annotations. It's just a collection of XML file containing nullability annotations for your project and JDK. You can find your external configuration pressing Command + ; (in macOS) and navigate to SDKs, specific SDK, and Annotations tab:

不带注释的参数覆盖@NotNull参数

Open the so-called jar, you can see the IDEA forced default annotation there:

不带注释的参数覆盖@NotNull参数

So the newThread method of ThreadFactory has @NotNull annotated. If you override nullability annotated method and don't provide nullability annotations, the annotation information is overridden (i.e., no nullability annotation information available anymore), which is exactly IDEA warning reports. This is actually ok, but you can make the warning disappear by annotating your newThread method parameter @NotNull or just delete the jdkAnnotations.jar in SDK settings.

In fact, overriding/implementing methods from Spring will incur similar warning (e.g., BeanPostProcessor) because Spring usually has a package level nullability annotation in package-info.java file.

BTW, JSR-305's status remains dormant for years. There exists multiple implementations in the wild:

  1. Jetbrains
  2. Spring
  3. com.google.code.findbugs:jsr305
  4. ...

huangapple
  • 本文由 发表于 2020年10月1日 21:32:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/64156483.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定