2.x: Add @NonNull annotation for Function/Predicate/etc.
See original GitHub issueOne of the larger tasks when transitioning from 1 to 2 is removing the use of null. Right now there’s no automated way to do it, but if we had @NonNull
then we’d be able to use static analysis to find where we’re returning null when we shouldn’t be.
I’m not sure the best way to go about it - if we should include jsr305, use Java 8’s NonNull, create our own, or something else.
Issue Analytics
- State:
- Created 7 years ago
- Reactions:6
- Comments:18 (11 by maintainers)
Top Results From Across the Web
2.x: Add @NonNull annotation for Function/Predicate/etc. #4876
I'm not sure about adding @NonNull to the functional interfaces though as it may add internal noise to RxJava. We could, however, annotate...
Read more >java - How to use @Nullable and @Nonnull annotations more ...
I understand the Nonnull annotation as documentation. The following method expresses that is requires (as a precondition) a non-null argument x .
Read more >Spring Null-Safety Annotations - Baeldung
A quick and practical guide to null-safety annotations in Spring. ... 2. The @NonNull Annotation. The @NonNull annotation is the most ...
Read more >@Nullable and @NotNull | IntelliJ IDEA Documentation
@Nullable and @NotNull annotations let you check nullability of a variable, parameter, or return value. They help you control contracts ...
Read more >497053 – [null][1.8] @NonNullByDefault affects fields even if ...
The class has set the @NonNullByDefault annotation, which leads to null checks for all fields in eclipse. The instance field "o" is ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Hmm, I’m most concerned about the functional interfaces, actually. Those are the ones where the app may accidentally crash during execution unexpectedly.
Here’s a contrived example:
I can imagine a lot of circumstances where coders don’t even realize that
usuallyDependableMethod()
could sometimes return null (when it’s not such an obvious problem). That’s where static analysis would really help out.It looks like Java 8 doesn’t actually include it - it’s part of a checker framework. More details here.
Regardless, I’m specifically targeting IntelliJ’s “Constant Conditions & Expressions” inspection and it looks like you can tell IntelliJ about which annotations should be considered non-null / nullable. So it would work if RxJava had its own
@NonNull
annotation included with the library (as long as developers are willing to do a little configuration on their end to accept it).The only downside to not using jsr305 is that we can’t use something like
@ParametersAreNonnullByDefault
which would be less verbose.If people are on board with this, I could start implementing this (but it’s a lot of busywork so I don’t want to bother unless people want it).