问题
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?
回答1:
Due to GitHub issue
Lombok
@Builderis 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.
回答2:
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.
回答3:
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.
来源:https://stackoverflow.com/questions/47573432/unsupportedoperationexception-is-thrown-with-lombok-builder-annotation