|
| 1 | +# Fluent Builder Generator |
| 2 | + |
| 3 | +A Java annotation processor library that automatically generates type-safe fluent builders for Java records with mandatory and optional fields. |
| 4 | + |
| 5 | +## Features |
| 6 | + |
| 7 | +- **Type-safe fluent builders**: Generated builders enforce that all mandatory fields must be set before `build()` can be called |
| 8 | +- **Flexible field ordering**: Mandatory fields can be set in any order |
| 9 | +- **Optional fields**: Non-mandatory fields can be set at any time during the building process |
| 10 | +- **Ignored fields**: Fields marked with `@Ignore` are excluded from the builder (useful for computed/internal fields) |
| 11 | +- **Compile-time code generation**: Uses annotation processing to generate builders at compile time |
| 12 | +- **Works with any number of mandatory fields**: Automatically generates the appropriate builder levels |
| 13 | + |
| 14 | +## Project Structure |
| 15 | + |
| 16 | +``` |
| 17 | +fluent-builder-parent/ |
| 18 | +├── fluent-builder-annotations/ # Contains @FluentBuilder and other annotations |
| 19 | +├── fluent-builder-processor/ # Annotation processor that generates builder code |
| 20 | +└── fluent-builder-example/ # Example usage |
| 21 | +``` |
| 22 | + |
| 23 | +## Usage |
| 24 | + |
| 25 | +### 1. Add dependencies to your project |
| 26 | + |
| 27 | +```xml |
| 28 | +<dependencies> |
| 29 | + <dependency> |
| 30 | + <groupId>stream.header.fluentbuilder</groupId> |
| 31 | + <artifactId>fluent-builder-annotations</artifactId> |
| 32 | + <version>1.0-SNAPSHOT</version> |
| 33 | + </dependency> |
| 34 | + <dependency> |
| 35 | + <groupId>stream.header.fluentbuilder</groupId> |
| 36 | + <artifactId>fluent-builder-processor</artifactId> |
| 37 | + <version>1.0-SNAPSHOT</version> |
| 38 | + <scope>provided</scope> |
| 39 | + </dependency> |
| 40 | +</dependencies> |
| 41 | + |
| 42 | +<build> |
| 43 | + <plugins> |
| 44 | + <plugin> |
| 45 | + <groupId>org.apache.maven.plugins</groupId> |
| 46 | + <artifactId>maven-compiler-plugin</artifactId> |
| 47 | + <version>3.11.0</version> |
| 48 | + <configuration> |
| 49 | + <annotationProcessorPaths> |
| 50 | + <path> |
| 51 | + <groupId>stream.header.fluentbuilder</groupId> |
| 52 | + <artifactId>fluent-builder-processor</artifactId> |
| 53 | + <version>1.0-SNAPSHOT</version> |
| 54 | + </path> |
| 55 | + </annotationProcessorPaths> |
| 56 | + </configuration> |
| 57 | + </plugin> |
| 58 | + </plugins> |
| 59 | +</build> |
| 60 | +``` |
| 61 | + |
| 62 | +### 2. Annotate your record |
| 63 | + |
| 64 | +```java |
| 65 | +import stream.header.fluentbuilder.FluentBuilder; |
| 66 | +import stream.header.fluentbuilder.FluentBuilder.Mandatory; |
| 67 | +import stream.header.fluentbuilder.FluentBuilder.Ignore; |
| 68 | + |
| 69 | +@FluentBuilder |
| 70 | +public record Person( |
| 71 | + @Mandatory String firstName, |
| 72 | + @Mandatory String lastName, |
| 73 | + @Mandatory int age, |
| 74 | + String email, |
| 75 | + String phoneNumber, |
| 76 | + String address, |
| 77 | + @Ignore String internalId // Ignored - not part of builder, will be null |
| 78 | +) { |
| 79 | +} |
| 80 | +``` |
| 81 | + |
| 82 | +### 3. Use the generated builder |
| 83 | + |
| 84 | +```java |
| 85 | +// All mandatory fields must be set before build() is available |
| 86 | +Person person1 = PersonBuilder.builder() |
| 87 | + .firstName("John") |
| 88 | + .lastName("Doe") |
| 89 | + .age(30) |
| 90 | + .email("john.doe@example.com") |
| 91 | + .phoneNumber("123-456-7890") |
| 92 | + .build(); |
| 93 | + |
| 94 | +// Mandatory fields can be set in any order |
| 95 | +Person person2 = PersonBuilder.builder() |
| 96 | + .lastName("Smith") |
| 97 | + .email("jane.smith@example.com") // Optional field can be set anytime |
| 98 | + .firstName("Jane") |
| 99 | + .address("123 Main St") |
| 100 | + .age(25) |
| 101 | + .build(); |
| 102 | + |
| 103 | +// Only mandatory fields |
| 104 | +Person person3 = PersonBuilder.builder() |
| 105 | + .age(40) |
| 106 | + .firstName("Bob") |
| 107 | + .lastName("Johnson") |
| 108 | + .build(); |
| 109 | +``` |
| 110 | + |
| 111 | +## How It Works |
| 112 | + |
| 113 | +The annotation processor generates a multi-level builder pattern: |
| 114 | + |
| 115 | +1. **OptionalBuilder**: Abstract base class with methods for all optional fields |
| 116 | +2. **InitialBuilder**: Starting point - offers methods for each mandatory field |
| 117 | +3. **Intermediate Builders**: One for each combination of mandatory fields not yet set |
| 118 | +4. **FinalBuilder**: Has all mandatory fields set - only this builder has a `build()` method |
| 119 | + |
| 120 | +This ensures at compile time that you cannot call `build()` until all mandatory fields have been provided. |
| 121 | + |
| 122 | +## Annotations |
| 123 | + |
| 124 | +### @FluentBuilder |
| 125 | +Place on a record to generate a fluent builder for it. |
| 126 | + |
| 127 | +### @FluentBuilder.Mandatory |
| 128 | +Mark record components that must be set before `build()` can be called. These fields enforce compile-time type safety. |
| 129 | + |
| 130 | +### @FluentBuilder.Ignore |
| 131 | +Mark record components to exclude them from the builder entirely. Ignored fields will be set to `null` when the record is constructed. Useful for: |
| 132 | +- Internal/computed fields |
| 133 | +- Fields set by factory methods |
| 134 | +- Metadata fields not relevant during construction |
| 135 | + |
| 136 | +## Example Generated Code |
| 137 | + |
| 138 | +For the `Person` record above, the processor generates a `PersonBuilder` class with multiple nested builder classes. The type system ensures you can only call `build()` after setting all three mandatory fields (firstName, lastName, age). |
| 139 | + |
| 140 | +## Building the Project |
| 141 | + |
| 142 | +```bash |
| 143 | +mvn clean install |
| 144 | +``` |
| 145 | + |
| 146 | +## Running the Example |
| 147 | + |
| 148 | +```bash |
| 149 | +cd fluent-builder-example |
| 150 | +mvn exec:java -Dexec.mainClass="com.example.Example" |
| 151 | +``` |
| 152 | + |
| 153 | +## Comparison with Original Manual Implementation |
| 154 | + |
| 155 | +See `src/main/java/Main.java` for the original manual implementation that inspired this library. The annotation processor automates the generation of this pattern for any record. |
0 commit comments