Skip to content

ltearno/builder-generator

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

20 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Builder Generator

Build Status

Builder Generator is a Java library allowing you to automatically generate builder classes for your constructors.

It uses the builder pattern described here. An advantage to this pattern is that it manages mandatory parameters in a clean and elegant way.

Builders are generated by adding the @UseBuilderGenerator annotation on the constructor you choose to be the target of the builder. Note that the generated builder class will not be part of your classes (although generated in the same package), but will call the targeted constructor of your class to build instances. Hence you have to write such a constructor in your class, and it has to be at least package protected.

On the other side, generating and maintaining code implementing the builder pattern is useful but painful and not productive.

To use the library in maven projects, declare :

	<dependency>
		<groupId>fr.lteconsulting</groupId>
		<artifactId>builder-generator</artifactId>
		<version>0.9</version>
		<scope>provided</scope> <!-- this is source generation, nothing at runtime -->
	</dependency>

To use the latest uploaded snapshot version, use 1.0-SNAPSHOT.

This is a sample of how to use Builder Generator :

Imagine you have a class with a terrible amount of parameters. Let's say a, c, and e are mandatory parameters, but the others are considered optional (meaning you probably provide an overloaded constructor falling back to a default value). It may look like this :

public class ComplexClass
{
	private String a;
	private String b;
	private String c;
	private String d;
	private String e;

	public ComplexClass(String a, String b, String c, String d, String e)
	{
		...
	}
	
	public ComplexClass(String a, String c, String e) ...
	public ComplexClass(String a, String b, String c, String e) ...
	public ComplexClass(String a, String c, String d, String e) ...
}

Builder generator allows you to instead generate a builder with the UseBuilderGenerator annotation. (You can by the way certainly remove your overloaded constructors...) :

public class ComplexClass
{
	private String a;
	private String b;
	private String c;
	private String d;
	private String e;

	@UseBuilderGenerator
	public ComplexClass(@Mandatory String a, String b, @Mandatory String c, String d, @Mandatory String e)
	{
		... // your normal constructor
	}
}

When you want to create an instance of this class, you use the generated builder :

	ComplexClass instance = ComplexClassBuilder
			.withA("this one is mandatory")
			.withC("this one too")
			.withE("all this is generated !")
			.withD("an optional parameter")
			.build();

Since the code is generated, you can even copy it in your own source and remove the annotations, but it won't be automatically maintained...

All kinds of possibilities are offered here, so may this be useful !

Note about IDE integration

This library is based on the pluggable annotation processor api (jsr-269) which is completely standard and will run with maven out of the box (although you may have to disable the incremental compilation sometimes). On the Eclipse IDE, you may need to download and activate the m2e-apt connector.

About

An automatic Builder Pattern generator for Java

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages