ExpandoMetaClass - How to call the original method

classic Classic list List threaded Threaded
10 messages Options
Reply | Threaded
Open this post in threaded view
|

ExpandoMetaClass - How to call the original method

Joachim Baumann
Hi,

one question regarding the ExpandoMetaClass. If I override the original
method how can I call it afterwards?

Cheers, Joachim

---------------------------------------------------------------------
To unsubscribe from this list please visit:

    http://xircles.codehaus.org/manage_email

Reply | Threaded
Open this post in threaded view
|

Re: ExpandoMetaClass - How to call the original method

graemer
def original = Foo.metaClass.getMetaMethod("foo", [String] as Class[] )

Foo.metaClass.foo = { String msg ->
     // do stuff
     original.invoke(delegate, [msg] as Object[])
}

Cheers

On 8/6/07, Joachim Baumann <[hidden email]> wrote:

> Hi,
>
> one question regarding the ExpandoMetaClass. If I override the original
> method how can I call it afterwards?
>
> Cheers, Joachim
>
> ---------------------------------------------------------------------
> To unsubscribe from this list please visit:
>
>     http://xircles.codehaus.org/manage_email
>
>


--
Graeme Rocher
Grails Project Lead
http://grails.org

---------------------------------------------------------------------
To unsubscribe from this list please visit:

    http://xircles.codehaus.org/manage_email

Reply | Threaded
Open this post in threaded view
|

Re: ExpandoMetaClass - How to call the original method

Joachim Baumann
Hi Graeme,

thanks for the quick answer.

Btw., when instantiating an ExpandoMetaClass e.g., by accessing the
metaClass property, no reference to the old metaclass is saved. Would it
make sense to do that? After all, it might already hold important
information. DelegatingMetaClass does keep a reference, and it seems to
make sense.

Only curious :-)

Cheers, Joachim

Graeme Rocher schrieb:

> def original = Foo.metaClass.getMetaMethod("foo", [String] as Class[] )
>
> Foo.metaClass.foo = { String msg ->
>      // do stuff
>      original.invoke(delegate, [msg] as Object[])
> }
>
> Cheers
>
> On 8/6/07, Joachim Baumann <[hidden email]> wrote:
>  
>> Hi,
>>
>> one question regarding the ExpandoMetaClass. If I override the original
>> method how can I call it afterwards?
>>
>> Cheers, Joachim
>>
>> ---------------------------------------------------------------------
>> To unsubscribe from this list please visit:
>>
>>     http://xircles.codehaus.org/manage_email
>>
>>
>>    
>
>
>  

--
Xinaris GmbH
Flüsselweg 10, 76646 Bruchsal
Tel.: +(49)7251/39264-21    | Sitz der Gesellschaft: Bruchsal
Fax:  +(49)7251/39264-29    | Registergericht: AG Mannheim HRB 232791
WWW:  http://www.xinaris.de | Geschäftsführer: Dr. Joachim Baumann
Mail: [hidden email]       | USt-ID DE 242 975 278


---------------------------------------------------------------------
To unsubscribe from this list please visit:

    http://xircles.codehaus.org/manage_email

Reply | Threaded
Open this post in threaded view
|

Re: ExpandoMetaClass - How to call the original method

aalmiray
In reply to this post by graemer
Out of curiosity, does it make sense to have an invoke() method that takes a List as the second argument?
so instead of

   original.invoke(delegate, [msg] as Object[])

we could write

   original.invoke(delegate, [msg])

then that version will internally convert the List to an Object[]

I have found that sometimes (specially when learning Groovy) the cast from List
to array is many times overlooked and prompts exceptions that are "hard" to understand
at first sight.

graemer wrote
def original = Foo.metaClass.getMetaMethod("foo", [String] as Class[] )

Foo.metaClass.foo = { String msg ->
     // do stuff
     original.invoke(delegate, [msg] as Object[])
}

Cheers

