使用 lombok 为一个字段添加两个不同的设置器
Add two different setters for an field using lombok
我正在使用@Setter生成setter,但我还想为具有不同数据类型的相同字段再添加一个setter作为输入。
@Setter
private Date date;
它将生成:
public void setDate(Date date) {
this.date = date;
}
我还想再添加一个 setter,其中输入的格式为字符串。
public void setDate(String date) {
this.date = Date.valueOf(date);
}
通过使用@Setter 并添加 setDate(String date) setter,lombok 无法自动生成 setDate(Date date) setter。有没有办法我可以使用 Date 作为输入自动生成 setter 并添加一个 setter 明确接受 String 作为输入。
来自doc:
No method is generated if any method already exists with the same name (case insensitive) and same parameter count. For example, getFoo() will not be generated if there's already a method getFoo(String... x) even though it is technically possible to make the method. This caveat exists to prevent confusion. If the generation of a method is skipped for this reason, a warning is emitted instead. Varargs count as 0 to N parameters. You can mark any method with @lombok.experimental.Tolerate
to hide them from lombok.
我的建议是使用类似的东西(或使用实验注释):
@Setter
private Date date;
public void setDateFromString(String date) {
this.date = Date.valueOf(date);
}
我正在使用@Setter生成setter,但我还想为具有不同数据类型的相同字段再添加一个setter作为输入。
@Setter
private Date date;
它将生成:
public void setDate(Date date) {
this.date = date;
}
我还想再添加一个 setter,其中输入的格式为字符串。
public void setDate(String date) {
this.date = Date.valueOf(date);
}
通过使用@Setter 并添加 setDate(String date) setter,lombok 无法自动生成 setDate(Date date) setter。有没有办法我可以使用 Date 作为输入自动生成 setter 并添加一个 setter 明确接受 String 作为输入。
来自doc:
No method is generated if any method already exists with the same name (case insensitive) and same parameter count. For example, getFoo() will not be generated if there's already a method getFoo(String... x) even though it is technically possible to make the method. This caveat exists to prevent confusion. If the generation of a method is skipped for this reason, a warning is emitted instead. Varargs count as 0 to N parameters. You can mark any method with
@lombok.experimental.Tolerate
to hide them from lombok.
我的建议是使用类似的东西(或使用实验注释):
@Setter
private Date date;
public void setDateFromString(String date) {
this.date = Date.valueOf(date);
}