Skip to content
Slothsoft edited this page Mar 4, 2019 · 3 revisions

Builder Pattern Plug-in

A small plug-in that helps you create not only getters and setters, but fluent methods, too. Works very similar (and is actually based on) the getter / setter dialog in the Eclipse JDT.

We call these methods "builder pattern" at work, because Builders where the first classes that used these methods.

In case you don't know about the fluent API, or maybe you do know it, but it has a different name for you, too - this is an example:

class Person {
    String firstName;
    String lastName;
    int age;

    Person firstName(String firstName) {
        this.firstName = firstName;
        return this;
    }
    
    Person lastName(String lastName) {
        this.lastName = lastName;
        return this;
    }

    Person age(int age) {
        this.age = age;
        return this;
    }
}

Which allows you to do something like that:

Person person1 = new Person().firstName("Korra").age(21);
Person person2 = new Person().firstName("Asami").lastName("Sato").age(22);
Person person3 = new Person().firstName("Tenzin");

(In my company, we would have created three constructors for this use case. And that's bad. Just think about what happens if we add another field or two. We had classes with 30 or mor constructors, complete with different default values for the fields not filled.)

So lately I started using fluent API more and more, and it's really hard to create all these methods, and since I have to do it by hand, it's error prone. I'm a perfectionist, but I do have the attention span of a cookie.

Therefore I create this Eclipse plug-in. It's only a raw first sketch, and I'll probably have to test it a bit before it works perfectly, but I thought I'd share it, so you can laugh about my bad code and my puny attempts to hook into the Eclipse platform. It was a first. And firsts are always bad.

The Plug-in

Menu Contribution

Okay, so the plug-in hooks into the "Source" menu of the Java editor.

Dialog

And when it's opened, it looks a lot like the regular getter / setter dialog, just with an additional option. Nice, isn't it? So yes, as usual you select what you want to generate, and it's done.

Change Log

Features 1.0.0

  • ignores static fields (because that would be silly)
  • button to select all builder setters by default
  • realizes when a field is final
  • insertion point in the class
  • sorting of the generated methods
  • access modifiers public / protected / package / private, final and synchronized
  • comment generation
  • builder methods use the setter if present, else they'll set the field manually

Features 1.0.1

  • parameter names start with "new" ("newFirstName" instead of "firstName")
  • fields are preselected like in the getter / setter dialog

Features 1.0.2

  • some various bugfixes
  • configurable key binding (CTRL + ALT + B on default)

Features 1.0.3

  • no idea

Features 1.0.4

  • made it work with Eclipse Oxygen

Features 1.1.0

Clone this wiki locally
You can’t perform that action at this time.