On 8/6/07, Joachim Baumann <joachim.baumann@xinaris.de> wrote:
> Hi,
>
> one question regarding the ExpandoMetaClass. If I override the original
> method how can I call it afterwards?
>
> Cheers, Joachim
>
> ---------------------------------------------------------------------
> To unsubscribe from this list please visit:
>
>     http://xircles.codehaus.org/manage_email
>
>


--
Graeme Rocher
Grails Project Lead
http://grails.org

---------------------------------------------------------------------
To unsubscribe from this list please visit:

    http://xircles.codehaus.org/manage_email
Reply | Threaded
Open this post in threaded view
|

Re: ExpandoMetaClass - How to call the original method

ld@ldaley.com

On 07/08/2007, at 10:07 AM, Andres Almiray wrote:

> Out of curiosity, does it make sense to have an invoke() method  
> that takes a
> List as the second argument?
> so instead of
>
>    original.invoke(delegate, [msg] as Object[])
>
> we could write
>
>    original.invoke(delegate, [msg])
>
> then that version will internally convert the List to an Object[]
>
> I have found that sometimes (specially when learning Groovy) the  
> cast from
> List
> to array is many times overlooked and prompts exceptions that are  
> "hard" to
> understand
> at first sight.

+1

- LD.

---------------------------------------------------------------------
To unsubscribe from this list please visit:

    http://xircles.codehaus.org/manage_email

Reply | Threaded
Open this post in threaded view
|

Re: ExpandoMetaClass - How to call the original method

Joachim Baumann
In reply to this post by aalmiray
+1

Andres Almiray schrieb:

> Out of curiosity, does it make sense to have an invoke() method that takes a
> List as the second argument?
> so instead of
>
>    original.invoke(delegate, [msg] as Object[])
>
> we could write
>
>    original.invoke(delegate, [msg])
>
> then that version will internally convert the List to an Object[]
>
> I have found that sometimes (specially when learning Groovy) the cast from
> List
> to array is many times overlooked and prompts exceptions that are "hard" to
> understand
> at first sight.
>
>
> graemer wrote:
>  
>> def original = Foo.metaClass.getMetaMethod("foo", [String] as Class[] )
>>
>> Foo.metaClass.foo = { String msg ->
>>      // do stuff
>>      original.invoke(delegate, [msg] as Object[])
>> }
>>
>> Cheers
>>
>> On 8/6/07, Joachim Baumann <[hidden email]> wrote:
>>    
>>> Hi,
>>>
>>> one question regarding the ExpandoMetaClass. If I override the original
>>> method how can I call it afterwards?
>>>
>>> Cheers, Joachim
>>>
>>> ---------------------------------------------------------------------
>>> To unsubscribe from this list please visit:
>>>
>>>     http://xircles.codehaus.org/manage_email
>>>
>>>
>>>      
>> --
>> Graeme Rocher
>> Grails Project Lead
>> http://grails.org
>>
>> ---------------------------------------------------------------------
>> To unsubscribe from this list please visit:
>>
>>     http://xircles.codehaus.org/manage_email
>>
>>
>>
>>    
>
>  

--
Xinaris GmbH
Flüsselweg 10, 76646 Bruchsal
Tel.: +(49)7251/39264-21    | Sitz der Gesellschaft: Bruchsal
Fax:  +(49)7251/39264-29    | Registergericht: AG Mannheim HRB 232791
WWW:  http://www.xinaris.de | Geschäftsführer: Dr. Joachim Baumann
Mail: [hidden email]       | USt-ID DE 242 975 278


---------------------------------------------------------------------
To unsubscribe from this list please visit:

    http://xircles.codehaus.org/manage_email

Reply | Threaded
Open this post in threaded view
|

Re: ExpandoMetaClass - How to call the original method

Jochen Theodorou
In reply to this post by aalmiray
Andres Almiray schrieb:

> Out of curiosity, does it make sense to have an invoke() method that takes a
> List as the second argument?
> so instead of
>
>    original.invoke(delegate, [msg] as Object[])
>
> we could write
>
>    original.invoke(delegate, [msg])
>
> then that version will internally convert the List to an Object[]

