groovy
  1. groovy
  2. GROOVY-2925

ExpandoMetaClass sometimes, but sometimes MetaClassImpl

    Details

    • Type: Bug Bug
    • Status: Closed Closed
    • Priority: Critical Critical
    • Resolution: Won't Fix
    • Affects Version/s: 1.6-beta-1
    • Fix Version/s: None
    • Component/s: None
    • Labels:
      None
    • Testcase included:
      yes
    • Number of attachments :
      0

      Description

      The type of a new instance of a class depends on whether the metaClass property on its class has been accessed before. That is just not right.

      groovy> class A {}
      groovy> println A.metaClass
      groovy> def a = new A ()
      groovy> println a.metaClass
      groovy> println A.metaClass

      groovy.lang.ExpandoMetaClass@b05c54[class A]
      groovy.lang.ExpandoMetaClass@b05c54[class A]
      groovy.lang.ExpandoMetaClass@b05c54[class A]
      groovy> class A {}
      groovy> //println A.metaClass
      groovy> def a = new A ()
      groovy> println a.metaClass
      groovy> println A.metaClass

      groovy.lang.MetaClassImpl@a7eb57[class A]
      groovy.lang.ExpandoMetaClass@2e22ed[class A]

        Activity

        Hide
        blackdrag blackdrag added a comment -

        Scott, if the object has a metaClass already, then using the property on the class won't change that. the per instance metaClass is only changed from MetaClassImpl if set to null, or not yet used and the default MetaClass registered for the class is no longer MetaClassImpl. So once it has a Metaclass the property accessor won't change it.

        Show
        blackdrag blackdrag added a comment - Scott, if the object has a metaClass already, then using the property on the class won't change that. the per instance metaClass is only changed from MetaClassImpl if set to null, or not yet used and the default MetaClass registered for the class is no longer MetaClassImpl. So once it has a Metaclass the property accessor won't change it.
        Hide
        Scott Vlaminck added a comment -

        Jochen, I understand that, I was just emphasizing Robert's and your point that "property accessors shouldn't have side effects."

        Show
        Scott Vlaminck added a comment - Jochen, I understand that, I was just emphasizing Robert's and your point that "property accessors shouldn't have side effects."
        Hide
        Marc Palmer added a comment -

        Guys this is very painful. There has to be a better way. We spent ages on this problem over the last week. We wanted to cry. ExpandoMetaClass.enableGlobally() works but... one shouldn't have to! I thought it was the new default for Groovy.

        Show
        Marc Palmer added a comment - Guys this is very painful. There has to be a better way. We spent ages on this problem over the last week. We wanted to cry. ExpandoMetaClass.enableGlobally() works but... one shouldn't have to! I thought it was the new default for Groovy.
        Hide
        blackdrag blackdrag added a comment -

        We intend to use a variant of EMC as default for Groovy 2.0, but not for 1.6 or 1.7

        Show
        blackdrag blackdrag added a comment - We intend to use a variant of EMC as default for Groovy 2.0, but not for 1.6 or 1.7
        Hide
        Robert Fischer added a comment -

        My standing recommendation is to put "ExpandoMetaClass.enableGlobally()" at the beginning of any Groovy script where you do EMC work. It's like the first four words of any Perl script – "use static; use warnings;"

        Show
        Robert Fischer added a comment - My standing recommendation is to put "ExpandoMetaClass.enableGlobally()" at the beginning of any Groovy script where you do EMC work. It's like the first four words of any Perl script – "use static; use warnings;"

          People

          • Assignee:
            blackdrag blackdrag
            Reporter:
            Robert Fischer
          • Votes:
            2 Vote for this issue
            Watchers:
            5 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved: