Code Monkey home page Code Monkey logo

java-pojobuilder's Introduction

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.

java-pojobuilder's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

java-pojobuilder's Issues

Add required fields

Is it possible to add a @required annotation to define that a class member is required in the Builder constructor?

As an example, considering the following POJO:

@Builder
public class User {
  @Required
  String name;
  int age;
}

It will generate the following Builder class:

public final class UserBuilder {

  private final String name;
  private int age;

  private UserBuilder(final String name) {
    this.name = name
  }

  public static UserBuilder user(final String name) {
    return new UserBuilder(name);
  }

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

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

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

Ignore static fields

It would be useful if the generator would auto-ignore static fields.

for example the code below generates an error.

@Builder
public class MyClass {
     public int field1;
     public String field2;

    // Key used to map this class somewhere else in the app (e.g. Bundle or Intent)
    private static final String KEY_MAP = "myclass.key.map";

   public void saveTo(Map<String, MyClass> map) {
       // maps this object into the map using KEY_MAP
   }
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.