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

Replacing method on ExpandoMetaClass doesn't work

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Fixed
    • 1.6.7
    • 2.2.0
    • groovy-runtime
    • None
    • Groovy Shell (1.6.7, JVM: 1.5.0_20)

    Description

      Expando.metaClass.foo={-> def directBar=delegate.bar()
          println "directBar=${directBar}"    
          def invokedBar=delegate.invokeMethod('bar',null)
          println "invokedBar=$invokedBar"
      }
      
      e=new Expando()
      e.metaClass.bar={->1}
      e.foo()
      //directBar=1
      //invokedBar=1
      
      e.metaClass.bar={->2}
      e.foo()
      //directBar=1
      //invokedBar=2
      

      for some reason delegate.bar() != delegate.invokeMethod('bar',null)
      looks like at the direct call the closure got cached ...

      if the previous example continues with:

      x=new Expando()
      x.metaClass.bar={->3}
      e.foo()
      //directBar=2
      //invokedBar=2
      

      creating the new 'x' object has side effect on the 'e' objects metaclass?

      Attachments

        Activity

          People

            Unassigned Unassigned
            lalyos@yahoo.com Lajos Papp
            Votes:
            1 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: