Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Invalid or insuficient scoping construct for @Nullable and @NonNull annotations #6402

Closed
elopezvalecky opened this issue Jan 17, 2024 · 2 comments

Comments

@elopezvalecky
Copy link

The @Nullable and @NonNull annotations does not meet all possible use of it as it is done in jrs305 or even the Jakarta EE

java: scoping construct cannot be annotated with type-use annotation: @org.checkerframework.checker.nullness.qual.Nullable
For a construct like

@Nullable
public Map.Entry<Object, Object> intersects() {
...
}

Comparing to other implementations of @Nullable for example, I found that in checker-qual, the annotation is defined as

...
@Target({ElementType.TYPE_USE, ElementType.TYPE_PARAMETER})
...
public @interface Nullable {}

And this prevents the use of the annotations at any scope causing issues on migration when moving away from findbugs:jsr305

But looking at jakarta.annotations:jakarta.annotations-api:2.1.1 for example, the annotation is defined without @Target (source: https://github.com/jakartaee/common-annotations-api/blob/2.1.1/api/src/main/java/jakarta/annotation/Nullable.java) same as FindBugs one

Also, JetBrain defines this annotation, but for this particular one they make use of @Target but they do declare all possibles locations explicitly (source: https://github.com/JetBrains/java-annotations/blob/master/common/src/main/java/org/jetbrains/annotations/Nullable.java)

@mernst mernst self-assigned this Jan 17, 2024
@mernst
Copy link
Member

mernst commented Jan 17, 2024

If you search for the error message in the Checker Framework manual, you will find https://checkerframework.org/manual/#common-problems-non-typechecking which explains that @Nullable Map.Entry is wrong since @Nullable refers to Entry, not to Map.
Other relevant sections are https://checkerframework.org/manual/#faq-declaration-annotations-moved and https://checkerframework.org/manual/#faq-type-annotation-formatting .

It is true that the @Nullable of the Checker Framework is not a drop-in replacement for every other @Nullable annotation (that would be impossible, since they differ). You need to:

  • change your imports and
  • fix wrong syntax in the few places it occurs.

The Checker Framework uses type annotations, which is the right approach for Java 8+ and which other toolsets (except abandoned ones like jsr305) are also coming to adopt.

@mernst mernst removed their assignment Jan 17, 2024
@elopezvalecky
Copy link
Author

Thanks so much for the reply, definitely there are some Java fundamental that I might have to revisit again. 😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants