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

[FEATURE REQUEST] Utility builder's methods and ability to ignore annotations on the class fields #71

Open
peterlitvak opened this issue Mar 7, 2023 · 0 comments

Comments

@peterlitvak
Copy link

peterlitvak commented Mar 7, 2023

There are a few features that would be nice to add to the generator.

  1. An option to add a static builder() method in the enclosing class. e.g.:
public record RecordA(String fieldOne, String fieldTwo) {

    public static RecordABuilder builder() {
        return new RecordABuilder();
    } 

    public static final class RecordABuilder {
    ...
    }
}

This would make the creation of the instances read a bit more fluent, e.g.:

RecordA a = RecordA.builder().withFieldOne("").build();

vs.

RecordA a = RecordABuilder.aRecordA().withFieldOne("").build();
  1. A copy constructor for the builder, e.g.:
public record RecordA(String fieldOne, String fieldTwo) {

    public static RecordABuilder builder() {
        return new RecordABuilder();
    }

    public static RecordABuilder builder(RecordA recordA) {
        return new RecordABuilder(recordA);
    }

    public static final class RecordABuilder {
        private String fieldOne;
        private String fieldTwo;

        private RecordABuilder() {
        }

        private RecordABuilder(RecordA recordA) {
            this.fieldOne = recordA.fieldOne();
            this.fieldTwo = recordA.fieldTwo();
        }
     ...
  }
}

This would allow writing code like this:

RecordA  a1 = RecordA.builder().withFieldOne("1").withFieldTwo("2").build();
RecordA a2 = RecordA.builder(a1).withFieldOne("xyz").build();
  1. Ability to ignore annotations on the class fields for which a builder is created. At the moment, it will generate the following:
public record RecordA(@NotNull String fieldOne, @NotNull String fieldTwo) {
    public static final class RecordABuilder {
        private @NotNull String fieldOne;
        private @NotNull String fieldTwo;
        ...
   }
}
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

1 participant