Lombok 构建器包范围
Lombok builder package scope
我想使用 Lombok, but I'm not sure if it's possible (I didn't find any clues in documentation).
生成 package-scope
生成器
Lombok 默认生成 public
构建器,即此代码:
@Builder
class User {
private final String name;
}
翻译成这样:
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 + ")";
}
}
}
没有这个前导 public
关键字,有什么方法可以生成生成器 class 吗?
查看 @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
如果您查看 Builder here 的源代码,您会看到默认情况下 @Builder
的访问级别为 lombok.AccessLevel.PUBLIC
,但可以package-private 与 @Builder(access = AccessLevel.PACKAGE)
.
此外,仅供参考,@Builder
支持以下访问级别:PUBLIC, MODULE, PROTECTED, PACKAGE, PRIVATE
。这是通过 AccessLevel
枚举的源代码 here.
我想使用 Lombok, but I'm not sure if it's possible (I didn't find any clues in documentation).
生成package-scope
生成器
Lombok 默认生成 public
构建器,即此代码:
@Builder
class User {
private final String name;
}
翻译成这样:
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 + ")";
}
}
}
没有这个前导 public
关键字,有什么方法可以生成生成器 class 吗?
查看 @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
如果您查看 Builder here 的源代码,您会看到默认情况下 @Builder
的访问级别为 lombok.AccessLevel.PUBLIC
,但可以package-private 与 @Builder(access = AccessLevel.PACKAGE)
.
此外,仅供参考,@Builder
支持以下访问级别:PUBLIC, MODULE, PROTECTED, PACKAGE, PRIVATE
。这是通过 AccessLevel
枚举的源代码 here.