BetterBuilder is a Java annotation processor used for automatically generating better builder codes(builder design pattern), which can make coding much more comfortable.
- fluent get/set method
- configurable get/set.
- 2 set types provided
- 3 builder patterns provided:
BetterBuilder doesn't add any runtime dependencies to your codes.
Download from releases. (Just add it to your classpath)
BetterBuilder(v1.0.8) has already been published to Central https://repo1.maven.org/maven2/.
Example Maven settings:
<dependency>
<groupId>cn.mpy634</groupId>
<artifactId>BetterBuilder</artifactId>
<version>1.0.8</version>
<scope>provided</scope>
</dependency>
Simple example; See how to customize
Given a class "Student":
import cn.mpy634.annotation.BetterBuilder;
// All configurations are default.
@BetterBuilder
public class Student {
private String name;
private Integer ID;
}
The compiled code could be :
public class Student {
private String name;
private Integer ID;
public Student ID(Integer ID) {this.ID = ID;return this;}
public Integer ID() {return this.ID;}
public Student name(String name) {this.name = name;return this;}
public String name() {return this.name;}
public static Student.StudentBuilder builder() {return new Student.StudentBuilder();}
public Student(String name, Integer ID) {this.name = name;this.ID = ID;}
public Student() {}
public static class StudentBuilder {
private String name;
private Integer ID;
private StudentBuilder() {}
public Student.StudentBuilder name(String name) {this.name = name;return this;}
public Student.StudentBuilder ID(Integer ID) {this.ID = ID;return this;}
public Student build() {return new Student(this.name, this.ID);}
}
}
Therefore you can code like
Student stu = Student.builder().ID(xx).name(xx)....build().ID(xx).name(xx)...
.
You can also customize BetterBuilder.
Once make fluentSet = false
, BetterBuilder will not generate set methods.
@BetterBuilder(fluentSet = false)
public class Student {
...
}
Once make fluentGet = false
, BetterBuilder will not generate get methods.
@BetterBuilder(fluentGet = false)
public class Student {
...
}
Make setType = 0 / 1
to change the return type of generated set methods.
Given a field private Integer ID;
, 2 kinds of set methods are available.
When setType = 0
, which is default( strongly suggested ):
@BetterBuilder(setType = 0)
public class Student {
private Integer ID;
public Student ID(Integer ID){this.ID = ID; return this;}
}
when setType = 1
, set methods will return nothing:
@BetterBuilder(setType = 1)
public class Student {
private Integer ID;
public void ID(Integer ID){this.ID = ID;}
}
Once make BUILDER_TYPE = BuilderType.NO_BUILDER
, BetterBuilder will not generate builder methods (nor the allArgsConstructor).
@BetterBuilder(BUILDER_TYPE = BuilderType.NO_BUILDER)
public class Student {
...
}
Make any fields annotated with @IgnoreGet or @IgnoreSet, BetterBuilder will not generate the get or set methods for them.
@BetterBuilder
public class Student {
@IgnoreSet
private String 牛;
@IgnoreGet
private Integer 年;
@IgnoreGet
@IgnoreSet
private Student 大;
private List<Boolean> 吉;
}
It is for those fields that aren't allowed to be changed or accessed after initialization.
When we use builder pattern to generate our object, some fields are supposed to be initialized. But the classic pattern does not guarantee this.
BetterBuilder provides a type-safe builder pattern. Once the fields annotated with @Required haven't been initialized, the goal object will not be generated ( Instead, an IllegalArgumentException will be thrown ).
@BetterBuilder(BUILDER_TYPE = BuilderType.TYPE_SAFE, fluentSet = false, fluentGet = true)
public class TypeSafe {
@BetterBuilder.Required
private Integer ID;
@BetterBuilder.Required
private String name;
private Boolean PID;
private Long PID79211;
}
You can also see the detail files : type-safe and type-safe-test.
- fluent - builder / test
- noBuilder
- fluent - set / test
- chain set options
- ignore set
- fluent - get / test
- ignore get
- compatible with lombok
- type-safe builder
...
- Any bugs or suggestions? Plz make PRs or issues.