catch32
catch32

Reputation: 18582

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?

Upvotes: 14

Views: 5725

Answers (3)

Missael De Nadai
Missael De Nadai

Reputation: 1054

Try this:

@Builder
@Data @AllArgsConstructor
public class ScreenDefinitionDTO {
    @Builder.Default
    private List<ScreenDeclaration> screens = new ArrayList<>();
}

This way you are telling lombok to, on build, initialize screens with an empty ArrayList.

Upvotes: 8

yanefedor
yanefedor

Reputation: 2262

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.

Upvotes: 12

Junior Dussouillez
Junior Dussouillez

Reputation: 2397

As said by @fbokovikov the @Builder annotation uses immutables so when you try to add an element in the list an exception is thrown.

dto.getScreens().add(new ScreenDeclaration()) // throws java.lang.UnsupportedOperationException

If you set a breakpoint to see the value returned by dto.getScreens() you can see its type is Collections$EmptyList. If you use the constructor of the DTO then the type is ArrayList and the exception is not thrown.

Upvotes: 0

Related Questions