Adding method as GString expression to per-instance metaclass

View: New views
5 Messages — Rating Filter:   Alert me  

Adding method as GString expression to per-instance metaclass

by Setya :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi all,

I tried to add dynamic method to per-instance metaclass, the method itself is in the form of GString expression as follows:

def emc = new ExpandoMetaClass(Expando);
def methodName = 'getTest';
emc."$methodName" = {-> println 'Hello World';};
emc.initialize();

def expando = new Expando();
expando.metaClass = emc;

Than I call the method :

expando.test;

No error was thrown but the closure within 'getTest' is never called

Any ideas ?

Regards,

Setya

Re: Adding method as GString expression to per-instance metaclass

by Robert Fischer :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Does expando.getTest() work?

~~ Robert.


Setya wrote:

> Hi all,
>
> I tried to add dynamic method to per-instance metaclass, the method itself
> is in the form of GString expression as follows:
>
> def emc = new ExpandoMetaClass(Expando);
> def methodName = 'getTest';
> emc."$methodName" = {-> println 'Hello World';};
> emc.initialize();
>
> def expando = new Expando();
> expando.metaClass = emc;
>
> Than I call the method :
>
> expando.test;
>
> No error was thrown but the closure within 'getTest' is never called
>
> Any ideas ?
>
> Regards,
>
> Setya

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

    http://xircles.codehaus.org/manage_email



Re: Adding method as GString expression to per-instance metaclass

by Setya :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

>Does expando.getTest() work?

No, it doesn't either.

Strangely, If I use GString literal all works as expected.

I also tried to print all the method attached to expando instance :

emc.expandoMethods.each
{
  println it;
}

And the method 'getTest' is there !!!.

Setya

Re: Adding method as GString expression to per-instance metaclass

by Roshan Dawrani :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi,
The issue is with the lines
---------------------------------------------
def expando = new Expando();
expando.metaClass = emc;
---------------------------------------------

Since Expando is a fully dynamically expandable bean, the call "expando.metaClass = emc" defines a new property metaClass for Expando bean instead of replacing its metaClass with emc.

The way to replace the metaClass for Expando is by calling "expando.setMetaClass(emc);" and not "expando.metaClass = emc;"

If you replace the meta-class as "expando.setMetaClass(emc);", then the new method getTest() is visible through emc as you are defining and the call goes through as expected.

HTH.
Roshan

On Sun, Sep 7, 2008 at 12:06 AM, Setya <jsetya@...> wrote:

>Does expando.getTest() work?

No, it doesn't either.

Strangely, If I use GString literal all works as expected.

I also tried to print all the method attached to expando instance :

emc.expandoMethods.each
{
 println it;
}

And the method 'getTest' is there !!!.

Setya
--
View this message in context: http://www.nabble.com/Adding-method-as-GString-expression-to-per-instance-metaclass-tp19349186p19349736.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




Re: Adding method as GString expression to per-instance metaclass

by Setya :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


Roshan Dawrani wrote:
Hi,
The issue is with the lines
---------------------------------------------
def expando = new Expando();
expando.metaClass = emc;
---------------------------------------------

Since Expando is a fully dynamically expandable bean, the call
"expando.metaClass = emc" defines a new property metaClass for Expando bean
instead of replacing its metaClass with emc.

The way to replace the metaClass for Expando is by calling
"expando.setMetaClass(emc);" and not "expando.metaClass = emc;"

If you replace the meta-class as "expando.setMetaClass(emc);", then the new
method getTest() is visible through emc as you are defining and the call
goes through as expected.

HTH.
Roshan

On Sun, Sep 7, 2008 at 12:06 AM, Setya <jsetya@gmail.com> wrote:

>
> >Does expando.getTest() work?
>
> No, it doesn't either.
>
> Strangely, If I use GString literal all works as expected.
>
> I also tried to print all the method attached to expando instance :
>
> emc.expandoMethods.each
> {
>  println it;
> }
>
> And the method 'getTest' is there !!!.
>
> Setya
> --
> View this message in context:
> http://www.nabble.com/Adding-method-as-GString-expression-to-per-instance-metaclass-tp19349186p19349736.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
>
>
>
It works. Thanks a lot.

Regards,

Setya