; last updated - 3 minutes read

Today I tried to convert a couple of Java classes to Groovy. Groovy is just Java on steroids, I thought, so this should be pretty simple. Just change the file name extension and be happy.

Alas! Red squiggly lines all over my project. As it turns out, there are subtle differences between the languages.

Optimistic start

My program uses the Java executor framework. So the first thing I realized was Groovy's lack of anonymous inner classes. The Groovy compiler cannot distinguish between anonymous inner classes and closures. Anything starting with a curly brace is considered as a closure. This difference didn't disturb me: I wanted to replace the inner classes by closures anyway.

My array initializers did not work, either. Java uses curly braces to define an array. Groovy cannot do the same, as curly braces indicate closures. Just replace the curly braces by square ones, and everything works. Probably performance suffers a lot: Groovy creates an ArrayList, converting it to a simple array when assigning it to the array variable. On the other hand, if speed is your primary concern, you won't use Groovy anyways.

I like using blocks with local variables. Java allows you to put a curly brace at any position in your method. No need to precede them by an if, a while or anything else. These blocks are a nice way to make the algorithms structure more obvious, and they allow you to repeat similar codes without having to rename the variables. Groovy confuses these blocks with closures, so I had to unroll my blocks.

In many cases, this may be an advantage. I should have created a method instead of repeating code. However, there are useful applications to block, so it is a pity Groovy does not support them.

It's getting a chore

Do you recognize the pattern? The next problem I ran into... no, it did not have anything to do with curly braces. However, it is interesting to see this small language change causing three different problems.

Quite an annoying difference is the do-while-loop. It does not exist in Groovy. While I do not use this kind of loop often, it comes in handy every once in a while. In particular, the sources I used for my little experiment did have quite a few do-while-loops. Converting a do-while-loop to a while-loop took me an hour. The do-while-loop guarantees the loop to be executed at least one time, whereas the while-loop doesn't. Converting the first into the latter is not difficult, but you have to be careful to do it right.

Now I was happy to be able to compile my project. However, it did not work.

I suspect this is a bug of the Groovy version I used. Groovy 1.8.4 seems to have a one or two bugs concerning polymorphism and call to methods of the superclass having the same signature. The latter is a known bug causing a StackOverflowException. Instead of calling the superclass method, Groovy calls the method of the current class. The Groovy developers are working on this bug and solved it in most cases. However, my parent class is a java class, and this bug has to be resolved yet (see http://jira.codehaus.org/browse/GROOVY-4922).

Wrapping it up

At this point, I stopped my little project as I couldn't figure out any workaround to my problem without changing a major part of the underlying Java framework. I'll try again in a couple of months. Let's hope this problem has been solved by the time you are reading my article!


Comments