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

MultipleNullnessAnnotations shouldn't consider Validation Annotations #4334

Open
xenoterracide opened this issue Mar 20, 2024 · 3 comments
Open

Comments

@xenoterracide
Copy link

xenoterracide commented Mar 20, 2024

The Jakarta (formerly JavaX) Validation spec shouldn't be considered for this bug. My rationale is that it is a runtime validation which may not occur until late in a process.

This is not, perhaps the best example of code, but it's what my brain is currently coming up with.

public class Foo {

  @NotNull
  @Nullable
  Long getId();
} 
if ( foo.getId() == null ) {
   foo.set(1L); // yes this is terrible, but you could query a database sequence, or this could be a UUID
}
repository.save(foo); // if I remove the above if statement, and a null is passed this would throw an error

so, getId is validly nullable at runtime, but at the point of validation we want it to be non null.

note: you could ignore the javax/jakarta namespaced annotations explicitly, but as far as I'm aware NotNull is always the name of the runtime checks, and some case insensitive variant of NonNull is the "compile time" variant

perhaps this is the whole point of this check though, it's docs are somewhat lacking; and if this is the point, perhaps they could be improved.

@cushon
Copy link
Collaborator

cushon commented Mar 20, 2024

cc @cpovirk

Thanks for the report. The use-case for MultipleNullnessAnnotations is primarily nullness annotations that have semantics similar to the https://github.com/jspecify/jspecify ones. We could look at having the check skip annotations with incompatible semantics, or clarifying what the use-case for it is.

@cpovirk
Copy link
Member

cpovirk commented Mar 20, 2024

Yes, thanks. I'm largely clueless about the validation annotations, which I have probably seen as many times in the past couple weeks as I had in my life previously.

We have at least one list of nullness annotations, which sadly includes at least one notably "@NotNull" annotation that is "similar to JSpecify" more than "similar to Jakarta Validation," org.jetbrains.annotations.NotNull. That doesn't make this proposal impossible to implement, but it keeps it from being a one-liner. For now, you might want to set -Xep:MultipleNullnessAnnotations:OFF.

@xenoterracide
Copy link
Author

Ugh, thanks jetbrains. Fortunately you can probably just use the two official validation spec paths for that. I don't know if I would bother going and tracking down other vendor specific ones. Like I'm sure hibernate had one at some point.

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

3 participants