繁体   English   中英

Lombok 的 `@Builder` 注释顽固地保留包 - 私有

[英]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 似乎可以很好地检测到它:

IDE 检测内部构建器类

我收到有关其可见性的编译时错误:

Builder 类不公开

我已经查看了一些解决方法,例如thisthis ,它们似乎都暗示我的问题应该已经自动解决,并且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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM