« Return to Thread: Method created via ExpandoMetaClass is not recognized

Re: Method created via ExpandoMetaClass is not recognized

by roshandawrani :: Rate this Message:

Reply to Author | View in Thread

I am also trying to learn Groovy and I tried this thing and for me both the things worked -

1) setting this.metaClass = null in the MyGroovyClassImpl constructor in MyGroovyClassImpl.groovy

2) setting MyGroovyClassImpl.metaClass.getNext in a static block so that it happens only once.

Can you please take out a few moments and explain the effect "this.metaClass = null" internally has? Does setting it null make the instance use the global metaclass that has been affected by setting "MyGroovyClassImpl.metaClass.getNext"?

Regards,
Roshan

On Fri, Aug 22, 2008 at 9:59 PM, Setya <jsetya@...> wrote:

Hi blackdrag,

Thanks for quick response.

I've tried your suggestions, but none of them works.

Regarding ExpandoMetaClass.enableGlobally(), If I put it in
MyGroovyClassImpl.groovy than the GroovyClassLoader won't parse the file as
instance of MyJavaInterface. If I add it when evaluating the script, it
doesn't take effect.

But If I put those scripts in 1 file it works as expected as follows:

class MyGroovyClassImpl
{
   MyGroovyClassImpl()
   {
       MyGroovyClassImpl.metaClass.getNext = {->println 'This is next
command.';};
   }
}
ExpandoMetaClass.enableGlobally();
def context = new MyGroovyClassImpl();
context.next;

Regards,

Setya
--
View this message in context: http://www.nabble.com/Method-created-via-ExpandoMetaClass-is-not-recognized-tp19108704p19110800.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: Method created via ExpandoMetaClass is not recognized