-
Notifications
You must be signed in to change notification settings - Fork 285
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
Is there a way to reverse the default assumption about nullability? #94
Comments
I don't think it's supported yet, but you can annotate packages with Checker Framework's |
Hi @jonnermut! We don't support this reversal at the moment, though with some work we could probably do it in the future. For the fields, are they in fact nullable and your code needs to have defensive null checks everywhere? Or is it that they are not null after initialization, but NullAway doesn't understand the initialization pattern? If it's the latter, and if there is some annotation on the fields or classes to indicate they are getting initialized in a special way, we could probably add support for that pretty easily. Parameters are a different story, and probably require something like flipping the default. For adopting Error Prone gradually, you can try to use the new |
Thanks @JakeWharton , I don't thinks its supported yet. Thanks @msridhar . Most of really nullable fields are in model objects which model JSON (where they are almost all nullable), or Hibernate database entities (where they are a mix). In both of these cases every field is private with a public getter/setter pair which would also need annotation. In both cases the fields have to start null and have empty constructors, and they may or may not be initialised inside the frameworks (Hibernate & Jackson). Parameters are also a mix. But the vast majority of our methods are written defensively with null guards (due to bitter experience of NPEs!), so it would be useful to set the reverse default there for sure. Lately the team has been writing a lot of Swift, Kotlin & (recent versions of) Typescript, and the non null enforcing in those languages really shows up how primitive the null checking is in Java (and our C++ code for that matter...). So I'm keen to make this happen. Do you think that the code is encapsulated enough that changing the logic in
|
@jonnermut feel free to give it a shot! I think the key issue is how NullAway should figure out whether a class has opted in to the default nullable treatment. I think we'll need to add support for something like FYI, I probably won't be able to look too much at this until after the holidays. |
Could this be solved by passing a custom annotation to the To extend upon this, I was hoping to find a way to completely exclude a field marked with this custom annotation from any NullAway analysis, and not just initialization. I have a field that is both late initialized, and is nulled out after being used in many different functions. I would ideally like for it to not be nullable, but in the meantime it would be nice to not have to add a |
After discussing the extended use case above with a co-worker, we decided that wrapping the value in |
To help solve the original problem in this thread, and to even match Kotlin, a pre-baked |
Hi @caseykulm, I won't be able to look more until next week, but could you open a separate issue in the |
Couldn't find this anywhere in the doco, so I'm assuming no... but thought I'd ask the question. I have a couple of quite large code bases that pretty much assume all fields and parameters are nullable. Eg they have null checks already for everything. There have lots of hibernate and json model objects while have to have empty constructors and null initialised fields.
For these existing code bases, it seems it would be a lot less annotation effort to reverse the assumption and assume @nullable by default. Then the checker would pick up non checked uses straight away, and we could gradually add @nonnull annotations.
As it is, its going to be too big a task (currently 5k+ combined NullAway and ErrorProne warnings on one of the code bases) to get this happening.
The text was updated successfully, but these errors were encountered: