Java Mailing List Archive

http://www.gg3721.com/

Home » user.groovy »

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

Setya

2008-08-22

Replies: Find Java Web Hosting

Author LoginPost Reply

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.';}  
}
}

Then I call above class from Java as follows:

//Java code to access MyGroovyClassImpl.groovy
GroovyClassLoader gcl = new GroovyClassLoader();
Class cls = gcl.parseClass(new File(<path to MyGroovyClassImpl.groovy>));
Object obj = cls.newInstance();
MyJavaInterface myInterface = MyJavaInterface.class.cast(obj);

Binding binding = new Binding();
binding.setVariable("myInterface",myInterface);

GroovyShell gs = new GroovyShell(binding);
gs.evaluate("myInterface.next");


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.

Any help would be greatly appreciated.

Regards,

Setya
--
Sent from the groovy - user mailing list archive at Nabble.com.


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

  http://xircles.codehaus.org/manage_email


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