Skip to content

⏳ An annotation processor which implements "Builder Pattern" for your java classes.

Notifications You must be signed in to change notification settings

AliAsadi/builder-annotation

Repository files navigation

builder-annotation

An annotation processor which implements "Builder Pattern" for your java classes.


Example:

we annotated our class with @Builder and initialize a constructor with all the fields.

@Builder
public class User {
    private int id;
    private String name;
    private String pass;
    private String email;
    private String age;

    public User(int id, String name, String pass, String email, String age) {
        this.id = id;
        this.name = name;
        this.pass = pass;
        this.email = email;
        this.age = age;
    }
}

Result:

generated class, the compiler did the rest for us to avoid boilerplates and repeated codes.

public class User_Builder {

  private int id;
  private String name;
  private String pass;
  private String email;
  private String age;

  public User_Builder() {
  }

  public User_Builder setId(int id) {
    this.id = id;
    return this;
  }

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

  public User_Builder setPass(String pass) {
    this.pass = pass;
    return this;
  }

  public User_Builder setEmail(String email) {
    this.email = email;
    return this;
  }

  public User_Builder setAge(String age) {
    this.age = age;
    return this;
  }

  public User build() {
    return new User(id,name,pass,email,age);
  }
}

Then:

 User user = new User_Builder()
                ...
                .setName("Ali")
                .setEmail("ali@gmail.com")
                .build();

Used libraries

Useful reads

MIT License
Copyright (c) 2018 Ali Esa Assadi