A free, open-source Java Annotation Processor for generating Builder classes.
From code like this:
public class Foo {
// ...
public Foo(int id, float ratio, String name, boolean enable) {
// ...
}
// ...
}... generate a builder that can be used like this:
new FooBuilder().withId(5).withName("foo").withRatio(0.5f).create();... by adding a single line:
public class Foo {
// ...
@GenerateBuilder // magic!
public Foo(int id, float ratio, String name, boolean enable) {
// ...
}
// ...
}Annotation Processing has become standard since Java 1.6. Adding BuilderGen to your build hence is very simple:
BuilderGen is deployed to the Maven Central Repository. Add the following to the dependencies section of your pom.xml:
<dependency>
<groupId>com.github.misberner.buildergen</groupId>
<artifactId>buildergen</artifactId>
<!-- Make this dependency non-transitive -->
<scope>provided</scope>
<version>0.1</version>
</dependency>If you are using maven-compiler-plugin of at least version 2.2, your builders will be automatically generated (into src/target/generated-sources/annotations) and compiled.
- Download the latest stable version.
- Make sure the JAR file is in your classpath during compilation (e.g., using an appropriate
classpathrefspecification for thejavactarget in Ant). - Enjoy!
- Usage guide in the Wiki (TBD)
- Javadoc
- Maven Project Site
- Builder generation from constructors or static methods
- Handles generic type parameters of both instance classes and constructor methods
- Respects
throwsspecification of constructor or static method - Highly configurable
- Very lightweight: only depends on StringTemplate
BuilderGen is distributed under the terms of the Apache License, Version 2.0.
Created and developed by Malte Isberner.