[英]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 似乎可以很好地檢測到它:
我收到有關其可見性的編譯時錯誤:
我已經查看了一些解決方法,例如this或this ,它們似乎都暗示我的問題應該已經自動解決,並且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.