简体   繁体   中英

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

I have the following @Builder - annotated class:

@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:

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. 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 . Any ideas? Is there something wrong with the fact that this class is also an @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.

The problem was that I was using the following line of code to create an instance of LiteProduct :

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

instead of:

return LiteProduct.builder().build();

which is what the @Builder annotation allows you to do. Clearly builder() is like a factory method for Builder s that already calls new for you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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