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

        Hide
        J. David Beutel added a comment -

        For the method calls without an explicit class, I'm not sure if the statically imported method should take precedence over the local method. But either way, I expect the last assert to succeed.

        Show
        J. David Beutel added a comment - For the method calls without an explicit class, I'm not sure if the statically imported method should take precedence over the local method. But either way, I expect the last assert to succeed.
        Hide
        Paul King added a comment -

        Should be fixed - thanks for finding the issue.

        We are still getting our CI servers switched over to git, so if you want to test in the very short term, you will need to build yourself from source. Once we have completed the switch, a snapshot jar should be available in the codehaus snapshot repo.

        Show
        Paul King added a comment - Should be fixed - thanks for finding the issue. We are still getting our CI servers switched over to git, so if you want to test in the very short term, you will need to build yourself from source. Once we have completed the switch, a snapshot jar should be available in the codehaus snapshot repo.

          People

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

            Dates

            • Created:
              Updated:
              Resolved: