-
Notifications
You must be signed in to change notification settings - Fork 7.6k
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
2.x: Add @NonNull annotation for Function/Predicate/etc. #4876
Comments
We can't do external dependencies like jsr305 and I can't find |
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 The only downside to not using jsr305 is that we can't use something like 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). |
Looks like there is an interest for new annotations, #4878, so it this has to be coordinated with that. I'm not sure about adding |
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 |
Another +1 reason for this would be that Kotlin automatically changes variables to Not null as a language feature if they are annotated with https://kotlinlang.org/docs/reference/java-interop.html#nullability-annotations Therefore, Kotlin users using RxJava would have the benefit of knowing that their values in something like @akarnokd Can you give a reason for not being able to pull in JSR 305 or some equivalent? Just curious. |
Historically, RxJava is kept to a minimum dependency: 0 in v1 and 1 in v2 |
I agree @dlew - in a larger code base, it's pretty easy to run into issues like this and there's no way to catch them until runtime. If it's annotated, a handful of static analysis tools and IDEs can leverage this at compile time (even if it's a custom annotation within the library). |
I have created a pull request with some changes: |
* add NotNull annotations add assertion to help static code analysis * avoid false positive * add annotations and assert statement to help static code analysis * remove redundant check * mark parameter as nullable * add test to reproduce npe * add null check for avoid npe * parameter time unit marked as @nonnull * add annotations and assert to help static code analysis * remove assert statements * add missing annotation * add comment for test case
I recently had an issue where a null List was accidentally passed into @akarnokd @dlew I believe it would be worth it add nullability annotations every where This would be a massive change and it will definitely break compilation in some Kotlin code bases, mine included. |
Recent IntelliJ infers most nullability annotations for me in Java: and in Kotlin but it doesn't post a warning (see Kotlin): |
Thanks for the quick reply. I haven't been able to get IntelliJ to infer nullability annotations. I've put together a quick example of my problem. The post you linked seems to address the problem of consuming Java types in Kotlin, rather than here where Kotlin types are consumed by Java. RxJava's I apologize if this level of Kotlin support is outside of the scope of this project. |
I thought writing in Kotlin requires one to explicitly forfeit non-nullness. Have you been using this particular nullable
We could add the |
The project was recently converted from RxJava 1.x -> 2.x and from Java to Kotlin in a short time span and this bug went unnoticed. As far as I can tell,
It's not necessarily the case if Java types are correctly annotated. |
@akarnokd Is there any reason why return type of |
Nulls that can't end up in a sequence are allowed, for example, in |
What do you think about creating annotation |
Java 6 doesn't allow annotating type arguments that way. You'd need to break binary compatibility to introduce all combinations of possible nullness to the interfaces and use sites. |
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.
The text was updated successfully, but these errors were encountered: