Reputation: 5132
I have a POJO class as follows:
@Value
@Builder
public class XYZ {
@NonNull
private final String a;
@NonNull
private final String b;
@NonNull
private final State state;
public enum State {
STARTED
}
}
When I try to create the object of XYZ class from my tst/ folder, it fails with an error that constructor cannot be accessed from outside package but when I used @RequiredArgsConstructor in that POJO, then it starts working fine and doesn't show the error. The actual error is this:
error: XYZ(String,String,State) is not public in XYZ; cannot be accessed from outside package
So, I was wondering what is the difference between @AllArgsConstructor(provided by @Value) and @RequiredArgsConstructor because of which I am seeing this issue.
Upvotes: 0
Views: 2204
Reputation: 8102
When processing its annotations, Lombok does not consider the order of those annotations in the source file. Instead, each annotation type has a priority that determines the processing order. This is because there are dependencies between different annotation types.
@Builder
has a higher priority than @Value
. So Lombok first generates the package-private all-args constructor required for the builder. As a result, there is already a constructor present when processing @Value
. According to the @Value
documentation, "any explicit constructor, no matter the arguments list, implies lombok will not generate a constructor".
However, in my opinion this is a bit misleading in the documentation. The constructor generated for @Builder
is more something like an implementation detail, but I would not call it an "explicit constructor". (By "explicit" the authors probably mean "not a default constructor".)
Once you add @RequiredArgsConstructor
or @AllArgsConstructor
(which is the same in this case), the constructor becomes public, and neither @Builder
nor @Value
has to generate it any more.
Upvotes: 4