簡體   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