Java Mailing List Archive

http://www.gg3721.com/

Home » user.groovy »

Re: [groovy-user] Method created via ExpandoMetaClass is not recognized

Jochen Theodorou

2008-08-22

Replies: Find Java Web Hosting

Author LoginPost Reply
Setya schrieb:
> Hi all,
>
> I'm trying to implement Java interface with Groovy as follows:
>
> //MyGroovyClassImpl.groovy
> class MyGroovyClassImpl implements MyJavaInterface
> {
>  MyGroovyClassImpl()
>  {
>   MyGroovyClassImpl.metaClass.getNext = {-> println 'This is next
> command.';}  
>  }
> }
[...]

please try:

> //MyGroovyClassImpl.groovy
> class MyGroovyClassImpl implements MyJavaInterface
> {
>  MyGroovyClassImpl()
>  {
>   MyGroovyClassImpl.metaClass.getNext = {-> println 'This is next command.';}
>      this.metaClass = null  
>  }
> }

because at the point you add the method the class has already a per
instance meta class, but you call will only affect global meta
classes... also... do you really want to do that each time? how about

> //MyGroovyClassImpl.groovy
> class MyGroovyClassImpl implements MyJavaInterface {
>  static {
>     this.metaClass.getNext = {-> println 'This is next command.';}{
>  }
> }

then it will be executed only once and I think the hack with
this.metaClass = null is also not needed any more.

> The last line produces the following exception:
> groovy.lang.MissingPropertyException: No such property: next for class:
> ScriptExecutionContext
>  at
> org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap (ScriptBytecodeAdapter.java:50)
>  at
> org.codehaus.groovy.runtime.ScriptBytecodeAdapter.getProperty (ScriptBytecodeAdapter.java:479)
>  at Script1.run(Script1.groovy)
>  at groovy.lang.GroovyShell.evaluate (GroovyShell.java:543)
>  at groovy.lang.GroovyShell.evaluate (GroovyShell.java:518)
> ...
>
> I've added ExpandoMetaClass.enableGlobally(), but the problem persists.

you did, hmm.. so maybe I am wrong then... unless... is it the first
thing you do?

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


©2008 gg3721.com - Jax Systems, LLC, U.S.A.