Lombok的`@Builder`注解顽固地保持为包私有。

huangapple go评论77阅读模式
英文:

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

问题

我有以下被 @Builder 注解的类:

@Data
@Builder(access = AccessLevel.PUBLIC)
@Entity
public class LiteProduct
{
	// 应用程序所需的最小信息。
	@Id
	private Long id;	// 唯一标识
	private String name;
	private Long costInCents;
	private String type;

	// 将产品类型建模为哈希集,以防我们最终有多个类型并且需要快速检索。
	public final static Set<String> PRODUCT_TYPES = new HashSet<>(
		Arrays.asList("flower", "topical", "vaporizer", "edible", "pet"));

	// 必须允许创建没有参数的产品以用于实体。
	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 似乎可以正常检测到它:

Lombok的`@Builder`注解顽固地保持为包私有。

我会收到关于其可见性的编译时错误:

Lombok的`@Builder`注解顽固地保持为包私有。

我已经尝试了一些解决方法,例如 这个 或者 这个,它们似乎都暗示我的问题应该已经自动解决了,Lombok 默认生成 public 的构建器类。但是从我的输出中并没有暗示这一点,在我将参数 access=AccessLevel.PUBLIC 放入 LiteProduct@Builder 注解中之后,问题仍然存在。有什么想法吗?这个类也是一个 @Entity,这是否有问题?还有其他我没有发现的问题吗?

// 编辑:我确定当我将这个类移到与我从中调用构建器模式的那个类相同的包中时,它能够正常工作。这不是一个 @Entity 的问题,而是一个包可见性的问题,根据我正在阅读的内容,这个问题不应该存在。

英文:

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&lt;String&gt; PRODUCT_TYPES = new HashSet&lt;&gt;
			 (Arrays.asList(&quot;flower&quot;, &quot;topical&quot;, &quot;vaporizer&quot;, &quot;edible&quot;, &quot;pet&quot;));

	// 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的`@Builder`注解顽固地保持为包私有。

I get a compile-time error about its visibility:

Lombok的`@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.

答案1

得分: 6

问题是我使用了以下代码行来创建 LiteProduct 的实例:

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

而不是:

return LiteProduct.builder().build();

这正是 @Builder 注解允许您执行的操作。显然,builder() 就像是为已经为您调用了 newBuilder 类型创建的工厂方法。

英文:

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.

huangapple
  • 本文由 发表于 2020年10月16日 09:38:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/64381811.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定