Tag Archives: plugin

Eclipse Java

Fluent Builder Generator Eclipse Plugin

I was looking recently for a nice way to generate Fluent Builder pattern code in Eclipse. I tried two plugins

  1. Builder Pattern Eclipse Plugin (https://github.com/henningjensen/bpep)
  2. fluent-builders-generator-eclipse-plugin (https://code.google.com/p/fluent-builders-generator-eclipse-plugin/)
but both are not enough for me. I was looking for a plugin with ability to generate Fluent Builder for immutable class with final fields. Unfortunately both mentioned plugins work only with mutable classes.
So I decided to write my own plugin as a fork of BPEP.
Below you can see some example class and builder generated for it by FBGEP – Fluent Builder Generator Eclipse Plugin.
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