Blog coding and discussion of coding about JavaScript, PHP, CGI, general web building etc.

Tuesday, February 2, 2016

Builder pattern code generation in IntelliJ

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;      }  }  

Answer by CrazyCoder for Builder pattern code generation in IntelliJ


Use the Replace Constructor with Builder refactoring.

Answer by P?lOliver for Builder pattern code generation in IntelliJ


If you are wondering if this can be used to create a class with an inner builder class as described by Joshua Block - you just have to define an empty inner class first, then check "Use existing" and search for your newly created (inner class) and hit "Refactor".

PS! Cursor must reside inside constructor (pre-written) in order to use the "Replace Constructor with Builder" refactoring function.

Answer by tibtof for Builder pattern code generation in IntelliJ


As a complement to @CrazyCoder's answer, I think it's very useful to know that in the upper-right side of Replace Constructor with Builder dialog there is a configuration button that you can use to rename setters' prefix.

Answer by Mansoor Siddiqui for Builder pattern code generation in IntelliJ


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);          }      }  }  

Answer by nucatus for Builder pattern code generation in IntelliJ


The IntelliJ way to to this is, IMHO, is convoluted. There are out there two plugins (I prefer this one: https://plugins.jetbrains.com/plugin/7354) that serve the purpose much much better.

For example, I prefer having the Builder class as an inner class of the PoJo. To achieve that with IntelliJ you need few extra strokes.

Another plus for the plugin is the location of the functionality (in the Generate... context menu).

Answer by jFrenetic for Builder pattern code generation in IntelliJ


Here's how to overcome the shortcomings mentioned by Mansoor Siddiqui:

1) It needs to use an existing constructor as reference.

Which is very easy to generate. Just hit Alt + Ins on Windows to invoke Generate menu and choose Constructor.

2) It's not quickly accessible via the "Generate" menu (command+N on OS X)

Just go to Settings -> Keymap, search for Builder and assign it a shortcut of your choice (if you use this functionality very often, which is rarely the case). You can assign Alt + B for example.

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.

I also prefer my builders as static inner classes. Unfortunately, there is no straightforward way to do it, but it's still feasible. You just have to define the nested inner class yourself (leave it empty), and when you invoke Replace Constructor with Builder dialog, choose Use existing option and select your inner class. Works like a charm! Although, it would've been easier to make this option configurable.


Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\XAMPP INSTALLASTION\xampp\htdocs\endunpratama9i\www-stackoverflow-info-proses.php on line 72

0 comments:

Post a Comment

Popular Posts

Powered by Blogger.