title: IntelliJ IDEA: simple way to create type-safe builder without plugins?
body: <p>Using IntelliJ IDEA, I want to create a POJO class that helps me avoid making silly mistakes, in terms of getting parameters around the wrong way.  </p>
  
  <ul>
  <li>I don't care about other IDEs</li>
  <li>I don't want to use any form of reflection (i.e. no Lombok or similar)</li>
  <li>I don't care about the JavaBean standard</li>
  <li>I don't care about "best practices"  </li>
  </ul>
  
  <p>I just want to create a class, declare some fields and have a convenient way to create said class that reduces the likelihood that I will get same-type parameters the wrong way round when I create the class.</p>
  
  <p>Starting with this:</p>
  
  <pre><code>public class TestObject {
    public String x;
    public String y; 
  }
  </code></pre>
  
  <p>I want to generate something I can use, like this:</p>
  
  <pre><code>TestObject o = new TestObject().setX("x").setY("y");
  </code></pre>
  
  <p>or this:</p>
  
  <pre><code>TestObject o = TestObjectBuilder.withX("x").withY("y").build();
  </code></pre>
  
  <p>I don't mind if there's a separate builder class (prefer to jam all the methods on the original class, but no biggie).  </p>
  
  <p>I don't mind if it makes many copies along the way.  Prefer if the whole process creates only 1 instance, like my solution below; but if the solution takes an immutable instance-per-setter-field approach - no biggie.</p>
  
  <hr>
  
  <p>Here's what I currently do to achieve this goal.</p>
  
  <p><strong>(1)</strong> Use IDEA to <code>Generate constructor</code>, selecting all fields, resulting in this:</p>
  
  <pre><code>public class TestObject {
    public String x;
    public String y;
  
    public TestObject(String x, String y) {
      this.x = x;
      this.y = y;
    }
  }
  </code></pre>
  
  <p><strong>(2)</strong> <code>Refactor Constructor with Builder</code>, having to select <code>Use existing</code>, then copy/paste the <code>TestObject</code> name into the field, because why not, resulting in:</p>
  
  <pre><code>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);
    }
  }
  </code></pre>
  
  <p><strong>(3)</strong> Manually delete the constructor (because its presence denies use of the default ctor) and delete the createTestObject() method (because it's superfluous, Java gives me a clone method for free).  Leaving me with this little beauty, which is all I wanted in the first place:</p>
  
  <pre><code>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;
    }
  }
  </code></pre>
  
  <p>How about it, Obi-wan Stack-overflow, is there a way to do this with less pfaffing about?</p>
  
  <p>Another thing I'd like to be able to do is add fields with less pfaffing about.  At the moment, when I add a field I'm doing <code>generate setter</code> the modifying the result by hand to be in line with the other setters - is there a better way to do that?</p>