是否有可能将龙目岛的建造者公之于众? [英] Is it possible to make Lombok's builder public?
问题描述
我在项目中使用了Lombok库,并且无法在外部包中使用带有 @Builder
注释的类.
I am using Lombok library in my project and I am not able to use a class annotated with @Builder
in outer packages.
有没有办法让建造者公开?
Is there a way to make the builder public?
MyClass instance = new MyClass.MyClassBuilder().build();
错误是:
'MyClassBuilder()'在以下位置不公开'com.foo.MyClass.MyClassBuilder'.无法访问从外部包装中
'MyClassBuilder()' is not public in 'com.foo.MyClass.MyClassBuilder'. Cannot be accessed from outside package
推荐答案
@Builder
已经产生了公共方法,只是封装私有的构造函数.原因是他们打算让您使用公开的静态 builder()
方法,而不是直接使用构造函数:
@Builder
already produces public methods, it's just the constructor that's package-private. The reason is that they intend for you to use the static builder()
method, which is public, instead of using the constructor directly:
Foo foo = Foo.builder()
.property("hello, world")
.build();
为了向您展示生成的所有内容,我编写了以下类:
Just to show you everything that gets generated, I wrote the following class:
@Builder
public class Foo
{
private final String property;
}
然后我通过 delombok 进行运行,以查看生成的所有内容.如您所见,一切都是公开的:
I then ran it through delombok to see everything that was generated. As you can see, everything is public:
public class Foo
{
private final String property;
@java.beans.ConstructorProperties({"property"})
Foo(final String property) {
this.property = property;
}
public static FooBuilder builder() {
return new FooBuilder();
}
public static class FooBuilder
{
private String property;
FooBuilder() { }
public FooBuilder property(final String property) {
this.property = property;
return this;
}
public Foo build() {
return new Foo(property);
}
public String toString() {
return "Foo.FooBuilder(property=" + this.property + ")";
}
}
}
这篇关于是否有可能将龙目岛的建造者公之于众?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!