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

Static import causes unresolved reference to become resolved

    XMLWordPrintableJSON

    Details

    • Type: Bug
    • Status: Open
    • Priority: Major
    • Resolution: Unresolved
    • Affects Version/s: 2.5.10
    • Fix Version/s: None
    • Labels:
      None

      Description

      com/foo/Person.groovy
      package com.foo
      
      @groovy.transform.builder.Builder
      class Person {
          String name
      }
      

      1.

      Main.groovy
      import com.foo.Person
      
      class Main {
          static void main(String[] args) {
              Person.PersonBuilder pb = Person.builder() 
              println(pb.build())
          }
      }
      

      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

      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())
          }
      }
      

      The code compiles, but fails with java.lang.NoClassDefFoundError: PersonBuilder when run.

      3. Let's add @CompileStatic

      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())
          }
      }
      

      Compilation fails with:
      Cannot assign value of type com.foo.Person$PersonBuilder to variable of type PersonBuilder and Cannot find matching method PersonBuilder#build()

        Attachments

          Activity

            People

            • Assignee:
              Unassigned
              Reporter:
              daniilo Daniil Ovchinnikov
            • Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

              • Created:
                Updated: