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
NonNull generating incorrect null check in constructor #1018
Comments
I'm experiencing this issue as well. It's also possibly related to #869. I've found a small test case to trigger the problem; a single class import lombok.*;
import lombok.experimental.Accessors;
@Accessors(prefix = "m")
@AllArgsConstructor
public class Issue {
@NonNull
String mName;
} Compiled with lombok snapshot (from today) with command: Disassembling the class file, I can see the constructor null checks the field, instead of the parameter: public class Issue {
@NonNull
String mName;
@ConstructorProperties({"name"})
public Issue(@NonNull String var1) {
if(this.mName == null) {
throw new NullPointerException("mName");
} else {
this.mName = var1;
}
}
} Removing either of the |
What is the status of this bug |
Generates the null check on the constructor parameter instead of the instance field. Fix for issues projectlombok#869 and projectlombok#1018.
…mbok#869 and projectlombok#1018. Added name to AUTHORS file. Added ECJ fix and test. Made one of the variables final to also verify the final error case.
I'm quite new to Lombok, so forgive me if I'm doing something wrong.
It seems the emitted code is checking the value of the object field before setting the field with the value of the parameter.
Notice the difference between the constructors' parameter names versus the setter parameter name. In the setter, the parameter is named _id, shadowing the field, whereas in the constructor, it is named id without the underscore, yet the null check in both cases checks _id.
my lombok config includes:
lombok.accessors.prefix+=_
I have a test case:
@DaTa
@requiredargsconstructor
@AllArgsConstructor
public class TestBok {
}
Output is:
java.lang.NullPointerException: _id
at gov.noaa.pmel.tsunami.model.TestBok.(TestBok.java:10)
at gov.noaa.pmel.tsunami.model.TestBok.main(TestBok.java:23)
Delomboked (partial) code:
import lombok.NonNull;
public class TestBok {
@nonnull
private Integer _id;
private String _foo;
[...]
@java.beans.ConstructorProperties({"id"})
@java.lang.SuppressWarnings("all")
@javax.annotation.Generated("lombok")
public TestBok(@nonnull final Integer id) {
if (_id == null) {
throw new java.lang.NullPointerException("_id");
}
this._id = id;
}
}
The text was updated successfully, but these errors were encountered: