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

static import overrides explicit class name when getting property

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Minor
    • Resolution: Fixed
    • 1.7.8, 1.8.1
    • 1.8.2, 1.9-beta-3
    • None
    • None

    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.

      Attachments

        Activity

          People

            paulk Paul King
            david_beutel J. David Beutel
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: