[英]Lombok's `@Builder` annotation stubbornly stays package - private
我有以下@Builder
- 带注释的类:
@Data
@Builder(access = AccessLevel.PUBLIC)
@Entity
public class LiteProduct
{
// Minimal information required by our application.
@Id
private Long id; // Unique
private String name;
private Long costInCents;
private String type;
// Model the product types as a Hash Set in case we end up with several
// and need fast retrieval.
public final static Set<String> PRODUCT_TYPES = new HashSet<>
(Arrays.asList("flower", "topical", "vaporizer", "edible", "pet"));
// Have to allow creation of products without args for Entities.
public LiteProduct()
{
}
public LiteProduct(@NonNull final Long id, @NonNull final String name,
@NonNull final String type, @NonNull final Long costInCents)
{
if(!PRODUCT_TYPES.contains(type))
{
throw new InvalidProductTypeException(type);
}
this.name = name;
this.id = id;
this.costInCents = costInCents;
}
每当我想使用Lombok
据称提供给我的构建器类时,尽管 IDE 似乎可以很好地检测到它:
我收到有关其可见性的编译时错误:
我已经查看了一些解决方法,例如this或this ,它们似乎都暗示我的问题应该已经自动解决,并且Lombok
默认生成public
Builder 类。 这似乎不是从我的输出中暗示的,即使我将参数access=AccessLevel.PUBLIC
放在我的@Builder
注释中LiteProduct
之后也不会发生。 有任何想法吗? 这个类也是@Entity
的事实有什么问题吗? 我没有检测到的其他东西?
// 编辑:我确定当我将类移动到与我从中调用构建器模式的包相同的包中时,它工作得很好。 这不是@Entity
问题,而是基于我正在阅读的内容的包可见性问题不应该存在。
问题是我使用以下代码行来创建LiteProduct
的实例:
return new LiteProduct.builder().build();
代替:
return LiteProduct.builder().build();
这是@Builder
批注允许您执行的操作。 显然builder()
就像Builder
的工厂方法,它已经为你调用了new
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.