简体   繁体   English

如何确保子类将在 lombok 中调用正确的超级构造函数?

[英]How to ensure that child class will call the correct super constructor in lombok?

Say that I have the following classes说我有以下课程

@AllArgsConstructor
@NoArgsConstructor
@Data
@Builder
public class A {

    private String item1;
    private String item2;
    private String item3;
}

and

@Data
public class B extends A {

    private String item4;

    @Builder(builderMethodName = "bBuilder")
    public B(String item1, String item2, String item3, String item4) {
        super(item1, item2, item3);
        this.item4 = item4;
    }
}

how can I (or is there even a way) to guarantee that the child constructor will call the "correct" constructor when I call the super constructor?当我调用超级构造函数时,我如何(或者甚至有办法)保证子构造函数将调用“正确”的构造函数? More specifically, I want to 100% ensure that the three string values I pass in indeed are set to the correct fields in the parent, and not something where, say, item1 in is set to item2.更具体地说,我想 100% 确保我传入的三个字符串值确实设置为父项中的正确字段,而不是将 item1 in 设置为 item2 的内容。

I know that I could, for example either:我知道我可以,例如:

  • explicitly create my own all args constructor显式创建我自己的所有 args 构造函数
  • in the child constructor, call all of the setters of the parent在子构造函数中,调用父构造函数的所有设置器

but I am just curious if Lombok is smart enough, somehow, to set the the fields in the child class to the correct ones in the parent class?但我只是好奇 Lombok 是否足够聪明,以某种方式将子类中的字段设置为父类中的正确字段?

Edit :编辑

I know that the order of the fields determine the order of the fields of the constructor, but that to me isn't safe enough, since if someone inserts a new field, say in the middle, then it will throw everything off.我知道字段的顺序决定了构造函数的字段的顺序,但这对我来说还不够安全,因为如果有人插入一个新字段,比如在中间,那么它就会把所有东西都扔掉。

However, perhaps the @SuperBuilder might be something that I could use, as some have suggested, if not, then I will just explicit create my own constructor to guarantee the order of fields.然而,也许@SuperBuilder 可能是我可以使用的东西,正如一些人所建议的那样,如果不是,那么我将明确创建我自己的构造函数来保证字段的顺序。

The order of the parameters of an @AllArgsConstructor matches the order of the fields in the source code. @AllArgsConstructor的参数顺序与源代码中的字段顺序匹配。 So right now you are safe.所以现在你是安全的。

However, if you later modify the order of the fields in A (or rename them), you will get wrong assignments (or wrong parameter names in your builder), but no compiler error.但是,如果您稍后修改A字段的顺序(或重命名它们),您将得到错误的赋值(或构建器中的错误参数名称),但不会出现编译器错误。

But there is an easy way out: Use @SuperBuilder , and remove @Data .但是有一个简单的方法:使用@SuperBuilder ,然后删除@Data (Note that you need @SuperBuilder on all classes in your hierarchy.) You won't get @AllArgsConstructor s, changes to the field order are irrelevant, and name changes are immediately reflected in the builder classes. (请注意,您的层次结构中的所有类都需要@SuperBuilder 。)您不会得到@AllArgsConstructor ,字段顺序的更改无关紧要,名称更改会立即反映在构建器类中。

If that is not possible, your only choice is to put a big fat warning comment into A advising later coders not to mess around with the names and order.如果这是不可能的,您唯一的选择是在A一个很大的警告注释,建议以后的编码人员不要弄乱名称和顺序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM