为什么 Lombok Builder 不能改变现有对象?

Why doesn't Lombok Builder work with mutating an existing object?

为什么 Lombok Builder 不能改变现有对象?

如果我想创建一个部分构建的对象,然后用剩余字段更新该对象怎么办?

请注意,这不起作用。预编译器不喜欢我正在尝试做的事情。

提供的代码:

private static final ObjectMapper objectMapper = new ObjectMapper();

{
    objectMapper.registerModule(new JavaTimeModule());
    objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
}
@Test
public void testBuilderMutating() throws JsonProcessingException {
    Book unfinishedBook = Book.builder()
            .isbn(String.valueOf(UUID.randomUUID()))
            .title("Title")
            .genre("Science Fiction")
            .author("me")
            .published(LocalDateTime.now())
            .description("description ...")
            .build();
    Book completedBook = rebuildBook(unfinishedBook)
            .sectionCount(4)
            .sectionNames(List.of("A", "B", "C"))
            .chapterCount(16)
            .chapterNames(List.of("1", "2", "3"))
            .build();
    log.info(objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(completedBook));
}

public Book.BookBuilder rebuildBook(Book unfinished) {
    return Book.builder()
            .isbn(unfinished.getIsbn())
            .title(unfinished.getTitle())
            .genre(unfinished.getGenre())
            .author(unfinished.getAuthor())
            .published(unfinished.getPublished())
            .description(unfinished.getDescription())
            .sectionCount(unfinished.getSectionCount())
            .sectionNames(unfinished.getSectionNames())
            .chapterCount(unfinished.getChapterCount())
            .chapterNames(unfinished.getChapterNames());
}

POJO 是:

@ToString
@Data
@NoArgsConstructor
@AllArgsConstructor
@SuperBuilder
public class Chapters extends Sections {
    private int chapterCount;
    private List<String> chapterNames;
}

@ToString
@Data
@NoArgsConstructor
@AllArgsConstructor
@SuperBuilder
public class Sections {
    private int sectionCount;
    private List<String> sectionNames;
}

@ToString
@Data
@AllArgsConstructor
@SuperBuilder
public class Book extends Chapters {
    private String isbn;
    private String title;
    private String genre;
    private String author;
    private LocalDateTime published;
    private String description;
}

发生这种情况是因为 @SuperBuilder 定义为 BookBuilder<?, ?>,如果使用原始类型 Book.BookBuilder,java 无法推断类型。要修复它,请将定义更改为

public Book.BookBuilder<?, ?> rebuildBook(Book unfinished) {
   ...

此外,要正确实施 toStringequals,您需要使用 callSuper = true.

此外,@NoArgsConstructor@AllArgsConstructor@SuperBuilder@Data 看起来有点太多了。有一种方法可以使用 Lombok 定义不可变的 POJO,并能够通过应用 @Jacksonized 注释使用构建器反序列化。

@Getter
@ToString(callSuper = true)
@EqualsAndHashCode(callSuper = true)
@Jacksonized
@SuperBuilder
public class Chapters extends Sections {
    private int chapterCount;
    private List<String> chapterNames;
}

@Getter
@ToString
@EqualsAndHashCode
@Jacksonized
@SuperBuilder
public class Sections {
    private int sectionCount;
    private List<String> sectionNames;
}

@Getter
@ToString(callSuper = true)
@EqualsAndHashCode(callSuper = true)
@Jacksonized
@SuperBuilder
public class Book extends Chapters {
    private String isbn;
    private String title;
    private String genre;
    private String author;
    private LocalDateTime published;
    private String description;
}