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

getAt as Operator Throws if given Fixed and Variable Arguments

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Open
    • Minor
    • Resolution: Unresolved
    • 2.3.6, 2.4.0-beta-2
    • None
    • None
    • RHEL 6.5 x64

    Description

      The getAt method for indexing fails when variable arguments are used with [] if any fixed arguments are present. For example:

      class Test
      {
         def getAt(String s) { return s }
         def getAt(String s, Integer x) { return "$s $x" }   
         // def getAt(Object... o) { return o }   // This would succeed
         def getAt(String s, Object... a) { return "$s $a" }
      
         static void main(String[] args)
         {
            Test t = new Test()
            assert t['a'] == 'a'
            assert t['b', 2] == 'b 2'
            assert t.getAt('c', 3, true) == 'c [3, true]'
            assert t['c', 3, true] == 'c [3, true]'   // This throws an exception
         }
      }
      

      The above produces:

      Exception thrown
      java.lang.IllegalArgumentException: wrong number of arguments
      	at Test.main(ConsoleScript42:14)
      

      Workaround: do not use fixed and variable arguments at the same time and use only variable arguments as in the case shown commented out above: getAt(Object... o). This is less-than desirable, however, because it restricts the user from using method overloading to separate out distinctly-different logic and forces the user to do runtime checks and implement control structure when using the single varargs method. In this case, however, the bug severity is mitigated by the fact that the user can explicitly use the "getAt" invocation instead of using the [ ] operator notation.

      Attachments

        1. Test.groovy
          0.5 kB
          Steve Amerige

        Activity

          People

            Unassigned Unassigned
            amerige Steve Amerige
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

              Created:
              Updated: