jenzz/Java-PojoBuilder


A Java code generator for the builder pattern using annotation processing

Download


Step 1. Add the JitPack repository to your build file

Add it in your root settings.gradle at the end of repositories:

	dependencyResolutionManagement {
		repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
		repositories {
			mavenCentral()
			maven { url 'https://jitpack.io' }
		}
	}

Add it in your settings.gradle.kts at the end of repositories:

	dependencyResolutionManagement {
		repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
		repositories {
			mavenCentral()
			maven { url = uri("https://jitpack.io") }
		}
	}

Add to pom.xml

	<repositories>
		<repository>
		    <id>jitpack.io</id>
		    <url>https://jitpack.io</url>
		</repository>
	</repositories>

Add it in your build.sbt at the end of resolvers:

 
    resolvers += "jitpack" at "https://jitpack.io"
        
    

Add it in your project.clj at the end of repositories:

 
    :repositories [["jitpack" "https://jitpack.io"]]
        
    

Step 2. Add the dependency

	dependencies {
		implementation 'com.github.jenzz:Java-PojoBuilder:v1.0'
	}
	dependencies {
		implementation("com.github.jenzz:Java-PojoBuilder:v1.0")
	}
	<dependency>
	    <groupId>com.github.jenzz</groupId>
	    <artifactId>Java-PojoBuilder</artifactId>
	    <version>v1.0</version>
	</dependency>

                            
    libraryDependencies += "com.github.jenzz" % "Java-PojoBuilder" % "v1.0"
        
        

                            
    :dependencies [[com.github.jenzz/Java-PojoBuilder "v1.0"]]
        
        

Readme


Java - POJO Builder

  • A Java code generator for the builder pattern using annotation processing

Usage

Just annotate your POJOs with the @Builder annotation, like so:

@Builder
public class User {
  String name;
  int age;
  Address address;
}
@Builder
public class Address {
  String street;
  String postcode;
  String city;
}

When you compile your project, a new Builder class will be generated for each annotated POJO, like so:

public final class UserBuilder {

  private String name;
  private int age;
  private Address address;

  public static UserBuilder user() {
    return new UserBuilder();
  }

  public static UserBuilder user(User from) {
    UserBuilder builder = new UserBuilder();
    builder.name = from.name;
    builder.age = from.age;
    builder.address = from.address;
    return builder;
  }

  public UserBuilder name(String name) {
    this.name = name;
    return this;
  }

  public UserBuilder age(int age) {
    this.age = age;
    return this;
  }

  public UserBuilder address(Address address) {
    this.address = address;
    return this;
  }

  public UserBuilder address(AddressBuilder addressBuilder) {
    this.address = addressBuilder.build();
    return this;
  }

  public User build() {
    User user = new User();
    user.name = name;
    user.age = age;
    user.address = address;
    return user;
  }
}

Using static imports, you can then create those objects as simple as:

User user = user()
  .name("Bob The Builder")
  .age(25)
  .address(
      address()
      .street("10 Hight Street")
      .postcode("WC2")
      .city("London"))
  .build();

Fields annotated with @Ignore will be ignored.

Example

Check out the sample project for an example implementation.

The sample project is currently an Android module, but it should work with a pure Java project just as well. If somebody knows how to setup an annotation processor for a Java module using Gradle, please let me know. The apt plugin currently supports only Android modules.

Download

Grab it via Gradle:

compile 'com.jenzz.pojobuilder:api:1.0'
apt 'com.jenzz.pojobuilder:processor:1.0'

You only need to include the api module (contains just the annotations) as a real dependency.

The annotation processor module can be a compile time only dependency. To do that I highly recommend using Hugo Visser's great apt plugin.

License

This project is licensed under the MIT License.