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

MethodSelectionException when calling respondsTo()

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Open
    • Minor
    • Resolution: Unresolved
    • 4.0.6
    • None
    • Documentation
    • None
    • Seen in Groovy 4.0.6 on Linux x64 with java 17.0.5 and MacOS aarch64 in Java 19.0.1. I don't think any of that is significant. 😀

    Description

      if you have a method which takes certain parameters, and annotate it @NamedVariant, and then try a respondsTo with a null, or an empty array, you get this exception, listing both the methods that are there, rather than the expected groovy-falsehood (empty list) for no matching methods.

      This is related to 

      1. GROOVY-8248
      2. GROOVY-8660

      but is different in scope because this is about the behaviour, specifically, of MetaObjectProtocol::respondsTo, which states clearly in the documentation that:

      This method is "safe" in that it will always return a value and never throw an exception

      ... which turns out not to be true, and should be. The behaviour may not be wrong for the calls described in the other two bugs, but MetaObjectProtocol::respondsTo, specifically, should be catching this, and returning, as promised, a groovy falsehood. (probably an empty list, which is correct as neither of the methods match.)

      Attached, a small script demonstrating this, which shows the problem clearly if simply run in groovy console.

      import groovy.transform.*
      
      @NamedVariant
      def myMethod(int number, String name) {
          println "number: ${number}; name: ${name}"
      }
      
      println myMethod(42, 'Arthur')
      println myMethod(name: 'Arthur', number: 42)
      
      def result1 = respondsTo('myMethod', 42, 'Arthur')
      println "positive match: ${result1}"
      
      def result2 = respondsTo('myMethod', [number: 42, name: 'Arthur'])
      println "positive match: ${result2}"
      
      def result3 = respondsTo('myMethod', null)
      // expected an empty list, a groovy-falsehood
      

      Attachments

        1. responds-to-issue.groovy
          0.5 kB
          Rachel Greenham

        Activity

          People

            Unassigned Unassigned
            Rachel Greenham Rachel Greenham
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated: