Frage

I am looking for Groovy AST transformation that would generate builder pattern code inside my class.

I know there are something like @Canonnical or @ToString or @EqualsAndHashCode enhancers that automatically generate useful methods and hoped if there would be @GenerateBuilder. I want to use it something like this:

//Groovy code:
@GenerateBuilder
@CompileStatic
class Person  {
    String name
    int age
    Long id
    String createdBy
}

//then in Java code:
Person p = Person.newBuilder()
    .withName("pawel")
    .withAge(19)
    .withId(11123)
    .withCreatedBy("system")
    .build();
War es hilfreich?

Lösung

There's nothing prior to 2.3 that will do this

But groovy 2.3 has a new @Builder annotation

https://github.com/groovy/groovy-core/blob/master/src/main/groovy/transform/builder/Builder.java

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top