how about writing

original.invoke(delegate, msg)

now? That should work

> I have found that sometimes (specially when learning Groovy) the cast from
> List
> to array is many times overlooked and prompts exceptions that are "hard" to
> understand at first sight.

arrays are rarely needed I think.

bye blackdrag

--
Jochen "blackdrag" Theodorou
Groovy Tech Lead (http://groovy.codehaus.org)
http://blackdragsview.blogspot.com/

---------------------------------------------------------------------
To unsubscribe from this list please visit:

    http://xircles.codehaus.org/manage_email

Reply | Threaded
Open this post in threaded view
|

Re: ExpandoMetaClass - How to call the original method

aalmiray
I'm confused, MetaMethod is defined in Java and the signature of invoke() takes an
Object[] as a second parameter. How is it possible to call

   original.invoke(delegate, msg)

and get the desired result when msg is a String?
Perhaps I'm missing some Groovy magic here.

Jochen Theodorou wrote
Andres Almiray schrieb:
> Out of curiosity, does it make sense to have an invoke() method that takes a
> List as the second argument?
> so instead of
>
>    original.invoke(delegate, [msg] as Object[])
>
> we could write
>
>    original.invoke(delegate, [msg])
>
> then that version will internally convert the List to an Object[]

how about writing

original.invoke(delegate, msg)

now? That should work

> I have found that sometimes (specially when learning Groovy) the cast from
> List
> to array is many times overlooked and prompts exceptions that are "hard" to
> understand at first sight.

arrays are rarely needed I think.

bye blackdrag

--
Jochen "blackdrag" Theodorou
Groovy Tech Lead (http://groovy.codehaus.org)
http://blackdragsview.blogspot.com/

---------------------------------------------------------------------
To unsubscribe from this list please visit:

    http://xircles.codehaus.org/manage_email
Reply | Threaded
Open this post in threaded view
|

Re: ExpandoMetaClass - How to call the original method

Jochen Theodorou
Andres Almiray schrieb:
> I'm confused, MetaMethod is defined in Java and the signature of invoke()
> takes an
> Object[] as a second parameter. How is it possible to call
>
>    original.invoke(delegate, msg)
>
> and get the desired result when msg is a String?
> Perhaps I'm missing some Groovy magic here.

the important thing here is that the Object[] is the last parameter, in
which case Groovy handles it as varargs part, even if not marked with
the three dots. Then you can call it the same way you call a varargs
method. you can easily combine this with a list

original.invoke(delegate, *list)

if you like

bye blackdrag

--
Jochen "blackdrag" Theodorou
Groovy Tech Lead (http://groovy.codehaus.org)
http://blackdragsview.blogspot.com/

---------------------------------------------------------------------
To unsubscribe from this list please visit:

    http://xircles.codehaus.org/manage_email

Reply | Threaded
Open this post in threaded view
|

Re: ExpandoMetaClass - How to call the original method

aalmiray
Right, absolutely true, forgot about the varargs bit.
Thanks Jochen =)

Jochen Theodorou wrote
Andres Almiray schrieb:
> I'm confused, MetaMethod is defined in Java and the signature of invoke()
> takes an
> Object[] as a second parameter. How is it possible to call
>
>    original.invoke(delegate, msg)
>
> and get the desired result when msg is a String?
> Perhaps I'm missing some Groovy magic here.

the important thing here is that the Object[] is the last parameter, in
which case Groovy handles it as varargs part, even if not marked with
the three dots. Then you can call it the same way you call a varargs
method. you can easily combine this with a list

original.invoke(delegate, *list)

if you like

bye blackdrag

--
Jochen "blackdrag" Theodorou
Groovy Tech Lead (http://groovy.codehaus.org)
http://blackdragsview.blogspot.com/

---------------------------------------------------------------------
To unsubscribe from this list please visit:

    http://xircles.codehaus.org/manage_email