Klitos Kyriacou
Klitos Kyriacou

Reputation: 11664

Not annotated parameter overrides @NotNull parameter

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.)

Upvotes: 4

Views: 5841

Answers (1)

guaner
guaner

Reputation: 82

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:

enter image description here

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

enter image description here

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. ...

Upvotes: 4

Related Questions