Groovy
  1. Groovy
  2. GROOVY-5358

Different ways of extending a class functionality esp. adding getProperty inconsistent

    Details

    • Type: Bug Bug
    • Status: Open
    • Priority: Minor Minor
    • Resolution: Unresolved
    • Affects Version/s: None
    • Fix Version/s: None
    • Component/s: groovy-runtime
    • Labels:
      None

      Description

      When getProperty is used e.g., to mimic a Map behaviour, the result differs depending on whether getProperty was defined directly in the class, added through a mixin, or through a metaclass.

      class FooWorksAsMap { // class names the same length as LinkedHashMap to make output pretty
       def getProperty(String foo) { "OK:FooWorksAsMap.$foo" }
      }
      class BarWorksAsMap {}
      class BaxWorksAsMap {}
      @Category(BarWorksAsMap) class C {
       def getProperty(String foo) { "OK:BarWorksAsMap.$foo" }
      }
      BarWorksAsMap.mixin C
      BaxWorksAsMap.metaClass.getProperty = { foo -> "OK:BaxWorksAsMap.$foo" }
      def maps = [new FooWorksAsMap(), new BarWorksAsMap(), new BaxWorksAsMap(),
                 [foo:'OK:LinkedHashMap.foo', class:'OK:LinkedHashMap.class']]
      for (def prop in ['foo','class']) {
       for (def m in maps) {
         def op = "${m.getClass().getSimpleName()}.$prop"
         try { println "$op -> " + m."$prop" }
         catch (t) { println "$op -> FAIL:$t" }
       }
      }
      

        Activity

        Mark Thomas made changes -
        Workflow jira [ 12971791 ] Default workflow, editable Closed status [ 12979577 ]
        Mark Thomas made changes -
        Project Import Mon Apr 06 02:11:23 UTC 2015 [ 1428286283443 ]
        Mark Thomas made changes -
        Workflow jira [ 12734281 ] Default workflow, editable Closed status [ 12746079 ]
        Mark Thomas made changes -
        Project Import Sun Apr 05 13:32:57 UTC 2015 [ 1428240777691 ]
        Pascal Schumacher made changes -
        Description When getProperty is used e.g., to mimic a Map behaviour, the result differs depending on whether getProperty was defined directly in the class, added through a mixin, or through a metaclass.

        ===
        class FooWorksAsMap { // class names the same length as LinkedHashMap to make output pretty
         def getProperty(String foo) { "OK:FooWorksAsMap.$foo" }
        }
        class BarWorksAsMap {}
        class BaxWorksAsMap {}
        @Category(BarWorksAsMap) class C {
         def getProperty(String foo) { "OK:BarWorksAsMap.$foo" }
        }
        BarWorksAsMap.mixin C
        BaxWorksAsMap.metaClass.getProperty = { foo -> "OK:BaxWorksAsMap.$foo" }
        def maps = [new FooWorksAsMap(), new BarWorksAsMap(), new BaxWorksAsMap(),
                   [foo:'OK:LinkedHashMap.foo', class:'OK:LinkedHashMap.class']]
        for (def prop in ['foo','class']) {
         for (def m in maps) {
           def op = "${m.getClass().getSimpleName()}.$prop"
           try { println "$op -> " + m."$prop" }
           catch (t) { println "$op -> FAIL:$t" }
         }
        }
        ===
        When getProperty is used e.g., to mimic a Map behaviour, the result differs depending on whether getProperty was defined directly in the class, added through a mixin, or through a metaclass.

        {code}
        class FooWorksAsMap { // class names the same length as LinkedHashMap to make output pretty
         def getProperty(String foo) { "OK:FooWorksAsMap.$foo" }
        }
        class BarWorksAsMap {}
        class BaxWorksAsMap {}
        @Category(BarWorksAsMap) class C {
         def getProperty(String foo) { "OK:BarWorksAsMap.$foo" }
        }
        BarWorksAsMap.mixin C
        BaxWorksAsMap.metaClass.getProperty = { foo -> "OK:BaxWorksAsMap.$foo" }
        def maps = [new FooWorksAsMap(), new BarWorksAsMap(), new BaxWorksAsMap(),
                   [foo:'OK:LinkedHashMap.foo', class:'OK:LinkedHashMap.class']]
        for (def prop in ['foo','class']) {
         for (def m in maps) {
           def op = "${m.getClass().getSimpleName()}.$prop"
           try { println "$op -> " + m."$prop" }
           catch (t) { println "$op -> FAIL:$t" }
         }
        }
        {code}
        Jochen Theodorou made changes -
        Field Original Value New Value
        Component/s groovy-runtime [ 16250 ]
        OC created issue -

          People

          • Assignee:
            Unassigned
            Reporter:
            OC
          • Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

            • Created:
              Updated:

              Development