Setya | 6 Sep 19:42

[groovy-user] Adding method as GString expression to per-instance metaclass


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
--

-- 
View this message in context: http://www.nabble.com/Adding-method-as-GString-expression-to-per-instance-metaclass-tp19349186p19349186.html
Sent from the groovy - user mailing list archive at Nabble.com.

---------------------------------------------------------------------
(Continue reading)

Robert Fischer | 6 Sep 20:15
Favicon
Gravatar

Re: [groovy-user] Adding method as GString expression to per-instance metaclass

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

(Continue reading)

Setya | 6 Sep 20:36

Re: [groovy-user] Adding method as GString expression to per-instance metaclass


>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

Roshan Dawrani | 6 Sep 21:54

Re: [groovy-user] Adding method as GString expression to per-instance metaclass

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-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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



Setya | 8 Sep 05:24

Re: [groovy-user] Adding method as GString expression to per-instance metaclass


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@...> 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

--

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


Gmane