A Java Code Generator for Pojo Builders
Author: Michael Karneim
Project Homepage: http://github.com/mkarneim/pojobuilder
The PojoBuilder Generator is a Java 6 compliant annotation processor that generates a fluent builder class for POJOs (Plain Old Java Object).
The generated builder provides
Here is an example of how you could use a generated pojo builder from your code:
Contact james = new ContactBuilder()
.withSurname("Bond")
.withFirstname("James")
.withEmail("[email protected]")
.build();
Builders are quite useful, for example, to build test data, where you only want to set the relevant data properties.
For more information on
The source code located in the “src” directory is in the PUBLIC DOMAIN.
For more information please read the COPYING file.
PojoBuilder is a pure code generator. It does not add any runtime dependencies to your project.
However, PojoBuilder adds the following compile-time dependency to your project, which has its own license:
PojoBuilder is Open Source. The source code is available at http://github.com/mkarneim/pojobuilder.
For older versions and a change log please see the release history page.
PojoBuilder binaries are available for download at Sonatype OSS Maven Repository and Maven Central.
If you don’t use any build automation tool that supports maven repos,
you might want to download the pojobuilder-4.3.0-jar-with-dependencies.jar
to get PojoBuilder complete with all dependent libraries included.
The PojoBuilder generator uses an annotation processor to generate pojo builders for you.
You have the following options to trigger the code generation:
To generate a builder class for a pojo you can annotate one of its constructors with @GeneratePojoBuilder
.
Let’s have a look at the following example pojo:
public class Contact {
private final String surname;
private final String firstname;
private String email;
@GeneratePojoBuilder
public Contact(String surname, String firstname) {
this.surname = surname;
this.firstname = firstname;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getSurname() {
return surname;
}
public String getFirstname() {
return firstname;
}
}
The @GeneratePojoBuilder annotation tells the annotation processor to create a new Java source file with
the name ContactBuilder
. Have a look at ContactBuilder.java
to see the generated source code.
Please note that the constructor must be public or otherwise accessible for the generated builder,
e.g. if it’s protected the generated builder must reside in the same package.
And also note that the constructor parameter names must match the names of the pojo’s properties exactly.
An optional @ConstructorProperties annotation can be used to specify the mapping from the constructor-parameter-names
to the corresponding bean-property-names on the pojo if they differ.
public class Contact {
private final String surname;
private final String firstname;
private String email;
@GeneratePojoBuilder
@ConstructorProperties({"surname","firstname"})
public Contact(String arg1, String arg2) {
this.surname = arg1;
this.firstname = arg2;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getSurname() {
return surname;
}
public String getFirstname() {
return firstname;
}
}
If your pojo has no constructor (or a public default constructor), you can annotate its class with @GeneratePojoBuilder
.
Let’s have a look at the following example pojo:
@GeneratePojoBuilder
public class User {
private String name;
private char[] password;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public char[] getPassword() {
return password;
}
public void setPassword(char[] password) {
this.password = password;
}
}
Have a look at UserBuilder.java
to see the generated source code.
Alternatively, if you don’t have access to the pojo’s source code, or if you are no fan of annotating a pojo,
you can annotate a factory method:
public class UrlFactory {
@GeneratePojoBuilder(withName="UrlBuilder", intoPackage = "samples")
public static URL createUrl(
String protocol, String host, int port, String file, URLStreamHandler handler)
throws MalformedURLException {
return new URL(protocol, host, port, file, handler);
}
}
Have a look at UrlBuilder.java
to see the generated source code.
Please note that the factory method must be public and static.
The method parameter names must match the names of the pojo’s properties exactly.
An optional @FactoryProperties annotation can be used to specify the mapping from the factory-method-parameter-names
to the corresponding bean-property-names on the pojo if they differ.
public class FileFactory {
@GeneratePojoBuilder(intoPackage = "samples")
@FactoryProperties({"path"})
public static File createFile(String arg1) {
return new File(arg1);
}
}
Have a look at FileBuilder.java
to see the generated source code.
Beginning with PojoBuilder 4.3 you can annotate a Java 17 record type:
@GeneratePojoBuilder
public record MyRecord(int x, int y, String blah) {}
The following elements of @GeneratePojoBuilder can be used to configure the output of the code generation process.
Fluent*Builder
will becomeFluentContactBuilder
if the pojo’s name is Contact
. The default pattern is *Builder
.Visibility.PUBLIC
.*.util
will becomecom.example.util
if the pojo’s package is com.example
. The default pattern is *
.Object.class
.Address.java
, Builder.java
and AddressBuilder.java
.Void.class
, which means, that no interface should be implemented.Recipient.java
, Builder.java
and RecipientBuilder.java
.false
.InputSourceFactory.java
and InputSourceBuilder.java
.*
.CalendarFactory.java
and GregorianCalendarBuilder.java
.Player.java
, PlayerBuilder.java
, and AbstractPlayerBuilder.java
.false
.TextEmail.java
and TextEmailBuilder.java
.false
.com.google.common.base.Optional
andjava.util.Optional
introduced with Java 8. Default is Void.class
, which means,with*
.validate
method having one parameter that is compatible with the pojo’sCredentials.java
, CredentialsValidator.java
and CredentialsBuilder.java
.Task.java
and TaskBuilder.java
.""
meaning not to generate this method.Beginning with version 3, PojoBuilder supports meta-annotations. That is, you can place @GeneratePojoBuilder onto
another annotation and it will be inherited.
Advantages are:
The following example defines @AppPojo
which can applied at a class level and encapsulates the annotations from three
different sources (PojoBuilder, Lombok and JSR-305).
@GeneratePojoBuilder(withName = "Fluent*Builder")
@lombok.experimental.Value // class-level annotation from Lombok
@javax.annotation.concurrent.Immutable // class-level annotation from JSR-305
@Target({ElementType.TYPE, ElementType.ANNOTATION_TYPE})
public @interface AppPojo {
}
This can be placed onto each of your pojos:
@AppPojo
public class Contact {
public String name;
}
PojoBuilder will generate FluentContactBuilder
based on the directives inherited from the @AppPojo
annotation.
Defaults inherited from meta-annotations can be overriden by more ‘local’ @GeneratePojoBuilder
annotations:
@AppPojo
@GeneratePojoBuilder(intoPackage = "builder")
public class Contact {
public String name;
}
This will generate FluentContactBuilder
as before but into the package builder
.
The PojoBuilder wiki provides a cookbook about using the PojoBuilder Generator, e.g for building a domain-specific language for automated tests.
For some complete code examples please have a look into the src/testdata/java/samples folder.
To execute the PojoBuilder annotation processor you just need to put it into the compile-time classpath.
During runtime no libraries are required since the retention policy of PojoBuilder’s annotations is CLASS
.
Here is a list of brief descriptions about how to run PojoBuilder with
The javac
compiler will auto-detect the presence of PojoBuilder if pojobuilder-*.jar
is included in the classpath.
For example:
javac -cp pojobuilder-4.3.0-jar-with-dependencies.jar Contact.java
will generate a ContactBuilder
if Contact
is annotated with @GeneratePojoBuilder
.
For more information see the javac documentation.
Add the following to your project’s pom.xml
to configure the PojoBuilder annotation processor.
<dependency>
<groupId>net.karneim</groupId>
<artifactId>pojobuilder</artifactId>
<version>4.3.0</version>
<!-- 'provided' scope because this is only needed during compilation -->
<scope>provided</scope>
</dependency>
Notes:
${project.build.directory}/generated-sources/annotations
.target
directory, then configure the generatedSourcesDirectory
of the maven-compiler-plugin
. See the sample Maven pom for an example.This is a small build script that shows how to run the PojoBuilder annotation processor with Gradle.
apply plugin: 'java'
repositories {
mavenCentral()
}
dependencies {
compile 'net.karneim:pojobuilder:4.3.0'
}
Please note that this not only adds the PojoBuilder and its dependencies to your compile-time class path but also to your run-time class path.
Alternatively you can use the following script to add PojoBuilder only to the compile-time class path:
apply plugin: 'java'
repositories {
mavenCentral()
}
configurations {
codeGeneration
}
dependencies {
codeGeneration 'net.karneim:pojobuilder:4.3.0'
compileOnly 'net.karneim:pojobuilder:4.3.0:annotations'
}
compileJava.classpath += configurations.codeGeneration
compileTestJava.classpath += configurations.codeGeneration
In both cases the generated sources will be placed into the standard build/classes
directory.
If you want to put them somewhere else, just specify the destination like this:
compileJava.options.compilerArgs += ['-s', 'src/generated/java']
The wiki contains an extended Gradle script that distinguishes completely between code generation and compilation tasks.
There is another Gradle script that enables PojoBuilder for Eclipse IDE.
With Gradle 5.0 any annotation processor in the classpath don’t get executed anymore.
To make pojobuilder work again, replace the used dependency scope with annotationProcessor
dependencies {
annotationProcessor 'net.karneim:pojobuilder:4.3.0'
}
Here is a code snippet of some sample ANT build script that runs the PojoBuilder annotation processor within the javac
task.
<!-- Add the required libraries into this directory. -->
<fileset id="libs.fileset" dir="${basedir}/lib">
<include name="*.jar" />
</fileset>
<path id="class.path">
<fileset refid="libs.fileset" />
</path>
<target name="compile" depends="init"
description="Compile java sources and run annotation processor">
<mkdir dir="${src.gen.java.dir}" />
<mkdir dir="${build.classes.dir}" />
<javac classpathref="class.path" destdir="${build.classes.dir}">
<src path="${src.main.java.dir}" />
<!-- This tells the compiler where to place the generated source files -->
<compilerarg line="-s ${src.gen.java.dir}"/>
</javac>
</target>
You could also configure Eclipse to run the PojoBuilder annotation processor during the build cycle.
It will be invoked whenever you save files that contain sources annotated with @GeneratePojoBuilder
.
Do the following to enable PojoBuilder for your Eclipse project:
pojobuilder-4.3.0-annotations.jar
to your project classpathpojobuiler-4.3.0-jar-with-dependencies.jar
If you want to compile this project’s sources yourself you can use Gradle (see build.gradle).