Skip to content

sbouclier/intellij-plugin-java-builder-pattern

Repository files navigation

Build Status

intellij-plugin-java-builder-pattern

Java Builder Pattern plugin for Intellij IDEA with two types of builder :

  • classic builder
  • fluent builder with interfaces

Available on Jetbrains repository: https://plugins.jetbrains.com/plugin/13749-java-builder-pattern

Setup

Open File > Settings > Plugins. The easy way to find it is by searching java fluent builder pattern and to install Java Builder pattern:

alt text

Quickstart

Create a class with members, in this example :

public class Employee {
    private String socialSecurityNumber;    // required
    private String firstName;               // required
    private String lastName;                // required
    private LocalDate birthday;             // optional
    private int salary;                     // optional

}

Set your cursor inside the class where you want to generate the builder code, then right click and open 'Generate' menu :

alt text

Click on Builder menu, this will open a dialog box. You have choice to generate two types of builder :

Classic builder

alt text

First, select which class members you want to include in constructor builder. You can also override parameter methods by using prefix. Then select members you want to include and click on OK button, this will generate the builder code. You can now build your class like this :

Employee johnDoe = Employee.builder("123")
    .withFirstName("John")
    .withLastName("Doe")
    .withBirthday(LocalDate.of(1970, 1, 1))
    .withSalary(1000)
    .build();

Fluent builder with interfaces

alt text

Same steps as classic builder except you can now select mandatory/optional parameters. You can only select constructor parameters for mandatory parameters. You can build your class like this (as classic builder) :

Employee johnDoe = Employee.builder("123")
    .withFirstName("John")
    .withLastName("Doe")
    .withBirthday(LocalDate.of(1970, 1, 1))
    .withSalary(1000)
    .build();

But using auto completion, Intellij will provide you next valid parameter.

Next mandatory parameter:

alt text

Next optional parameter(s):

alt text

About

Java builder pattern plugin for Intellij IDEA

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages