The problem is when I mark a primitive field with @lombok.NonNull annotation, then build task produces this warning:
model/User.java:24: warning: @NonNull is meaningless on a primitive.
@RequiredArgsConstructor(staticName = "of")
^
But when I remove this annotation, build fails because of:
UserFormControllerTest.java:268: error: method of in class User cannot be applied to given types;
User user = User.of(firstName, lastName, 0);
^
required: String,String
found: String,String,int
reason: actual and formal argument lists differ in length
How can one indicate, that he want to use primitive type field in RequiredArgsConstructor without such warnings?
@rzwitserloot, maybe you could create something like @lombok.RequiredArg to indicate that this field must be included in RequiredArgsConstructor? This would be much more clear than NonNull
@eximius313 Can't you make the field final? Otherwise, it's actually not required.
Can't you use @AllArgsConstructor?
Otherwise, your proposal is a hack pretending a field is required. But maybe we can say that while it's not required by java semantics, it's required by you.
Anyway, I like it. It could be used both for primitives and for nullable fields.
Real-life Entities (let's take User as an example):
1) have lot of fields, so @AllArgsConstructor is an overkill
2) have fields, that are required during entity creation (email, password) and fields that are optional (pet). This is why we use factoryMethods, right?
3) being required field doesn't implicate being final! User can change his password (not final) although it is required (used in @RequiredArgsConstructor).
4) @NonNull has different semantics and @RequiredArg could be used on primitive types without warning
This isn't a bug; if something is meaningless, just... don't.
The real issue here is that you're abusing @NonNull just to make the field be included in the required args list. The solution to this problem is for lombok to have a way to mark fields as required, as @kksoft suggests in the original report, or even more generally, for lombok to have a mechanism to generate a constructor with whatever fields you selected, based either on a stringly typed list (of/exclude style: @SomeArgsConstructor(of = {"field1", "field2"}), or on annotations. These are all much more complicated than they sound and we revisit the topic from time to time without coming up with something that's obviously the right answer).
Please file a different enhancement request for that one. Thanks :)
Is there any fix to this now a days?
@RequiredArgs would be an amazing addition.
Is there already a work around or fix?
Just a naming idea: What about @ChosenArgumentConstructor? By default it'd include all the fields, the @RAC does, but there'd be *.Include and *.Exclude annotations to change this. My reason for this is that I sometimes could use both these constructors.
Simple solution: use a wrapper-class (rather than primitives).
Most helpful comment
@rzwitserloot, maybe you could create something like
@lombok.RequiredArgto indicate that this field must be included in RequiredArgsConstructor? This would be much more clear thanNonNull