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

Reading static metaClass property of a class replaces existing metaClass in registry

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Fixed
    • 1.1-rc-2
    • 1.1-rc-3
    • groovy-jdk
    • None
    • Windows XP
      Java 1.6
      built from subversion on 11/14

    Description

      When accessing the static metaClass method on a class, if it has a Custom MetaClass defined in the registry, it is overridden with an ExpandoMetaClass. This also affects already instantiated instances of the class.

      If we call "println CustomMetaClassTest.metaClass", the existing CustomMetaClassTestMetaClass is replaced in the registry by an ExpandoMetaClass. Specfically, the static method GroovyDefaultMethods.getMetaClass(class c) creates an ExpandoMetaClass, and registers it, replacing whatever metaClass was already there.

      Example problem scenario:
      I create a custom metaclass for a legacy Java class, to make it more 'Groovy'-like. A user creates an instance of the Java class, and is using it. During the execution of his application, he calls "println LegacyClass.metaClass" so he can see which metaClass is being used. My custom metaClass is replaced by an expandoMetaclass, during runtime, and now method and property calls that my custom metaClass handled before the static call start to fail without warning.

      I have added a test case to the CustomMetaClassTest to demonstrate this issue
      ----------------------------------
      Method added to test case (revision 9152)
      The last 2 asserts currently fail
      ----------------------------------
      void testStaticMetaClass ()

      { assertTrue metaClass instanceof groovy.runtime.metaclass.groovy.bugs.CustomMetaClassTestMetaClass assertEquals MetaClassImpl, metaClass.delegate.class def staticMetaClass = CustomMetaClassTest.metaClass assertTrue staticMetaClass instanceof groovy.runtime.metaclass.groovy.bugs.CustomMetaClassTestMetaClass assertTrue metaClass instanceof groovy.runtime.metaclass.groovy.bugs.CustomMetaClassTestMetaClass }

      Attachments

        1. CustomMetaClassTest.groovy
          3 kB
          Adam Rinehart

        Activity

          People

            ait Alex Tkachman
            johannz Adam Rinehart
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: