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

@Builder should have an option to include superclass properties

    XMLWordPrintableJSON

Details

    • Improvement
    • Status: Closed
    • Major
    • Resolution: Fixed
    • None
    • 2.5.0-alpha-1
    • xforms
    • None

    Description

      If one annotates a groovy class with @Builder and that class extends from another class, then the generated builder does not support setting the parent class properties.

      This is especially problematic when mixin groovy builders in java code.

      e.g. the following class shows what will and will not compile

      // Animal.groovy
      import groovy.transform.builder.Builder
      import groovy.transform.builder.SimpleStrategy
      
      @Builder(builderStrategy = SimpleStrategy)
      class Animal {
      
         String color
          int legs
      }
      
      // Pet.groovy
      import groovy.transform.builder.Builder
      import groovy.transform.builder.SimpleStrategy
      
      @Builder(builderStrategy = SimpleStrategy)
      class Pet extends Animal {
      
          String name
      }
      
      // PetTest.java
      import org.junit.Test;
      import static org.junit.Assert.*;
      
      public class PetTest {
          @Test public void createPet() {
              // Pet pet = new Pet().setColor("white").setLegs(4).setName("Bobby"); does not compile
              Pet pet = (Pet) new Pet().setName("Bobby").setColor("white").setLegs(4);
      
              assertTrue(pet.getLegs() == 4);
          }
      }
      

      Attachments

        Activity

          People

            pascalschumacher Pascal Schumacher
            controlix Marc Bogaerts
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: