Smart Builder generator

Compatible with IntelliJ IDEA
17.11.2014
826
Based on build pattern described: blog.crisp.se

To generate builder just hit Alt+Insert and choose 'Smart Builder generator'.
Plugin allows you to easily create builder for given class. Eg. having class:
public class Person {
    String mandatoryFirstName;
    String mandatoryLastName;
    int mandatoryAge;
    int optionalHeight;
    Color optionalEyeColor;
    Color optionalHairColor;
}
You can generate builder:
public class Person {
    String mandatoryFirstName;
    String mandatoryLastName;
    int mandatoryAge;
    int optionalHeight;
    Color optionalEyeColor;
    Color optionalHairColor;

    private Person() {
    }

    public static IMandatoryLastName mandatoryFirstName(String mandatoryFirstName) {
        return new Person.Builder(mandatoryFirstName);
    }

    public interface IMandatoryLastName {
        IMandatoryAge mandatoryLastName(String mandatoryLastName);
    }

    public interface IMandatoryAge {
        IBuild mandatoryAge(int mandatoryAge);
    }

    public interface IBuild {
        IBuild optionalHeight(int optionalHeight);
        IBuild optionalEyeColor(Color optionalEyeColor);
        IBuild optionalHairColor(Color optionalHairColor);
        Person build();
    }

    private static class Builder implements IMandatoryLastName, IMandatoryAge, IBuild {
        private Person instance = new Person();

        private Builder(String mandatoryFirstName) {
            instance.mandatoryFirstName = mandatoryFirstName;
        }

        @Override
        public IMandatoryAge mandatoryLastName(String mandatoryLastName) {
            instance.mandatoryLastName = mandatoryLastName;
            return this;
        }

        @Override
        public IBuild mandatoryAge(int mandatoryAge) {
            instance.mandatoryAge = mandatoryAge;
            return this;
        }

        @Override
        public IBuild optionalHeight(int optionalHeight) {
            instance.optionalHeight = optionalHeight;
            return this;
        }

        @Override
        public IBuild optionalEyeColor(Color optionalEyeColor) {
            instance.optionalEyeColor = optionalEyeColor;
            return this;
        }

        @Override
        public IBuild optionalHairColor(Color optionalHairColor) {
            instance.optionalHairColor = optionalHairColor;
            return this;
        }

        public Person build() {
            return instance;
        }
    }
}
And use it like:
Person build = Person.mandatoryFirstName("John").mandatoryLastName("Doe").mandatoryAge(18).optionalHeight(180).build();

Download plugin

Recent change notes

Version 1.2 - Fixed bug reported by Cassius Cai.