Search code examples
javalistruntimeexceptionlombokbuilder-pattern

UnsupportedOperationException is thrown with Lombok Builder annotation


I am using Lombok for my project. My model looks like:

@Builder
@Data @AllArgsConstructor
public class ScreenDefinitionDTO {
    @Singular
    private List<ScreenDeclaration> screens;
}

I want to do next operation:

String screenName = ctx.screenName().getText();
ScreenDeclaration declaration = ParsingUtils
                .buildScreenDeclaration(StringUtils.trim(screenName));

Where instance is created:

public static ScreenDefinitionDTO buildEmptyScreenDTO() {
    return ScreenDefinitionDTO.builder()
            .screens(new ArrayList<>())
            .build();
}

Finally, I got:

Exception in thread "main" java.lang.UnsupportedOperationException
    at java.util.AbstractList.add(AbstractList.java:148)
    at java.util.AbstractList.add(AbstractList.java:108)

When I changed creating the instance without Lombok builder pattern everything is fine:

public static ScreenDefinitionDTO buildEmptyScreenDTO() {
    return new ScreenDefinitionDTO(new ArrayList<>());
}

I couldn't understand what is wrong with Lombok's builder pattern?


Solution

  • Due to GitHub issue

    Lombok @Builder is primarily meant for immutables (and uses either Collections.unmodifiableList or Guava's ImmutableList

    that's why you have UnsupportedOperationException

    For greater certainty reproduce full code pattern where you have exception please.