Groovy
  1. Groovy
  2. GROOVY-3283

Can't "import static" more than one enum

    Details

    • Type: Bug Bug
    • Status: Closed
    • Priority: Minor Minor
    • Resolution: Fixed
    • Affects Version/s: 1.5.7
    • Fix Version/s: 1.6-rc-2, 1.5.8, 1.7-beta-1
    • Component/s: Compiler
    • Labels:
      None
    • Environment:
      Windows, XP, JRE 1.6.0

      Description

      Trying to use "import static Foo.*" for more than one enum throws on use even though the names don't collide. It's behaving like "with".

       
      enum Foo { A,B }
      enum Bar { X,Y }
      
      import static Foo.*
      import static Bar.*
      
      a = A
      x = X
      

      throws:

       
      java.lang.ExceptionInInitializerError
      	at Script146.class$(Script146)
      	at Script146.run(Script146:9)
      Caused by: org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object 'X' with class 'Foo' to class 'Bar'
      	at Bar.<clinit>(Script146)
      	... 2 more
      

        Activity

        Chris Currivan created issue -
        Roshan Dawrani made changes -
        Field Original Value New Value
        Assignee Roshan Dawrani [ roshandawrani ]
        Hide
        Roshan Dawrani added a comment -

        Since enums don't have constructors that can be called from outside, enum classes expose method $INIT as a constructor helper via which enum constructors are called and enum values are created.

        And then StaticImportVisitor has this logic to find imported static methods by name and argument types.

        Since all enum classes have these standard static methods like $INIT()/values() when enum values in the 2nd enum Bar are being created using static call $INIT(

        {X, 0}

        ), StaticImportVisitor, starts searching for $INIT in all static imports and finds it in the first static import Foo itself (as $INIT is a standard method) and because of that enum value for Bar gets typecasted to Foo mistakenly and throws this type-casting error that has been reported.

        Made the changes in StaticImportVisitor to invoke $INIT of the correct static-imported enum in such a case.

        Fixed it on branches 1.5.x, 1.6.x and trunk.

        Show
        Roshan Dawrani added a comment - Since enums don't have constructors that can be called from outside, enum classes expose method $INIT as a constructor helper via which enum constructors are called and enum values are created. And then StaticImportVisitor has this logic to find imported static methods by name and argument types. Since all enum classes have these standard static methods like $INIT()/values() when enum values in the 2nd enum Bar are being created using static call $INIT( {X, 0} ), StaticImportVisitor, starts searching for $INIT in all static imports and finds it in the first static import Foo itself (as $INIT is a standard method) and because of that enum value for Bar gets typecasted to Foo mistakenly and throws this type-casting error that has been reported. Made the changes in StaticImportVisitor to invoke $INIT of the correct static-imported enum in such a case. Fixed it on branches 1.5.x, 1.6.x and trunk.
        Roshan Dawrani made changes -
        Fix Version/s 1.6 [ 13832 ]
        Status Open [ 1 ] Resolved [ 5 ]
        Fix Version/s 1.5.8 [ 14630 ]
        Fix Version/s 1.7-beta-1 [ 14014 ]
        Resolution Fixed [ 1 ]
        Paul King made changes -
        Status Resolved [ 5 ] Closed [ 6 ]
        Mark Thomas made changes -
        Project Import Sun Apr 05 13:32:57 UTC 2015 [ 1428240777691 ]
        Mark Thomas made changes -
        Workflow jira [ 12732280 ] Default workflow, editable Closed status [ 12744086 ]
        Mark Thomas made changes -
        Project Import Mon Apr 06 02:11:23 UTC 2015 [ 1428286283443 ]
        Mark Thomas made changes -
        Workflow jira [ 12970115 ] Default workflow, editable Closed status [ 12977869 ]
        Transition Time In Source Status Execution Times Last Executer Last Execution Date
        Open Open Resolved Resolved
        1d 4h 7m 1 Roshan Dawrani 16/Jan/09 14:39
        Resolved Resolved Closed Closed
        6d 6h 55m 1 Paul King 22/Jan/09 21:34

          People

          • Assignee:
            Roshan Dawrani
            Reporter:
            Chris Currivan
          • Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved:

              Development