« Return to Thread: ArrayList.propertyMissing ?

Re: ArrayList.propertyMissing ?

by roshandawrani :: Rate this Message:

Reply to Author | View in Thread

For collection objects, ".property" access works a little differently.

For the collection objects "<collection>.<someProperty>" syntax is part of what is called GPath expression language (similar to XPath for XML). It is used to navigate collections in groovy and retrieve all values of a specific attributes from all its members.

Taking an example:

ArrayList.metaClass.methodMissing = {String name, args ->    println "Ok for method missing - $name, $args" }

class MyName{def name; MyName(tmpName) {name = tmpName}}
def list = []

list.test("ok") // invokes methodMissing added on the metaClass above as expected

list << new MyName("Roshan1"); list << new MyName("Roshan2")

// following does not result in propertyMissing as list is collection and instead, it makes a list of "name" property values from all list members

println list.name


In your case, since the list is empty, list.test property also is an empty list.

Another example - "println this.class.methods.name" - return names of all the methods - by making a list of values of the "name" property on every object in the collection returned by "this.class.methods"

rgds,
Roshan

On Wed, Sep 17, 2008 at 9:58 PM, melix <cedric.champeau@...> wrote:
Hi,

I have a subclass of ArrayList for which I would like to implement propertyMissing. However, I can't figure out how to do this. Seems that Groovy ignores it, while overriding methodMissing works like a charm.

Here's an example :

ArrayList.metaClass.methodMissing = { String name, args->
	println "Ok for method missing"
}
ArrayList.metaClass.propertyMissing = { String name ->
	println "Ok for property missing"
}

def list = []
list.test("ok")
list.test
outputs the following :
Ok for method missing

Is there anything I'm missing ?

View this message in context: ArrayList.propertyMissing ?
Sent from the groovy - user mailing list archive at Nabble.com.

 « Return to Thread: ArrayList.propertyMissing ?