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

Cannot create static property via metaClass

    XMLWordPrintableJSON

Details

    • Sub-task
    • Status: Open
    • Major
    • Resolution: Unresolved
    • 1.7.6
    • None
    • groovy-runtime
    • None
    • Linux, Fedora 13, Vaio F12

    Description

      As per this thread on Groovy User:
      http://groovy.329449.n5.nabble.com/Cannot-create-static-property-via-metaClass-td3792536.html#a3832577

      I am unable to do the following:
      class Foo {}
      Foo.metaClass.static.bar = 1
      Foo.bar // error, no such property bar for class Foo

      Suggested non-working workaround:
      class Foo {}
      Foo.metaClass.static.getBar =

      { 1 }


      Foo.metaClass.static.setBar =

      { Integer x->bar=x }


      println Foo.bar // 1
      Foo.bar = 5
      println Foo.bar // obviously, still 1 since static bar property is never created, just creating static getter/setter for a non-existent foo

      Interestingly, running above code once, and then swapping getBar=

      {1}

      with getBar=

      {bar}

      , the code works and one can overwrite the initial value.

      On a side note: when we are able to create an MCL static properties, what will the scope be? i.e. same as
      class Foo {
      static bar
      }

      I ask because I am unsure where groovy MOP and JVM garbage collection come into play. I stumbled across this problem as I was attempting to create a per request singleton (via MCL), as opposed to application/instance lifetime scoped with
      class Foo {
      private static final bar = new Thing()
      }

      Thanks for evolving Groovy, now & the future are very bright.

      Attachments

        Activity

          People

            Unassigned Unassigned
            virtualeyes James Lang
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated: