Skip to content
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

[FEATURE] Ability to suppress run-time null checks of @Getter and @Setter #2078

Closed
nlwillia opened this issue Mar 21, 2019 · 5 comments
Closed

Comments

@nlwillia
Copy link

The changelog for v1.18.4 contains a surprising change to the behavior of @Getter and @Setter when a nullability annotation is used.

Lombok has always had its own @NonNull annotation which injected runtime null checking, but you didn't have to use it. If you wanted to use a documentation annotation like JetBrains @NotNull, you could. Lombok would copy it to the getters and setters, but it wouldn't inject any runtime checks.

As of 1.18.4, there doesn't appear to be any way of avoiding runtime checks. I generally have IntelliJ IDEA configured to compile runtime null checks for development but omit them from server builds. Lombok's interjection here is redundant with what my IDE is already doing and unwanted on the server.

I tried lombok.nonNull.flagUsage values of warning and allow, but neither kept the runtime check from being added. (I'm not using "NonNull", I'm using JetBrains "NotNull".) The new lombok.copyableAnnotations property seems to be add-only. I tried = [] and it didn't change anything.

In a large code base where nullability annotations are already being used but only for documentation purposes, Lombok's new behavior poses an unwanted risk. It's now possible that a usage pattern that worked (even if it was ill-advised) could start to actively fail. There should be a way to configure the previous behavior.

In other words, I want to configure public @NotNull @Getter @Setter String foo; (or the @Data versions of the same) to produce only the following:

public @NotNull String getFoo() {
    return foo;
}
public void setFoo(@NotNull String foo) {
    this.foo = foo;
}
@rspilker
Copy link
Collaborator

rspilker commented Mar 25, 2019

Short term work-around: You can create your own annotation, and teach IntelliJ to treat is like a NotNull. Since lombok does not know your annotation, we will not generate a null-check.

We are considering a long term solution.

The list of built-in non-null annotations is not modifiable from the configuration system. They are hard-coded.

Regarding the syntax of the configuration system, the way to make remove individual entries from a list is:

lombok.copyableAnnotations -= my.Annotation

To remove all of them use:

clear lombok.copyableAnnotations

@rspilker
Copy link
Collaborator

I think we now have a solution for you.

In lombok.config, you can now configure lombok.nonNull.exceptionType = Assertion.

If you do so, you will get assert foo != null : "foo is marked non-null but is null".

Assuming that you will not run the application with -ea, this means you will not get any errors in production.

@nlwillia
Copy link
Author

That sounds like a reasonable compromise. Thanks.

@rspilker
Copy link
Collaborator

Thanks for the feedback.

@nlwillia
Copy link
Author

Confirmed working in the edge release.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants