« Return to Thread: ArrayList.propertyMissing ?
Thanks, but what I'd like is exactly the opposite : in my DSL, I need to be
able to override the default propertyMissing method. It seems to be
impossible, but I may be wrong.
>> ?<http://www.nabble.com/ArrayList.propertyMissing---tp19536068p19536068.html>
Roshan Dawrani wrote:
>
> 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<http://www.nabble.com/groovy---user-f11867.html>at Nabble.com.
>>
>
>
--
View this message in context: http://www.nabble.com/ArrayList.propertyMissing---tp19536068p19538228.html
Sent from the groovy - user mailing list archive at Nabble.com.---------------------------------------------------------------------
To unsubscribe from this list, please visit:
http://xircles.codehaus.org/manage_email
« Return to Thread: ArrayList.propertyMissing ?
| Free embeddable forum powered by Nabble | Forum Help |