简体   繁体   English

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

[英]Lombok's `@Builder` annotation stubbornly stays package - private

I have the following @Builder - annotated class:我有以下@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;
    }

Whenever I want to use the builder class that Lombok is purported to give me, despite the fact that the IDE seems to detect it just fine:每当我想使用Lombok据称提供给我的构建器类时,尽管 IDE 似乎可以很好地检测到它:

IDE 检测内部构建器类

I get a compile-time error about its visibility:我收到有关其可见性的编译时错误:

Builder 类不公开

I have looked at some workarounds such as this or this , and they all seem to imply that my problem ought to already be solved automatically and that Lombok by default produces public Builder classes.我已经查看了一些解决方法,例如thisthis ,它们似乎都暗示我的问题应该已经自动解决,并且Lombok默认生成public Builder 类。 This does not seem to be implied from my output, and does not happen even after I put the parameter access=AccessLevel.PUBLIC in my @Builder annotation in LiteProduct .这似乎不是从我的输出中暗示的,即使我将参数access=AccessLevel.PUBLIC放在我的@Builder注释中LiteProduct之后也不会发生。 Any ideas?有任何想法吗? Is there something wrong with the fact that this class is also an @Entity ?这个类也是@Entity的事实有什么问题吗? Something else I'm not detecting?我没有检测到的其他东西?

// Edit: I determined that when I move the class in the same package as the one I am calling the builder pattern from, it works just fine. // 编辑:我确定当我将类移动到与我从中调用构建器模式的包相同的包中时,它工作得很好。 This is not an @Entity issue, but a package visibility issue which based on what I'm reading should not be there.不是@Entity问题,而是基于我正在阅读的内容的包可见性问题应该存在。

The problem was that I was using the following line of code to create an instance of LiteProduct :问题是我使用以下代码行来创建LiteProduct的实例:

return new LiteProduct.builder().build();

instead of:代替:

return LiteProduct.builder().build();

which is what the @Builder annotation allows you to do.这是@Builder批注允许您执行的操作。 Clearly builder() is like a factory method for Builder s that already calls new for you.显然builder()就像Builder的工厂方法,它已经为你调用了new

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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