我有一个类,我想使用Lombok.Builder,我需要预处理一些参数.像这样的东西:
@Builder
public class Foo {
public String val1;
public int val2;
public List<String> listValues;
public void init(){
// do some checks with the values.
}
}
Run Code Online (Sandbox Code Playgroud)
通常我会调用init()NoArg构造函数,但是使用生成的构建器我无法这样做.有没有办法让init生成的构建器调用它?例如,build()会生成如下代码:
public Foo build() {
Foo foo = Foo(params....)
foo.init();
return foo;
}
Run Code Online (Sandbox Code Playgroud)
我知道我可以手动编码all args构造函数,Builder会通过它调用它,我可以init在那里调用.
但这是次优解决方案,因为我的类可能偶尔会添加新字段,这也意味着更改构造函数.
我已经Lombok在我的代码中使用自动生成Getter和Setter编码,现在我想添加其他个人Annotations并使用它.
例如,我想添加@Exist验证现有密钥列表:
@Getter @Setter
public class User {
private String name;
private List<Integer> keys;
public boolean existKeys(Integer key) {
boolean exist = keys.contains(key);
return exist;
}
}
Run Code Online (Sandbox Code Playgroud)
在创建Annotation之后,我将只需执行以下操作:
@Getter @Setter
public class User {
private String name;
@Exist
private List<Integer> keys;
}
Run Code Online (Sandbox Code Playgroud)