Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

@Builder annotation lose field default value #1165

Closed
gardenias opened this issue Jul 28, 2016 · 3 comments
Closed

@Builder annotation lose field default value #1165

gardenias opened this issue Jul 28, 2016 · 3 comments

Comments

@gardenias
Copy link

public class BuilderAnnotationTest {

    @Test
    public void defalutValue() throws Exception {
        assertThat(Entity.builder().build().isSingle()).isTrue();
    }
}

@Getter
@Builder
class Entity{
    private String name;
    private boolean single = true;
}
@Maaartinus
Copy link
Contributor

Duplicate of #916. There's a workaround

@Getter
@Builder(toBuilder=true)
@AllArgsConstructor
@NoArgsConstructor(access=AccessLevel.PRIVATE)
public class Entity {
    public static EntityBuilder builder() {
        return PROTOTYPE.toBuilder();
    }

    private static final Entity PROTOTYPE = new Entity();

    private String name;
    private boolean single = true;
}

@kishaningithub
Copy link

This also works

public class BuilderAnnotationTest {

    @Test
    public void defalutValue() throws Exception {
        assertThat(new Entity().toBuilder().build().isSingle()).isTrue(); // note the "new" here
    }
}

@Getter
@Builder(toBuilder = true)
class Entity{
    private String name;
    private boolean single = true;
}

@rzwitserloot
Copy link
Collaborator

Use @Builder.Default to fix this problem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants