Groovy and me, a long love story.

For many days I was trying to adapt some Plain old java test (POJT ?) on my groovy portage of Chromattic Framework (Object mapper to JCR).
This adaptation use the MOP, it’s a cool way to have a good chromattic integration.
But after quick test based on GroovyClassLoader, I have to port all the existing tests to my adaptation, but nothing work !
After several long hours of work in my code and original code, I have just found the real problem : Groovy !
When we say “groovy is dynamic, MOP is beautiful, MOP is cool” it’s not groovy but groovy in specific context.
Groovy called method have the same mechanism of method resolution of Java …
MOP feature seem available only if the call are made by groovy code …

Check this example :

class A {
 public Integer m() { return 3 }
 def invokeMethod(String m, Object p) { return 42 }

and Java unit test :

public class GoofyTestCase extends TestCase {
 public void testGroofy() throws Exception {
   assertEquals(42, new GroovyShell().evaluate("import org.chromattic.groovy.metamodel.B; new B().m()")); // true
   assertEquals((Object) 3, new B().m()); // true

MOP is used only when the call are made in groovy script.
Do the Dynamic resolution be made by groovy or the groovy shell ?


One thought on “Groovy and me, a long love story.

  1. a method call from Java can always only follow the rules made up by Java. There is a design flaw in Groovy regarding invokeMethod. It was used in concurring ways. One was to make method invocations from Java. This works nicely if you leave invokeMethod as it is and not overwrite it. Then it will delegate the call to the MetaClass and use the logic there for the method call, which involves the MOP. The other usage for invokeMethod is “method missing”. Here the method is called if a method call did not find a target. Here for example “assert new B().y() == 42”. If you have overwritten invokeMethod and still want Groovy style method invocation, then use InvokerHelper#invokeMethod instead.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s