Use custom setter in Lombok's builder

后端 未结 4 1072
暖寄归人
暖寄归人 2021-01-01 08:42

I have a custom setter in my Lombok-based POJO:

@Data
@Builder
public class User {
    private static final PasswordEncoder ENCODER = new BCryptPasswordEncod         


        
4条回答
  •  春和景丽
    2021-01-01 09:14

    Not really an answer to the question, but an edge case that made me to spend quite some time to find the issue.
    If you have Builder with custom setter on a field with Builder.Default value, the name of the field in the generated builder class is not the same as the original field. In fact you would have two fields instead of one: password$value and password$set.
    Nevertheless you can use them in the custom setter in this way:

    public UserBuilder password(String password) {
      this.password$value = ENCODER.encode(password);
      this.password$set = true;
      return this;
    }
    

    The tricky part is that if you use the origianl field name, Intellij IDEA doesn't warn you about that (as if everything is fine, but of course it won't compile). Already submitted a bug report to the plugin.

提交回复
热议问题