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

isCase() - static method invocation by way of InvokeHelper should be polymorphic

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Fixed
    • 1.0-beta-4
    • 1.0-JSR-5
    • None
    • None
    • beta 4, on XP

    Description

      switch (3..7)

      { case (3..7): println ' got it' break default: println "nope" }

      I got "Nope".

      This was due to the fact that static method invocation by way of InvokeHelper was not polymorphic but it should, shouldn't it?

      I solved the issue by adding to DefaultGroovyMethods.java

      // br
      public static boolean isCase(IntRange caseValue, Object switchValue) {
      return caseValue.equals(switchValue);
      }

      But I don't think this is the right solution since this solves only IntRange but not other Range types. A more serious problem is with polymorphism of InvokerHelper or the lack of it. If polymorphism works had worked, this method would have immediately matched:

      public static boolean isCase(Collection caseValue, Object switchValue) {
      return caseValue.contains(switchValue);
      }

      And this won't produce the right result since the logic inside the contains() seems ot be a little issues itslef too. I wish the genernic catch-all method

      public static boolean isCase(Object caseValue, Object switchValue) {
      return caseValue.equals(switchValue);
      }

      would match since it uses the equals() as the defalt comparator.

      I added one test case in DefaultGroovyMethodsTest.java like this:

      public void testIsCase()

      { IntRange ra = new IntRange(1,3); assertTrue(InvokerHelper.isCase(ra, ra)); // line 1 assertTrue(DefaultGroovyMethods.isCase(ra, ra)); // line 2 }

      The line 1 invocation is not polymorphic while line 2 is.

      Thus the whole issue.

      Bing Ran

      Attachments

        Activity

          People

            blackdrag Jochen Theodorou
            bingran Bing Ran
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: