Uploaded image for project: 'Groovy'
  1. Groovy
  2. GROOVY-2503 MOP 2.0 design inflluencing issues
  3. GROOVY-2599

unable to overwrite toString() via MOP in some cases

    XMLWordPrintableJSON

Details

    • Sub-task
    • Status: Open
    • Major
    • Resolution: Unresolved
    • 1.5.4
    • None
    • groovy-runtime
    • None

    Description

      If toString is overridden via the metaClass then "${obj}" != obj.toString().

      Test case below

      class DateTest extends GroovyTestCase {
      
          void testToString(){
      
              Date.metaClass.toString = { ->
                  'boogie'
              }
             
              def date = new Date()
              assertEquals('boogie', date.toString())
              assertEquals( date.toString(), "${date}") //fails
          }
      }
      

      Paul King gave this pointer as to where the problem is:

      "Yes, the final line of InvokerHelper.format() should be something like:

      return (String) invokeMethod(arguments, "toString", EMPTY_ARGS);
      //return arguments.toString();

      to make your example work."

      Attachments

        Issue Links

          Activity

            People

              Unassigned Unassigned
              leebutts Lee Butts
              Votes:
              9 Vote for this issue
              Watchers:
              10 Start watching this issue

              Dates

                Created:
                Updated: