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

1.2k views Asked by At

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 detects inner builder class

I get a compile-time error about its visibility:

Builder class not publically available

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.

1

There are 1 answers

0
Jason On BEST ANSWER

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 Builders that already calls new for you.