Marc Bogaerts created GROOVY-7501:
-------------------------------------

             Summary: Builder class name should not contain package name
                 Key: GROOVY-7501
                 URL: https://issues.apache.org/jira/browse/GROOVY-7501
             Project: Groovy
          Issue Type: Bug
          Components: ast builder
    Affects Versions: 2.4.3
            Reporter: Marc Bogaerts
            Assignee: Guillaume Laforge
            Priority: Minor


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:
{code:title=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
}
{code}
{code:title=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");
        }
}
{code}
When trying to comilpile the java class I get a 'class not found' error:
{noformat}
: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
{noformat}
If I uncomment the (builderClassName='PersonBuilder') the the java code 
compiles fine.
{noformat}
: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
{noformat}

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.



--
This message was sent by Atlassian JIRA
(v6.3.4#6332)

Reply via email to