All Projects → TakuSemba → Autobuilder

TakuSemba / Autobuilder

Auto builder generation with Annotation Processing

Programming Languages

java
68154 projects - #9 most used programming language

AutoBuilder

This is an sample project of Annotation Processing. AutoBuilder generates a Buidler of a class you annotated.

Usage

Add an @Buildable annotation to your class and @BuilderField annotations to your fields.

@Buildable 
class User {

  @BuilderField String name;

  @BuilderField int age;
}

You can use a generated builder after compiled.

User user = new UserBuilder().age(19).name("taku").build();

Generated code

// app/build/generated/source/apt/debug/com/takusemba/autobuildersample/UserBuilder.java
public final class UserBuilder {
  private String name;
  private int age;

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

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

  public User build() {
    User target = new User();
    target.name = this.name;
    target.age = this.age;
    return target;
  }
}
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].