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

A trait that defines a static method that uses generic return types throws "BUG! Type is null"

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Major
    • Resolution: Fixed
    • Affects Version/s: 2.4.6
    • Fix Version/s: 2.4.7
    • Component/s: None
    • Labels:
      None

      Description

      If you have a trait that defines a method such as:

          static <T> T withClient(MyTrait myTrait, @DelegatesTo(MyTrait) Closure<T> callable ) {
              // no-op
          }
      

      Compilation will fail with:

      BUG! Type is null. Most probably you let a transform reuse existing ClassNodes with generics information, that is now used in a wrong context.
      

        Issue Links

          Activity

          Hide
          graemerocher1 Graeme Rocher added a comment -

          Attached example reproducing the problem

          Show
          graemerocher1 Graeme Rocher added a comment - Attached example reproducing the problem
          Hide
          graemerocher1 Graeme Rocher added a comment -

          correct example app

          Show
          graemerocher1 Graeme Rocher added a comment - correct example app
          Hide
          graemerocher1 Graeme Rocher added a comment -

          I narrowed it down to this script

          import org.codehaus.groovy.ast.ClassNode
          
          trait Foo  {
              static <T> T withClient(@DelegatesTo(Foo) Closure<T> callable ) {
                  // no-op
              }
          }
          
          cls = new GroovyClassLoader().parseClass('''
          class Bar implements Foo {}
          
          ''')
          
          new ClassNode(cls).methods
          
          Show
          graemerocher1 Graeme Rocher added a comment - I narrowed it down to this script import org.codehaus.groovy.ast.ClassNode trait Foo { static <T> T withClient(@DelegatesTo(Foo) Closure<T> callable ) { // no-op } } cls = new GroovyClassLoader().parseClass(''' class Bar implements Foo {} ''') new ClassNode(cls).methods
          Show
          graemerocher1 Graeme Rocher added a comment - This is the workaround I have applied in the Grails codebase for now https://github.com/grails/grails-data-mapping/blob/master/grails-datastore-gorm/src/main/groovy/org/grails/compiler/gorm/GlobalTraitRepairTransformation.groovy#L46
          Hide
          githubbot ASF GitHub Bot added a comment -

          GitHub user graemerocher opened a pull request:

          https://github.com/apache/groovy/pull/340

          Make sure generics are correct for generified return types

          Fixes https://issues.apache.org/jira/browse/GROOVY-7846

          You can merge this pull request into a Git repository by running:

          $ git pull https://github.com/graemerocher/incubator-groovy Groovy7846

          Alternatively you can review and apply these changes as the patch at:

          https://github.com/apache/groovy/pull/340.patch

          To close this pull request, make a commit to your master/trunk branch
          with (at least) the following in the commit message:

          This closes #340


          commit d4c9951290f033320f2a1a4f9ed848b57aa345ee
          Author: graemerocher <graeme.rocher@gmail.com>
          Date: 2016-05-27T09:26:20Z

          Make sure generics are correct for generified return types. Fixes https://issues.apache.org/jira/browse/GROOVY-7846


          Show
          githubbot ASF GitHub Bot added a comment - GitHub user graemerocher opened a pull request: https://github.com/apache/groovy/pull/340 Make sure generics are correct for generified return types Fixes https://issues.apache.org/jira/browse/GROOVY-7846 You can merge this pull request into a Git repository by running: $ git pull https://github.com/graemerocher/incubator-groovy Groovy7846 Alternatively you can review and apply these changes as the patch at: https://github.com/apache/groovy/pull/340.patch To close this pull request, make a commit to your master/trunk branch with (at least) the following in the commit message: This closes #340 commit d4c9951290f033320f2a1a4f9ed848b57aa345ee Author: graemerocher <graeme.rocher@gmail.com> Date: 2016-05-27T09:26:20Z Make sure generics are correct for generified return types. Fixes https://issues.apache.org/jira/browse/GROOVY-7846
          Hide
          githubbot ASF GitHub Bot added a comment -

          Github user asfgit closed the pull request at:

          https://github.com/apache/groovy/pull/340

          Show
          githubbot ASF GitHub Bot added a comment - Github user asfgit closed the pull request at: https://github.com/apache/groovy/pull/340
          Hide
          paulk Paul King added a comment -

          PR merged - thanks!

          Show
          paulk Paul King added a comment - PR merged - thanks!

            People

            • Assignee:
              paulk Paul King
              Reporter:
              graemerocher1 Graeme Rocher
            • Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved:

                Development