[英]Override lombok builder and change value type
I have the below class with Lombok @Builder
where I override one builder method to convert the input string list into an enum list (field barList
):我有下面的 class 和 Lombok
@Builder
在其中我覆盖了一个构建器方法以将输入字符串列表转换为枚举列表(字段barList
):
(I still use the default method for the other field number
) (其他字段
number
我还是用默认方法)
import java.util.List;
import java.util.stream.Collectors;
import lombok.Builder;
import lombok.Getter;
@Getter
@Builder(toBuilder = true)
public class Foo {
private List<BarEnum> barList;
private int number;
public static class FooBuilder {
private List<BarEnum> barList;
public FooBuilder barList(List<String> barStringList) {
this.barList = barStringList.stream()
.map(barString -> BarEnum.valueOf(barString))
.collect(Collectors.toList());
return this;
}
}
}
When compiling, I get this error on the line @Builder(toBuilder = true)
above:编译时,我在上面的行
@Builder(toBuilder = true)
上收到此错误:
incompatible types: java.util.List<com.mypackage.BarEnum> cannot be converted to java.util.List<java.lang.String>
不兼容的类型:java.util.List<com.mypackage.BarEnum> 无法转换为 java.util.List<java.lang.String>
I couldn't find any answers where I can override the default builder method and change a value type.我找不到任何可以覆盖默认构建器方法并更改值类型的答案。 Is it possible to do this?
是否有可能做到这一点?
First we run delombok to get a better idea of what's happening, and then compile the output.首先我们运行 delombok 以更好地了解发生了什么,然后编译 output。
java -jar lombok.jar delombok -p Foo.java >Foo2.java
# Rename Foo2.java or make the `class Foo` declaration non-public
javac Foo2.java
gets us:得到我们:
Foo2.java:63: error: incompatible types: List<BarEnum> cannot be converted to List<String>
return new Foo.FooBuilder().barList(this.barList).number(this.number);
^
And then the answer is clear: Lombok needs the barList(List<BarEnum>)
variant of the builder barList method to exist.然后答案很明确:Lombok 需要存在构建器 barList 方法的
barList(List<BarEnum>)
变体。 By writing it yourself, lombok doesn't generate its own version.通过自己编写,lombok 不会生成自己的版本。
Lombok core developer speaking here: We don't, because it is quite difficult to ascertain if there is a signature clash. Lombok 核心开发人员在此发言:我们没有,因为很难确定是否存在签名冲突。 Thus, we notice a method with the same name as something we want to generate and with the same argument count, and we don't do any further analysis: That's a 'match' and means lombok doesn't generate this method anymore, assuming that you've taken over the responsibility for it.
因此,我们注意到一个方法与我们想要生成的东西具有相同的名称和相同的参数计数,我们不做任何进一步的分析:这是一个“匹配”,意味着 lombok 不再生成这个方法,假设你已经接管了它的责任。 This is a fine, fine example: There cannot be a
barList(List<BarEnum>)
method in addition to your explicit barList(List<String>)
method because you can't have 2 methods that differ solely in the generics parts of the arguments.这是一个很好的例子:除了你的显式
barList(List<String>)
方法之外,不能有barList(List<BarEnum>)
方法,因为你不能有 2 个仅在 generics 部分不同的方法arguments。 (Try it - won't compile). (试试看 - 不会编译)。
Thus, your approach here is fundamentally untenable.因此,您在这里的方法基本上是站不住脚的。 The fix is fairly easy - make that custom method something like:
修复相当简单 - 使自定义方法类似于:
public FooBuilder barStringList(List<String> barStringList) { .. }
ie give it another name.即给它另一个名字。 Now lombok will also generate
barList(List<BarEnum>)
and all will be well.现在 lombok 也会生成
barList(List<BarEnum>)
,一切都会好起来的。
If you want the barlist(List<BarEnum>)
variant to not exist, then don't, but you'll have to handwrite the toBuilder()
part of it: At that point it is no longer possible for lombok to automatically know how to turn an instance into a pre-filled builder anymore, if you start just effectively removing the very methods that are needed as 'setters' to make that work.如果您希望
barlist(List<BarEnum>)
变体不存在,则不要,但您必须手写它的toBuilder()
部分:此时,lombok 不再可能自动知道如何如果您开始有效地删除作为“设置器”所需的方法以使该工作正常工作,则可以将实例转变成预填充的构建器。
DISCLAIMER: I'm a core lombok dev.免责声明:我是核心 lombok 开发人员。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.