Is there a way to convert Groovy to Java automatically?

Probably not the answer you want to hear, but I would focus on becoming more comfortable with Groovy instead of trying to convert the code to Java. There are many things you can do in Groovy which simply won't translate well to Java (like closures). Any automated conversion to Java will make the code much less readable and harder to understand.

If you can't be persuaded to stick with Groovy, and you MUST migrate to Java, your best bet will be to do it by hand.


IntelliJ IDEA has a quite decent support for refactoring of groovy code. It also has a source code level converter from Groovy -> Java. Most of the time it generates code that does not compile, but it may help to get you started with the process of converting the code. Groovy code:

class HelloWorld {
def name
def greet() { "Hello ${name}" }
int add(int a, int b) {
    return a+b;
}
}

Converted Java code:

public class HelloWorld {
public GString greet() {
    return "Hello " + String.valueOf(name);
}

public int add(int a, int b) {
    return a + b;
}

public Object getName() {
    return name;
}

public void setName(Object name) {
    this.name = name;
}

private Object name;
}