mjedynak / builder-generator-idea-plugin

MIT License
150 stars 74 forks source link

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

Open peterlitvak opened 1 year ago

peterlitvak commented 1 year ago

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();
  2. 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();
  3. 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;
        ...
    }
    }