Method created via ExpandoMetaClass is not recognized
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