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
Enforce @NonNull
on method return and locals
#907
Comments
So, you want lombok to silently inject: if (result != null) throw new NullPointerException("Erm, what do we put here then?");? I'm not sure that's particularly useful; presumably your method is going to blow up all by itself soon. Well, it might not. Still, there's a pretty big difference between Your IDE should just generate the warnings; the right move is NOT to generate an actual at-runtime check. Runtime checks are of dubious value here; you really want static code analysis. We have 3 options here: (A) Disallow the use of C is impossible. A is backwards incompatible. B is the choice we're going with. |
Yes, but what about cases where I get a property from a bean and just want to validate it's non-nullness? I don't like See this gist for an example test which I would like to pass. |
One approach I've used before on similar situations is to have a small utility method to check my post-conditionss/ constraints. Something like ...
Where the helper method Personally, if the error was in the parameters that is when I expect an: I find these most useful in Spock specifications and JUnit tests. |
Despite this issue being dead for about 5 years, I would ask to reassert it's validity. If not by default, at least a configuration to assert objects to not be null on return would be very helpful. Usually, if a method has only one return statements, it is relatively easy to throw on
This is in fact the best possible argument for As an example, if I have a wrapper method which passes one of many incoming parameters to an underlying API, and that value (while not being null) is faulty for some reason, and the API returns It is a lengthy discussion on programming paradigms when and where public @NonNull String exampleString() {
return null;
} becoming public String exampleString() {
return Lombok.nonNull(null);
} with a Lombok.nonNull looking akin to this: public static <T> T nonNull(T value) {
if (value == null) {
throw new NullPointerException...
}
return value;
} |
@TreffnonX I'm afraid, This feature would surely require a specific configuration. A fool-proof setting would be to use Concerning the static analysis which would make this superfluous - somehow I still don't get it running smoothly. |
Ah, I was falsely assuming that Lombok preferrably used it's own utility methods for things like this, but
TBH. I would rather have my production throw errors on me than search for data inconsistencies. But I believe that to be dependant upon personal preference, and of course the project's context. |
Null is far more complex than this; I'm done trying to explain the garbage fire that is nullity annotations and null checks in general on an issue per issue basis because it is taking up tons of time. I regret that I can't get into it more, but these kinds of issues shall remain closed, and any debate on why or how lombok can be of use can be held in person at conferences or if you're anywhere around Delft, The Netherlands, contact us and we'll meet. Make sure you free up an hour or 3 on your calendar, because it is that complex. What's left for me to say is: Oh, I have thought about this. Lots of time spent thinking about it, in fact. It's not this simple, this won't work well. I may be wrong, but going a few rounds on an issue tracker is not an acceptable use of time. |
Sorry, I was very much unaware of the deep implications of my question and apologize for it. It won't Happen again. I might take you up on your offer though, when in the netherlands again. |
As
@lombok.NonNull
(unlike related annotations) really means "never ever null", it should be enforced not only in generated methods and on fields, but everywhere. I find it disappointing to seereturning
null
. Actually, I can't see any reason for placing@NonNull
on methods or locals, other then enforcing the check.I'm afraid that for compatibility reasons (i.e. for people happily returning null from such methods), there need to be a corresponding configuration key.
The text was updated successfully, but these errors were encountered: