Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migrated to Confluence 5.3
Excerpt
hiddentrue

Adding or overriding properties

Adding properties

Properties can be added in a couple of ways. Firstly you can use the instance method syntax seen previously:

Code Block

class Book {
  String title
}
Book.metaClass.getAuthor << {-> "Stephen King" }

def b = new Book("The Stand")

assert "Stephen King" == b.author

In this case the property is dictated by the closure and is a read-only property. You can add the equivalent setter but then remember you will have to store the property somewhere for retrieval later so make sure you use thread safe code. For example you could store values in a synchronized Map using the object identity as the key:

Code Block

def properties = Collections.synchronizedMap([:])

Book.metaClass.setAuthor = { String value ->
   properties[System.identityHashCode(delegate) + "author"] = value
}
Book.metaClass.getAuthor = {->
   properties[System.identityHashCode(delegate) + "author"]
}

...

Alternatively you can simply assign a value as follows:

Code Block

Book.metaClass.author = "Stephen King"
def b = new Book("The Stand")

assert "Stephen King" == b.author

In this case the property is mutable and has both a setter and getter.

Warning

However, using this technique the property is stored in a ThreadLocal, WeakHashMap so don't expect the value to stick around forever!