Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Builder pattern code generation in IntelliJ

Is there any way to automate writing Builder patterns in IntelliJ?

For example, given this simple class:

class Film {    private String title;    private int length;     public void setTitle(String title) {        this.title = title;    }     public String getTitle() {        return this.title;    }     public void setLength(int length) {        this.length = length;    }     public int getLength() {        return this.length;    } } 

is there a way that I could get the IDE to generate this, or similar:

public class FilmBuilder {      Film film;      public FilmBuilder() {         film = new Film();     }      public FilmBuilder withTitle(String title) {         film.setTitle(title);         return this;     }      public FilmBuilder withLength(int length) {         film.setLength(length);         return this;     }      public Film build() {         return film;     } } 
like image 385
Martyn Avatar asked Apr 27 '12 09:04

Martyn


People also ask

How do I create a builder class in Intellij?

Similarly, as for InnerBuilder, we can either press Alt+Insert (on PC) and choose Builder option or use Alt+Shift+B shortcut. The first option that Builder Generator plugin provides to customize the created builder class – Inner builder – is rather self-explanatory.


2 Answers

Use the Replace Constructor with Builder refactoring.

To use this function, click on the constructor's signature in your code, then right click and select the "Refactor" menu, then click "Replace Constructor with Builder..." to bring up the dialog box to generate the code.

like image 109
CrazyCoder Avatar answered Oct 15 '22 05:10

CrazyCoder


I found the built-in builder pattern generation in IntelliJ to be a bit clunky for a few reasons:

  1. It needs to use an existing constructor as reference.
  2. It's not quickly accessible via the "Generate" menu (command+N on OS X).
  3. It only generates external Builder classes. As others have mentioned, it's very common to use static inner classes when implementing the builder pattern.

The InnerBuilder plugin addresses all of these shortcomings, and requires no setup or configuration. Here's a sample Builder generated by the plugin:

public class Person {     private String firstName;     private String lastName;     private int age;      private Person(Builder builder) {         firstName = builder.firstName;         lastName = builder.lastName;         age = builder.age;     }      public static final class Builder {         private String firstName;         private String lastName;         private int age;          public Builder() {         }          public Builder firstName(String firstName) {             this.firstName = firstName;             return this;         }          public Builder lastName(String lastName) {             this.lastName = lastName;             return this;         }          public Builder age(int age) {             this.age = age;             return this;         }          public Person build() {             return new Person(this);         }     } } 
like image 20
Mansoor Siddiqui Avatar answered Oct 15 '22 07:10

Mansoor Siddiqui