A Java annotation processor used for automatically generating better builder codes.

Overview

BetterBuilder

GitHub license Build status Version

BetterBuilder is a Java annotation processor used for automatically generating better builder codes(builder design pattern), which can make coding much more comfortable.

Why better ?

Getting BetterBuilder

BetterBuilder doesn't add any runtime dependencies to your codes.

Directly reach the jar

Download from releases. (Just add it to your classpath)

Maven

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>

Usage

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.

Customization

FluentSet switch

Once make fluentSet = false, BetterBuilder will not generate set methods.

@BetterBuilder(fluentSet = false)
public class Student {
    ...
}

FluentGet switch

Once make fluentGet = false, BetterBuilder will not generate get methods.

@BetterBuilder(fluentGet = false)
public class Student {
    ...
}

Set type

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;}
}

NoBuilder switch

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 {
    ...
}

Field ignore

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.

Type-Safe Builder

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.

Todo list

  • fluent - builder / test
    • noBuilder
  • fluent - set / test
    • chain set options
    • ignore set
  • fluent - get / test
    • ignore get
  • compatible with lombok
  • type-safe builder

...

Others

  • Any bugs or suggestions? Plz make PRs or issues.
You might also like...

A fast JSON parser/generator for Java.

A fast JSON parser/generator for Java.

fastjson Fastjson is a Java library that can be used to convert Java Objects into their JSON representation. It can also be used to convert a JSON str

Dec 31, 2022

JSON to JSON transformation library written in Java.

Jolt JSON to JSON transformation library written in Java where the "specification" for the transform is itself a JSON document. Useful For Transformin

Dec 30, 2022

Java JsonPath implementation

Jayway JsonPath A Java DSL for reading JSON documents. Jayway JsonPath is a Java port of Stefan Goessner JsonPath implementation. News 10 Dec 2020 - R

Jan 4, 2023

Sawmill is a JSON transformation Java library

Sawmill is a JSON transformation Java library

Update: June 25, 2020 The 2.0 release of Sawmill introduces a breaking change to the GeoIpProcessor to comply with the updated license of the MaxMind

Jan 1, 2023

Genson a fast & modular Java Json library

Genson Genson is a complete json - java conversion library, providing full databinding, streaming and much more. Gensons main strengths? Easy to use

Jan 3, 2023

A reference implementation of a JSON package in Java.

JSON in Java [package org.json] Click here if you just want the latest release jar file. Overview JSON is a light-weight language-independent data int

Jan 6, 2023

Fast JSON parser for java projects

ig-json-parser Fast JSON parser for java projects. Getting started The easiest way to get started is to look at maven-example. For more comprehensive

Dec 26, 2022

Java JsonPath implementation

Jayway JsonPath A Java DSL for reading JSON documents. Jayway JsonPath is a Java port of Stefan Goessner JsonPath implementation. News 10 Dec 2020 - R

Jan 5, 2023

Generate Java types from JSON or JSON Schema and annotates those types for data-binding with Jackson, Gson, etc

jsonschema2pojo jsonschema2pojo generates Java types from JSON Schema (or example JSON) and can annotate those types for data-binding with Jackson 2.x

Jan 5, 2023
Releases(v1.0.8)
A JSON Transmission Protocol and an ORM Library for automatically providing APIs and Docs.

?? 零代码、热更新、全自动 ORM 库,后端接口和文档零代码,前端(客户端) 定制返回 JSON 的数据和结构。 ?? A JSON Transmission Protocol and an ORM Library for automatically providing APIs and Docs.

Tencent 14.4k Dec 31, 2022
Configuration library based on annotation processing

net.cactusthorn.config The Java library with the goal of minimizing the code required to handle application configuration. Motivation The inspiring id

Alexei Khatskevich 7 Jan 8, 2023
Jakarta money is a helpful library for a better developer experience when combining Money-API with Jakarta and MicroProfile API.

jakarta-money Jakarta money is a helpful library for a better developer experience when combining Money-API with Jakarta and MicroProfile API. The dep

Money and Currency API | JavaMoney 19 Aug 12, 2022
Convert Java to JSON. Convert JSON to Java. Pretty print JSON. Java JSON serializer.

json-io Perfect Java serialization to and from JSON format (available on Maven Central). To include in your project: <dependency> <groupId>com.cedar

John DeRegnaucourt 303 Dec 30, 2022
A Java serialization/deserialization library to convert Java Objects into JSON and back

Gson Gson is a Java library that can be used to convert Java Objects into their JSON representation. It can also be used to convert a JSON string to a

Google 21.7k Jan 8, 2023
A universal types-preserving Java serialization library that can convert arbitrary Java Objects into JSON and back

A universal types-preserving Java serialization library that can convert arbitrary Java Objects into JSON and back, with a transparent support of any kind of self-references and with a full Java 9 compatibility.

Andrey Mogilev 9 Dec 30, 2021
A simple java JSON deserializer that can convert a JSON into a java object in an easy way

JSavON A simple java JSON deserializer that can convert a JSON into a java object in an easy way. This library also provide a strong object convertion

null 0 Mar 18, 2022
Java with functions is a small java tools and utils library.

Java with functions is a small java tools and utils library.

null 4 Oct 14, 2022
Set of support modules for Java 8 datatypes (Optionals, date/time) and features (parameter names)

Overview This is a multi-module umbrella project for Jackson modules needed to support Java 8 features, especially with Jackson 2.x that only requires

FasterXML, LLC 372 Dec 23, 2022
A modern JSON library for Kotlin and Java.

Moshi Moshi is a modern JSON library for Android and Java. It makes it easy to parse JSON into Java objects: String json = ...; Moshi moshi = new Mos

Square 8.7k Dec 31, 2022