Java – IntelliJ IDEA : simple way to create type-safe builder without plugins?

IntelliJ IDEA : simple way to create type-safe builder without plugins?… here is a solution to the problem.

IntelliJ IDEA : simple way to create type-safe builder without plugins?

I want to create a POJO class using IntelliJ IDEA to help me avoid making silly mistakes like getting parameters the wrong way.

  • I don’t care about other IDEs
  • I don’t want to use any form of reflection (i.e. no Lombok or anything like that).
  • I don’t care about the JavaBean standard
  • I don’t care about “best practices”

I just want to create a class

, declare some fields and have a convenient way to create said class to reduce the possibility that I get the same type parameter the wrong way when creating the class.

Start here:

public class TestObject {
  public String x;
  public String y; 
}

I

want to generate something I can use, like this :

TestObject o = new TestObject().setX("x").setY("y");

Or this:

TestObject o = TestObjectBuilder.withX("x").withY("y").build();

I don’t mind if there’s a separate builder class (prefer to put all the methods on the original class, but no big deal).

I don’t mind it copying many copies along the way. Prefer to create only 1 instance for the whole process, like my solution below; However, if the solution takes an immutable instance per-setter field approach – no big deal.


Here’s what I’m currently doing to make that happen.

(1) Use IDEA to generate the constructor, select all fields, and the result is as follows:

public class TestObject {
  public String x;
  public String y;

public TestObject(String x, String y) {
    this.x = x;
    this.y = y;
  }
}

(2) Refactor Constructor with Builder, you must select Use existing, and then copy/paste the TestObject name into the field, because why not, causes:

public class TestObject {
  public String x;
  public String y;

public TestObject(String x, String y) {
    this.x = x;
    this.y = y;
  }

public TestObject setX(String x) {
    this.x = x;
    return this;
  }

public TestObject setY(String y) {
    this.y = y;
    return this;
  }

public TestObject createTestObject() {
    return new TestObject(x, y);
  }
}

(3) Manually remove the constructor (

which rejects the default constructor because of its presence) and remove the createTestObject() method (because it is redundant, Java gave me a clone method for free). Leave me this little beauty, this is what I originally wanted :

public class TestObject {
  public String x;
  public String y;

public TestObject setX(String x) {
    this.x = x;
    return this;
  }

public TestObject setY(String y) {
    this.y = y;
    return this;
  }
}

How, Obi-wan Stack-overflow, is there a way to do this without?

Another thing I’d like to be able to do is add fields instead of. Currently, when I add a field, I’m doing generate setters to manually modify the results to be consistent with other setters – is there a better way?

Solution

Yes, it is possible.

  1. Press Alt-Insert (or Code->Build) to open the Build menu
  2. Choose a setter.
  3. In the next window, select “Builder” in the top drop-down menu called “Template” (by default it is “intellij Default”).

You will get exactly the same result as you want.

Related Problems and Solutions