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

Builder class name should not contain package name

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Minor
    • Resolution: Fixed
    • Affects Version/s: 2.4.3
    • Fix Version/s: 2.4.5
    • Component/s: ast builder
    • Labels:
      None

      Description

      When creating a builder for a class not in the default package, the builder class name includes the package name. This makes the builder unsusable from java code.

      Example:

      Person.groovy
      package alfa.beta
      
      import groovy.transform.ToString
      import groovy.transform.builder.Builder
      
      @ToString
      @Builder // (builderClassName='PersonBuilder')
      class Person {
      	String name
      	String firstName
      	Date birthDate
      }
      
      Show.java
      package alfa.beta;
      
      public class Show {
      	public static void main(String[] args) {
      		Person lance = new Person();
      		lance.setFirstName("Lance");
      		lance.setName("Von Trier");
      		System.out.println("Hello from " + lance);
      
      		Person rick = Person.builder().name("Ashley").firstName("Rick").build();
      		System.out.println("Hello from " + rick + " too");
      	}
      }
      

      When trying to comilpile the java class I get a 'class not found' error:

      :compileJava
      /home/marc/dev/workspace-groovy/java-package/src/main/java/Show.java:10: error: cannot access alfa.beta.PersonBuilder
      		Person rick = Person.builder().name("Ashley").firstName("Rick").build();
      		                            ^
        class file for alfa.beta.Person$alfa.beta.PersonBuilder not found
      1 error
      :compileJava FAILED
      

      If I uncomment the (builderClassName='PersonBuilder') then the java code compiles fine.

      :compileJava
      :processResources UP-TO-DATE
      :classes
      :run
      Hello from alfa.beta.Person(Von Trier, Lance, null)
      Hello from alfa.beta.Person(Ashley, Rick, null) too
      
      BUILD SUCCESSFUL
      

      The problem boils down to the creation of the builder class name in groovy.transform.builder.DefaultStrategy and groovy.transform.builder.InitializerStrategy. Replacing the code buildee.getName() to buildee.getNameWithoutPackage() solves the problem.

        Attachments

          Activity

            People

            • Assignee:
              paulk Paul King
              Reporter:
              controlix Marc Bogaerts
            • Votes:
              1 Vote for this issue
              Watchers:
              3 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: