groovy
  1. groovy
  2. GROOVY-4241

Problems with the 'as' operator

    Details

    • Type: Bug Bug
    • Status: Closed Closed
    • Priority: Major Major
    • Resolution: Fixed
    • Affects Version/s: 1.7.2
    • Fix Version/s: 1.7.3, 1.6.9, 1.8-beta-1
    • Component/s: None
    • Labels:
      None
    • Environment:
      Mac OSX 10.6 with Java 1.6
    • Testcase included:
      yes
    • Number of attachments :
      3

      Description

      The following code reproduce the error:

      class Foo {}

      Foo.metaClass.invokeMethod = { String name, args ->
      for (arg in args)

      { arg.getClass() }

      }

      def f = new Foo()

      f.echo(f as Foo)

      1. v18x_4241_Patch.txt
        2 kB
        Roshan Dawrani
      2. v18x_4241B.patch
        0.5 kB
        Paul King
      3. v2_v18x_4241_Patch.txt
        1 kB
        Roshan Dawrani

        Activity

        Hide
        Roshan Dawrani added a comment -

        Recording here the issue originally raised by Paulo on the mailing list

        class Foo {
            def echo() { println 10 }
        }
        
        class Bar {}
        
        Foo.metaClass.invokeMethod = { String name, args ->
            for (arg in args) {
                println arg.getClass()
            }
        }
        
        def f = new Foo()
        
        // This works
        def bar = [key: 'foo'] as Bar
        f.echo(bar)
        
        // This throws a NullPointerException
        f.echo([key: 'foo'] as Bar)
        

        The example raised in the JIRA is the shortened version of the original issue but I think it's not the same because in my shortened example "asType" call also goes through overridden invokeMethod and since that does not delegate any further to do the actual asType, the shortened version is a little contrived.

        So, the real issue to solve is the one here in the comment (raised originally on the mailing list by Paulo).

        Show
        Roshan Dawrani added a comment - Recording here the issue originally raised by Paulo on the mailing list class Foo { def echo() { println 10 } } class Bar {} Foo.metaClass.invokeMethod = { String name, args -> for (arg in args) { println arg.getClass() } } def f = new Foo() // This works def bar = [key: 'foo'] as Bar f.echo(bar) // This throws a NullPointerException f.echo([key: 'foo'] as Bar) The example raised in the JIRA is the shortened version of the original issue but I think it's not the same because in my shortened example "asType" call also goes through overridden invokeMethod and since that does not delegate any further to do the actual asType, the shortened version is a little contrived. So, the real issue to solve is the one here in the comment (raised originally on the mailing list by Paulo).
        Hide
        Roshan Dawrani added a comment -

        Attaching a patch for review.

        Show
        Roshan Dawrani added a comment - Attaching a patch for review.
        Hide
        Paul King added a comment -

        Alternative patch - should be run against Roshan's test.

        Looks to me like Wrapper could do with some cleanup. Remove all the commented out code which is effectively in the subclasses now and probably remove the protected field too (I suspect no one is using it but to be sure, I would deprecate in 1.7.3 first then remove in 1.8.0).

        Show
        Paul King added a comment - Alternative patch - should be run against Roshan's test. Looks to me like Wrapper could do with some cleanup. Remove all the commented out code which is effectively in the subclasses now and probably remove the protected field too (I suspect no one is using it but to be sure, I would deprecate in 1.7.3 first then remove in 1.8.0).
        Hide
        Roshan Dawrani added a comment -

        I have tested the other patch as well and that with the test is again attached for review.

        Clean-up not touched yet just to quickly get the fix reviewed and have the issue reported out of the way.

        Show
        Roshan Dawrani added a comment - I have tested the other patch as well and that with the test is again attached for review. Clean-up not touched yet just to quickly get the fix reviewed and have the issue reported out of the way.
        Hide
        Paul King added a comment -

        I did the trivial cleanup items but not the change on the protected field nor anything related this patch of course. We can do any remaining cleanup after the patch if needed.

        Show
        Paul King added a comment - I did the trivial cleanup items but not the change on the protected field nor anything related this patch of course. We can do any remaining cleanup after the patch if needed.
        Hide
        Roshan Dawrani added a comment -

        Done.

        It's also fixed on 1.6.9 as it seemed to me like a small bug fix - if anyone thinks otherwise, please let me know.

        Show
        Roshan Dawrani added a comment - Done. It's also fixed on 1.6.9 as it seemed to me like a small bug fix - if anyone thinks otherwise, please let me know.

          People

          • Assignee:
            Roshan Dawrani
            Reporter:
            Paulo Poiati
          • Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved: