在我的项目中,我使用lombok来避免为一个类编写getter和setter。 另外,我使用lombok.Builder来构建一个对象,而不是编写新的Obeject()然后设置所有的值。
但是当我们有继承关系时,当我们想使用lombok builder构建子对象时,我没有得到父对象的字段。
例如:
@Data
@NoArgsConstructor
@AllArgsConstructor
@ToString
@EqualsAndHashCode
public class Parent{
private String nationality;
.
.
// more columns
}
Child课程将是这样的:
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@ToString(callSuper = true)
@EqualsAndHashCode(callSuper = true)
public class Child extends Parent{
private String firstName;
private String lastName;
.
.
}
在我的测试类中,我需要构建子对象
public class Test{
public void testMethod(){
Child child = Child.builder()
.firstName("Rakesh")
.lastName("SS")
.nationality("some text")// I am not able to set nationality
.build();
}
}
请告诉我,有没有办法在lombok中处理这种情况。
答案 0 :(得分:19)
@Builder
无法确定您希望公开的Parent
字段。
当@Builder
放在某个类上时,只有在该类上显式声明的字段才会添加到*Builder
。
当@Builder
置于静态方法或构造函数上时,生成的*Builder
将为每个参数提供一个方法。
此外,如果您使用的是@Builder
,那么可以安全地假设至少Child
是不可变的吗?
我提供了两个示例,一个示例Parent
可变,Child
是不可变的,Parent
和Child
都是不可变的。
import static org.junit.Assert.*;
import lombok.Builder;
import lombok.EqualsAndHashCode;
import lombok.ToString;
import lombok.Value;
import lombok.experimental.NonFinal;
import org.junit.Test;
public class So32989562ValueTest {
@Value
@NonFinal
public static class Parent {
protected final String nationality;
}
@Value
@ToString(callSuper = true)
@EqualsAndHashCode(callSuper = true)
public static class Child extends Parent {
private final String firstName;
private final String lastName;
@Builder(toBuilder = true)
private Child(String nationality, String firstName, String lastName) {
super(nationality);
this.firstName = firstName;
this.lastName = lastName;
}
}
@Test
public void testChildBuilder() {
String expectedFirstName = "Jeff";
String expectedLastName = "Maxwell";
String expectedNationality = "USA";
Child result = Child.builder()
.firstName(expectedFirstName)
.lastName(expectedLastName)
.nationality(expectedNationality)
.build();
assertEquals(result.toString(), expectedFirstName, result.getFirstName());
assertEquals(result.toString(), expectedLastName, result.getLastName());
assertEquals(result.toString(), expectedNationality, result.getNationality());
}
}
import static org.junit.Assert.*;
import lombok.Builder;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.ToString;
import lombok.Value;
import org.junit.Test;
public class So32989562DataTest {
@Data
public static class Parent {
protected String nationality;
}
@Value
@ToString(callSuper = true)
@EqualsAndHashCode(callSuper = true)
public static class Child extends Parent {
private final String firstName;
private final String lastName;
@Builder(toBuilder = true)
private Child(String nationality, String firstName, String lastName) {
this.setNationality(nationality);
this.firstName = firstName;
this.lastName = lastName;
}
}
@Test
public void testChildBuilder() {
String expectedFirstName = "Jeff";
String expectedLastName = "Maxwell";
String expectedNationality = "USA";
Child result = Child.builder()
.firstName(expectedFirstName)
.lastName(expectedLastName)
.nationality(expectedNationality)
.build();
assertEquals(result.toString(), expectedFirstName, result.getFirstName());
assertEquals(result.toString(), expectedLastName, result.getLastName());
assertEquals(result.toString(), expectedNationality, result.getNationality());
}
}
答案 1 :(得分:6)
上述解决方案有效,但是需要太多解决方法。而且,子类和父类的任何更改都需要在各处更改构造函数参数。
Lombok引入了实验性功能,其版本为1.18.2,用于解决Builder注释所面临的继承问题,并且可以使用@SuperBuilder注释进行解决,如下所示。
@SuperBuilder
public class ParentClass {
private final String a;
private final String b;
}
@SuperBuilder
public class ChildClass extends ParentClass{
private final String c;
}
现在,可以使用如下所示的Builder类(使用@Builder注释是不可能的)
ChildClass.builder().a("testA").b("testB").c("testC").build();