question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

2.x: Add @NonNull annotation for Function/Predicate/etc.

See original GitHub issue

One 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:closed
  • Created 7 years ago
  • Reactions:6
  • Comments:18 (11 by maintainers)

github_iconTop GitHub Comments

3reactions
dlewcommented, Nov 23, 2016

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:

void main() {
    Observable.just("Dan")
        .map(new Function<String, String>() {
            @Override
            public String apply(String s) throws Exception {
                return usuallyDependableMethod(s);
            }
        })
        .subscribe();
}

String usuallyDependableMethod(String name) {
    return (Math.random() < .95) ? "Hello " + name : null;
}

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.

3reactions
dlewcommented, Nov 23, 2016

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

Read more comments on GitHub >

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

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found