问题描述
我有以下@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
会产生{{ 1}}构建器类。这似乎并不从我的输出中暗示出来,即使将参数public
放在access=AccessLevel.PUBLIC
的{{1}}注释中也不会发生。有任何想法吗?此类也是@Builder
的事实有什么问题吗?我没有检测到其他东西?
//编辑:我确定,当将类与我从其调用构建器模式的包移入同一包时,它可以正常工作。这不是一个LiteProduct
问题,而是一个包装可见性问题,该问题基于我正在阅读的内容,应该不存在。
解决方法
问题是我正在使用以下代码行创建LiteProduct
的实例:
return new LiteProduct.builder().build();
代替:
return LiteProduct.builder().build();
这是@Builder
批注允许您执行的操作。显然,builder()
就像Builder
的工厂方法,已经为您调用new
。