mjedynak / builder-generator-idea-plugin

MIT License
151 stars 75 forks source link

Support Immutable Class Builders #57

Open abhinav3295 opened 3 years ago

abhinav3295 commented 3 years ago

Immutable classes has its fields declared as final which makes the current build method less helpful,

Suggest generating/updating constructor along with builder and do field assignment there

Sample generated code

public class Book {

    private final String name;
    private final Long costInCents;
    private final Optional<String> ISBN10;

    public Book(Builder builder) {
        this.name = builder.name;
        this.costInCents = builder.costInCents;
        this.ISBN10 = builder.ISBN10;
    }

    public static final class Builder {

        // Snip code for brevity

        public Book build() {
            return new Book(this);
        }
    }
}