[ 
https://issues.apache.org/jira/browse/GROOVY-9472?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17507462#comment-17507462
 ] 

Paul King commented on GROOVY-9472:
-----------------------------------

Yes, there are definitely two sides to the situation.

On the one hand, it isn't "complicated" when you use the feature in the 
standard way. The documentation shows the standard way. In some sense, the fact 
that an inner class is used as part of the building process for some strategies 
is an internal detail that can usually be ignored. For a few of the strategies, 
you call the {{builder()}} method to get the builder and the builder has a 
{{build()}} method. For other strategies, no inner class is used or the other 
details may differ. So by design, since the inner class is an internal detail 
not mentioned in the source code anywhere, it is a class you should not be 
referencing. The {{ExternalStrategy}} is by design one which mentions the 
builder class and the preferred option when you want to use the class elsewhere.

On the other hand, the strategies align with some of the more common builder 
strategies which Java folks use and there are expectations around how they work 
and things which can be made to work if you were doing it by hand in Java. With 
that in mind, I agree it would be good to consider more sophistication in the 
resolution process.

> Static import causes unresolved reference to become resolved
> ------------------------------------------------------------
>
>                 Key: GROOVY-9472
>                 URL: https://issues.apache.org/jira/browse/GROOVY-9472
>             Project: Groovy
>          Issue Type: Bug
>          Components: Compiler, Static Type Checker
>    Affects Versions: 2.5.10
>            Reporter: Daniil Ovchinnikov
>            Assignee: Eric Milles
>            Priority: Major
>             Fix For: 4.0.0
>
>
> {code:groovy|title=com/foo/Person.groovy}
> package com.foo
> @groovy.transform.builder.Builder
> class Person {
>     String name
> }
> {code}
> 1.
> {code:groovy|title=Main.groovy}
> import com.foo.Person
> class Main {
>     static void main(String[] args) {
>         Person.PersonBuilder pb = Person.builder() 
>         println(pb.build())
>     }
> }
> {code}
> Trying to use it without a static import yields {{unable to resolve class 
> Person.PersonBuilder}}, which is another issue.
> 2. Let's add a static import
> {code:groovy|title=Main.groovy}
> import com.foo.Person
> import static com.foo.Person.PersonBuilder
> class Main {
>     static void main(String[] args) {
>         PersonBuilder pb = Person.builder()
>         println(pb.build())
>     }
> }
> {code}
> The code compiles, but fails with {{java.lang.NoClassDefFoundError: 
> PersonBuilder}} when run.
> 3. Let's add {{@CompileStatic}}
> {code:groovy|title=Main.groovy}
> import com.foo.Person
> import static com.foo.Person.PersonBuilder
> import groovy.transform.CompileStatic
> @CompileStatic
> class Main {
>     static void main(String[] args) {
>         PersonBuilder pb = Person.builder()
>         println(pb.build())
>     }
> }
> {code}
> Compilation fails with: 
>  {{Cannot assign value of type com.foo.Person$PersonBuilder to variable of 
> type PersonBuilder}} and {{Cannot find matching method PersonBuilder#build()}}



--
This message was sent by Atlassian Jira
(v8.20.1#820001)

Reply via email to