I would like to generate package-scope
builder using Lombok , but I'm not sure if it's possible (I didn't find any clues in documentation ).
By default Lombok generates public
builder, ie this code:
@Builder
class User {
private final String name;
}
Is translated into this:
class User {
private final String name;
User(final String name) {
this.name = name;
}
public static User.UserBuilder builder() { // <-- how to make it package-private?
return new User.UserBuilder();
}
public static class UserBuilder { // <-- how to make it package-private?
private String name;
UserBuilder() {
}
public User.UserBuilder name(final String name) {
this.name = name;
return this;
}
public User build() {
return new User(this.name);
}
public String toString() {
return "User.UserBuilder(name=" + this.name + ")";
}
}
}
Is there any way to generate builder class without this leading public
keyword?
Check out the below in the @Builder documentation :
@Builder(access = AccessLevel.PACKAGE) is legal (and will generate the builder class, the builder method, etc with the indicated access level) starting with lombok v1.18.8
And if you see the source code of Builder here , you'll see that by default, the access level of a @Builder
would be lombok.AccessLevel.PUBLIC
, but can be made package-private with @Builder(access = AccessLevel.PACKAGE)
.
Also FYI, the following access levels are supported by @Builder
: PUBLIC, MODULE, PROTECTED, PACKAGE, PRIVATE
. This is via the AccessLevel
enum's source code here .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.