I have written some code in Java and I need to convert the code to Groovy, to take advantage that Groovy offers. Is there any way to convert Java code to Groovy automatically or some existing plugins that can get me started in this direction?
Fun fact - Groovy has a tool for this already, called java2groovy
- you can see it in the bin
directory of your Groovy installation. It's a wrapper script that calls a class in the distro - org.codehaus.groovy.antlr.java.Java2GroovyMain
.
Pay no attention to the warnings added to the header of the converted code:
!! NOT FIT FOR ANY PURPOSE !!
and
'java2groovy' cannot be used to convert one working program into another
On a more serious note - I'm pretty sure that code hasn't been updated in a long time, and that it wasn't very feature-rich at its peak.
My suggestion is similar to what the others have said - leave it as is if it's working and tested. If you do really need it to be in Groovy, there are a few problematic differences between Groovy and Java, primarily due to Groovy using the { } chars for a closure; Java constructs that use those (e.g. arrays) need to be converted. Also, strangely - there's no do
/while
loop in Groovy. And there are cases where runtime behavior is different from compile time, but Java uses what was compiled. Dynamic dispatch typically results better choices because Groovy looks at what the types are, not what it appeared they looked at compile time. But better is a problem if you're expecting it to be the same.
See http://groovy.codehaus.org/Differences+from+Java for a description of the problems and some workarounds.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With