rigd-loxia / builder-generator

Yet another builder generator. Primary style of this generator will be fluent wither, although other styles will be supported in the future. There is support for builder chaining.
Apache License 2.0
2 stars 2 forks source link

Support for Class with multiple constructors #38

Open Zegveld opened 11 months ago

Zegveld commented 11 months ago

When you have multiple constructors. The smaller ones usually have default values set for other fields, while the most extensive one has all options.

For example:

class MyClass {
    private final String defaultValue;
    private final String requiredValue;

    MyClass(String requiredValue) {
        this(requiredValue, "defaultValue");
    }

    MyClass(String requiredValue, defaultValue) {
        this.requiredValue = requiredValue;
        this.defaultValue = defaultValue;
    }
// getters
}

If you use a builder like this:

new MyClassBuilder().withRequiredValue("value").build();

then the wanted constructor would be MyClass(requiredValue)

However if you use a builder like this:

new MyClassBuilder().withRequiredValue("value").defaultValue("value").build();

then the wanted constructor would be MyClass(requiredValue, defaultValue)

So the builder should pick the correct constructor based on the values that are set in the builder.