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

Custom constructors added via metaclass are sometimes not cleaned up

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Fixed
    • 1.7.1
    • 1.7.2, 1.8-beta-1
    • None
    • None

    Description

      There seems to be an issue with constructors added via metaclass and cleanup:

      class MetaClassMagicTests extends GroovyTestCase {
          void testIt() {
              ExpandoMetaClass.enableGlobally()
              
              // Child.metaClass // uncomment and everything works
              
              // Save the old meta class
              def oldMetaClass = Parent.metaClass
              
              // Install a new one
              def emc = new ExpandoMetaClass(Parent, true, true)
              emc.initialize()
              GroovySystem.metaClassRegistry.setMetaClass(Parent, emc)
              
              // Install a map based constructor
              emc.constructor = { Map m -> Parent.newInstance() }
              
              // Parent constructor is used, all good
              assert new Child([:]) instanceof Parent
              
              // Reinstate the old meta class
              GroovySystem.metaClassRegistry.removeMetaClass(Parent) 
              GroovySystem.metaClassRegistry.setMetaClass(Parent, oldMetaClass)
      
              // This fails, this calls the custom constructor from above and returns an instance of Parent
              assert new Child([:]) instanceof Child
          }
          
      }
      
      class Parent { def a }
      class Child extends Parent { def b }
      

      Attachments

        1. 4069_v18x_Patch.txt
          8 kB
          Roshan Dawrani
        2. V2_4069_v18x_Patch.txt
          6 kB
          Roshan Dawrani

        Activity

          People

            roshandawrani Roshan Dawrani
            ldaley Luke Daley
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: