Fluent Builder Generator Eclipse Plugin

About

The Builder Pattern was first introduced by Joshua Bloch at JavaOne 2007. It’s a pattern for class creation and helps getting rid of ugly constructors, constructor telescoping and increases the general readability of your code.

Feature

This project is a Eclipse plugin that automates the writing of these builders. With just two clicks you can generate all the builder code you need.

Example

public class Person {
    private final String firstname;
    private final String lastname;
    private final String address;
    private final String zipcode;
    private final String city;

    public Person(String firstname, String lastname, String address,
            String zipcode, String city) {
        this.firstname = firstname;
        this.lastname = lastname;
        this.address = address;
        this.zipcode = zipcode;
        this.city = city;
    }
}

Class with generated fluent builder:

public class Person {
    private final String firstname;
    private final String lastname;
    private final String address;
    private final String zipcode;
    private final String city;

    public Person(String firstname, String lastname, String address,
            String zipcode, String city) {
        this.firstname = firstname;
        this.lastname = lastname;
        this.address = address;
        this.zipcode = zipcode;
        this.city = city;
    }

    public static class PersonBuilder {
        private String firstname;
        private String lastname;
        private String address;
        private String zipcode;
        private String city;

        public PersonBuilder withFirstname(String firstname) {
            this.firstname = firstname;
            return this;
        }

        public PersonBuilder withLastname(String lastname) {
            this.lastname = lastname;
            return this;
        }

        public PersonBuilder withAddress(String address) {
            this.address = address;
            return this;
        }

        public PersonBuilder withZipcode(String zipcode) {
            this.zipcode = zipcode;
            return this;
        }

        public PersonBuilder withCity(String city) {
            this.city = city;
            return this;
        }

        public Person build() {
            return new Person(firstname, lastname, address, zipcode, city);
        }

        public static PersonBuilder person() {
            return new PersonBuilder();
        }
    }
}

GitHub repository:

https://github.com/coffeedriven/fbgep

Eclipse plugin update site:

http://coffeedriven.org/fbgep/update

  • Michael

    The update site does not work – 403 Forbidden.

    Also the plugin does not seem to work with Eclipse Neon. Meaning that the menu items do not show up as expected.