|
View:
New views
8 Messages
—
Rating Filter:
Alert me
|
|
|
ArrayList.propertyMissing ?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 missingIs there anything I'm missing ? |
|
|
Re: ArrayList.propertyMissing ?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, |
|
|
Re: 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.
|
|
|
Re: ArrayList.propertyMissing ?Since "collection.property" syntax is used up for providing the GPath expression evaluation feature, I am not sure if you can provide propertyMissing on ArrayList's metaClass and have it called by groovy when you use "collection.anAbsentProperty" syntax.
That is my understanding. However, let's wait for more inputs on this. rgds, Roshan On Wed, Sep 17, 2008 at 11:50 PM, melix <cedric.champeau@...> wrote:
|
|
|
Re: ArrayList.propertyMissing ?melix schrieb:
> 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. does it have to be ArrayList? Cant you use a custom class? bye blackdrag -- Jochen "blackdrag" Theodorou The Groovy Project Tech Lead (http://groovy.codehaus.org) http://blackdragsview.blogspot.com/ http://www.g2one.com/ --------------------------------------------------------------------- To unsubscribe from this list, please visit: http://xircles.codehaus.org/manage_email |
|
|
Re: ArrayList.propertyMissing ?I do use a custom class (that extends ArrayList) and it doesn't work either. Seems like the propertyMissing method that is used is always the default Groovy one. Looks like a bug to me, but I'd like to be sure ;)
|
|
|
Re: ArrayList.propertyMissing ?melix schrieb:
> I do use a custom class (that extends ArrayList) and it doesn't work either. > Seems like the propertyMissing method that is used is always the default > Groovy one. Looks like a bug to me, but I'd like to be sure ;) it does not even go to property missing. property misssing is kind of new and the GPath implementation is much older. It is of course an interesting idea to support gpaths through property missing... Of course overwriting propertyMissing through user code would then also mean, that any gpath expression used might no longer work. bye blackdrag -- Jochen "blackdrag" Theodorou The Groovy Project Tech Lead (http://groovy.codehaus.org) http://blackdragsview.blogspot.com/ http://www.g2one.com/ --------------------------------------------------------------------- To unsubscribe from this list, please visit: http://xircles.codehaus.org/manage_email |
|
|
Re: ArrayList.propertyMissing ?Well, that's not really a problem in my situation. I'm just pleased that I'm able to setup methodMissing, and it would help me a lot to be able to override propertyMissing too.
|
| Free embeddable forum powered by Nabble | Forum Help |