Skip to content

Instantly share code, notes, and snippets.

Last active August 29, 2015 14:00
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
@builder AST Strategies sample
/* Sample use cases of @Builder AST which is added in Groovy 2.3 */
import groovy.transform.builder.*
import groovy.transform.ToString
class Person {
String name
int age
//Uses field names as builder methods
println Person.builder().name( 'Jack' ).age( 10 ).build()
//DefaultStrategy with prefix
@Builder(builderStrategy=DefaultStrategy, prefix='make')
class Hotel {
String name
int noOfRooms
//Uses prefixed method names
println Hotel.builder().makeName( 'MGM' ).makeNoOfRooms( 100 ).build()
//SimpleStrategy with prefix
@Builder(builderStrategy=SimpleStrategy, prefix='with')
class Aircraft {
String type
int rank
//Uses prefix but initialization is self contained
println new Aircraft().withType( 'Citation X' ).withRank( 7 )
class Airport {
String name
String code
//Uses an initializer as a construct
println new Airport( Airport.createInitializer()
.name( 'John F Kennedy International Airport' )
.code( 'JFK' ) )
//Uses a separate Builder class externally
class Car {
String make
String category
String type
//Explicit Builder Class for Car
@Builder(builderStrategy=ExternalStrategy, forClass=Car)
class CarBuilder { }
//User builder class
println new CarBuilder().make( 'Audi' ).category( 'Luxury' ).type( 'Sedan' ).build()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment