groovy
  1. groovy
  2. GROOVY-3252

Fix DGM toString for Map and Collection

    Details

    • Type: Improvement Improvement
    • Status: Closed Closed
    • Priority: Major Major
    • Resolution: Fixed
    • Affects Version/s: 1.6-rc-1
    • Fix Version/s: 1.6-rc-2
    • Component/s: None
    • Labels:
      None
    • Testcase included:
      yes
    • Patch Submitted:
      Yes
    • Number of attachments :
      1

      Description

      In Groovy code, the toString() method does not produce a Groovy formatted string for Map or Collection.

      The problem is that the toString(Map) and toString(Collection) methods can't work because they're declared on interfaces and DGM methods must be declared on an implementation class in order to be dispatched.

        Activity

        Hide
        Jim White added a comment -

        Turns out we really need to make Object.toString() a metamethod.

        If we don't then if someone implements a Map or Collection that doesn't not extend AbstractMap or AbstractCollection, then any time Groovy converts it to String internally (like {{"$

        {mymap}

        "}} or [mycoll].join() then we'll use our own formatter. But if they call toString() on their object then they'll get either Object.toString() or their own method if they have one.

        This is actually probably a bug and may mean we need to do that instanceof Map/Collection differently.

        Show
        Jim White added a comment - Turns out we really need to make Object.toString() a metamethod. If we don't then if someone implements a Map or Collection that doesn't not extend AbstractMap or AbstractCollection , then any time Groovy converts it to String internally (like {{"$ {mymap} "}} or [mycoll] .join() then we'll use our own formatter. But if they call toString() on their object then they'll get either Object.toString() or their own method if they have one. This is actually probably a bug and may mean we need to do that instanceof Map/Collection differently.
        Hide
        Jim White added a comment - - edited

        Additional Map or Collection classes that don't extend AbstractMap/Collection:

        Collection:

        • java.beans.beancontext.BeanContextSupport
        • java.beans.beancontext.BeanContextServicesSupport
        • java.util.concurrent.CopyOnWriteArrayList

        Map:

        • java.util.jar.Attributes
        • java.util.Hashtable
        • java.awt.RenderingHints
        • javax.management.openmbean.TabularDataSupport

        I'll open another JIRA on this more general problem.

        Show
        Jim White added a comment - - edited Additional Map or Collection classes that don't extend AbstractMap/Collection: Collection: java.beans.beancontext.BeanContextSupport java.beans.beancontext.BeanContextServicesSupport java.util.concurrent.CopyOnWriteArrayList Map: java.util.jar.Attributes java.util.Hashtable java.awt.RenderingHints javax.management.openmbean.TabularDataSupport I'll open another JIRA on this more general problem.
        Hide
        Jim White added a comment -

        A case demonstrating that we really need Object.toString() to be a metamethod (at least as the code is now) is to try out "this.binding.variables.toString()" in groovyConsole. A result of "this.binding.variables" is fine!

        Committed the change (already on trunk - cs14873) to 1.6 branch (cs14946).

        Show
        Jim White added a comment - A case demonstrating that we really need Object.toString() to be a metamethod (at least as the code is now) is to try out " this.binding.variables.toString() " in groovyConsole. A result of " this.binding.variables " is fine! Committed the change (already on trunk - cs14873) to 1.6 branch (cs14946).
        Hide
        Guillaume Laforge added a comment -

        So, what's the status of this issue now?

        Show
        Guillaume Laforge added a comment - So, what's the status of this issue now?
        Hide
        Jim White added a comment -

        I had held off marking this resolved because I noticed a comment about DefaultGroovyStaticMethods and it makes me think some of these methods in DGM are supposed to be in there.

        But things seem to be working, and if that (DGSM) is an issue, we can open one for that.

        Show
        Jim White added a comment - I had held off marking this resolved because I noticed a comment about DefaultGroovyStaticMethods and it makes me think some of these methods in DGM are supposed to be in there. But things seem to be working, and if that (DGSM) is an issue, we can open one for that.

          People

          • Assignee:
            Jim White
            Reporter:
            Jim White
          • Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved: