groovy
  1. groovy
  2. GROOVY-4964

static import overrides explicit class name when getting property

    Details

    • Type: Bug Bug
    • Status: Closed Closed
    • Priority: Minor Minor
    • Resolution: Fixed
    • Affects Version/s: 1.7.8, 1.8.1
    • Fix Version/s: 1.8.2, 1.9-beta-3
    • Component/s: None
    • Labels:
      None
    • Testcase included:
      yes
    • Number of attachments :
      0

      Description

      When I static import a method, it takes precedence over a call to the method explicitly on another class, if I get a property from the result.

      $ cat Foo.groovy 
      class Foo {
          static doIt() { [k: 'foo'] }
      }
      
      jbeutel@jbeutel-mac:~/proj/groovySandboxes/staticImports (master *)
      $ cat Bar.groovy 
      import static Foo.*
      class Bar {
          static doIt() { [k: 'bar'] }
          static doAssert() {
              assert doIt().k == 'foo'
              assert doIt() == [k: 'foo']
              assert Bar.doIt() == [k: 'bar']
              assert Bar.doIt().k == 'bar'
          }
      }
      
      jbeutel@jbeutel-mac:~/proj/groovySandboxes/staticImports (master *)
      $ groovy -e 'Bar.doAssert()'
      Caught: Assertion failed: 
      
      assert Bar.doIt().k == 'bar'
                        | |
                        | false
                        foo
      
      Assertion failed: 
      
      assert Bar.doIt().k == 'bar'
                        | |
                        | false
                        foo
      
      	at Bar.doAssert(Bar.groovy:8)
      	at Bar$doAssert.call(Unknown Source)
      	at script_from_command_line.run(script_from_command_line:1)
      
      jbeutel@jbeutel-mac:~/proj/groovySandboxes/staticImports (master *)
      $ groovy --version
      Groovy Version: 1.8.1 JVM: 1.6.0_26
      

      This was a problem for me when statically importing (with wildcards) a bunch of enum classes into another enum.

      Work-around: assign the method result to a temporary variable before getting a property from it.

        Activity

        blackdrag blackdrag made changes -
        Field Original Value New Value
        Assignee Paul King [ paulk ]
        Paul King made changes -
        Status Open [ 1 ] Resolved [ 5 ]
        Fix Version/s 1.8.2 [ 17495 ]
        Fix Version/s 1.9-beta-2 [ 17494 ]
        Resolution Fixed [ 1 ]
        Paul King made changes -
        Status Resolved [ 5 ] Closed [ 6 ]

          People

          • Assignee:
            Paul King
            Reporter:
            J. David Beutel
          • Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved: