Java: auto-generated POJO builders


6 April 2015, by

Any of you that have worked for a while with Java have undoubtedly come across code similar to this, usually in unit tests:

public SomeEntity createEntityWithFoo(String foo) {
    SomeEntity entity = new Entity();
    entity.setFoo(foo);
    return entity;
}

public SomeEntity createEntityWithFooAndBar(String foo, String bar) {
    SomeEntity entity = new Entity();
    entity.setFoo(foo);
    entity.setBar(bar);
    return entity;
}

Since you are all well-versed in your design patterns, you next thought ‘I could get rid of these by creating a fluent SomeEntityBuilder class and do away with these superfluous methods. Suddenly your tests are much neater, with your objects neatly created by builders:

SomeEntity entityWithFoo = new SomeEntityBuilder().withFoo("Foooooo!").build();
SomeEntity entityWithFooAndBar = new SomeEntityBuilder().withFoo("Foooooo!").withBar("I am a banana!").build();

The new problem is that now you have to maintain an extra Builder class on top of your SomeEntity class. When you have done this repeatedly, you might have noticed that usually your Builder classes look something like

public class SomeEntityBuilder {

    private SomeEntity entity;

    public SomeEntityBuilder() {
        entity = new SomeEntity();
    }

    public SomeEntityBuilder withFoo(String foo) {
        entity.setFoo(foo);
        return this;
    }

    // Another bazilion with[Property Name Here](...) methods which look exactly the same

    public SomeEntity build() {
        return entity;
    }
}

Every one of them has

  1. A constructor which creates an empty instance of the thing you are building
  2. A load of withProperty() methods, which call the relevant property setter and return this.
  3. build() method which returns the thing you are building.

Wouldn’t it be nice if we could auto-generate all of that? Well, we can.


Pojobuilder

The pojobuilder library allows you to simply annotate your POJO with @GeneratePojoBuilder, give it a bunch of useful options such as

  • Destination package (optionally) relative to the original POJO package.
  • Initialisation using a pre-existing entity
  • Optional setters (with support for the Guava version if you are a bit behind the Java 8 times).
  • Factory constructors.
  • Nested builder setters.
  • Meta-annotations.

The code generation can be run via javac, Ant (via the javac task), Gradle, Maven or Eclipse. For example, adding it to your Maven build process is as simple as adding the dependency to the project pom.xml – Maven will auto-detect and run it during the compile step.

I have tried it out on a Softwire project and so far it is working rather well. No more (well, slightly less) DTO boiler-plate code, not having to worry about maintaining multiple classes instead of one and not being afraid of adding more POJOs because you’d have to write yet another builder class for them.


Tags: , ,

Categories: Technical

«
»

One Response to “Java: auto-generated POJO builders”

  1. Simon Wilson says:

    Nice. I recently came across NBuilder which does a similar thing for .net. The resulting syntax isn’t quite as neat as a dedicated builder, but it avoids a lot of boilerplate and it’s particularly powerful if you want to create a collection of many similar objects.


Leave a Reply

* Mandatory fields


2 + four =

Submit Comment