Uploaded image for project: 'Groovy'
  1. Groovy
  2. GROOVY-6484

Provide an AST that creates a "fluent builder" for an immutable class

    XMLWordPrintableJSON

Details

    • New Feature
    • Status: Closed
    • Major
    • Resolution: Fixed
    • None
    • 2.3.0-rc-1
    • None
    • None

    Description

      When working in Java, I've been using an Eclipse plugin to generate a builder for an immutable class with final fields in it.

      There is an example of such generated builder in Grails source code:
      https://github.com/grails/grails-core/blob/master/grails-web/src/main/groovy/org/codehaus/groovy/grails/web/pages/GroovyPageOutputStackAttributes.java#L85

      The "fluent builder" is known at least from the Effective Java 2nd ed. book by Joshua Bloch .

      Some blog posts about the builder pattern:
      http://www.javaspecialists.eu/archive/Issue163.html
      http://java.dzone.com/articles/too-many-parameters-java-1

      I'd like to have an AST that creates a "fluent builder" for an immutable class.

      For Groovy I'd prefer something like this for the syntax.

      Example of the class:

      @Immutable
      @FluentBuilder
      class Person {
         String firstName
         String lastName
      }
      

      The AST would generated a public inner class Person.Builder with setters, fluent property methods and a build method.

      Examples of code using the builder support:

      def person = new Person.Builder().with {
          firstName = 'John'
          lastName = 'Doe'
      }.build()
      

      or

      def person = new Person.Builder().firstName('John').lastName('Doe').build()
      

      or

      def person = new Person.Builder([firstName: 'John', lastName: 'Doe').build()
      

      Attachments

        Issue Links

          Activity

            People

              paulk Paul King
              lhotari Lari Hotari
              Votes:
              0 Vote for this issue
              Watchers:
              6 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: