It doesn't fully solve the problem, but @lombok.NonNull helps a lot. It makes it clear which properties shouldn't be null, and catches NPEs closer to the source. Incidentally, lombok in general does wonders for boilerplate reduction.https://projectlombok.org/
topspin|3 years ago
Which @NonNull? There's javax.validation.constraints.NotNull, org.springframework.lang.NonNull, org.checkerframework.checker.nullness.qual.NonNull, org.jetbrains.annotations.NotNull, android.support.annotation.NonNull and a bunch of others[1]. The proliferation of Not|NonNull is evidence that I'm right, no matter how hard I get downed on HN.
[1] https://stackoverflow.com/questions/35892063/which-nonnull-j...
bhuber|3 years ago
Realistically, null is so fundamental to the Java language that removing it would arguably result in a different language entirely. Certainly all existing java codebases would have to be refactored. The same goes for exceptions. That's obviously not an option when one of your primary selling points is backwards compatibility, so I'm not really sure what kind of solution you're looking for here.
The answer to your SO link notwithstanding, I would argue the @lombok.NonNull is at least one of the best options, as it actually generates a null check that is executed at runtime. This makes it more powerful than most of the other solutions.
kaba0|3 years ago