Is there a way to convert Groovy to Java automatically?

谁说我不能喝 提交于 2019-12-12 07:42:44

问题


I have inherited significant amounts of Groovy code, and I have found it difficult to maintain for several reasons:

  1. Very often it's hard to tell what's the type of a variable.
  2. Corollary: it's easy to modify a variable with a different type, and not being aware of it.
  3. Many errors will be discovered until run-time (which is scary if your unit testing doesn't cover pretty much everything).
  4. The type of the parameters is basically ignored.
  5. The IDE I'm using (STS Pro) is useful, but far behind from Java. For instance, refactoring is just not available.
  6. Suggestions are available some times, others, not.

Although I appreciate the compactness of the language, maintenance has been difficult and cumbersome.

I have tried to manually convert some pieces to Java, it's been a pain. Are you aware of any tools or plugins that help with this conversion?


回答1:


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;
}



回答2:


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.




回答3:


The Groovy and Java languages both compile to the same bytecode (Java Platform Bytecode). So just (a) compile your .groovy file to a .class file; (b) use a decompiler such as JDGUI to decompile your .class file to a .java file.




回答4:


My top tip is write lots of unit tests.

This advice holds true for most dynamic languages, because you get less of a "safety net" from static type checking by the compiler. You'll want to add tests to check that input and output values have the correct type etc.

I think this will solve most of your problems.




回答5:


I found and alternate solution, using Groovy++. It has almost all the advantages of Groovy, but with performance and strong typing from Java. Furthermore, it is based on Groovy, so apparently you only need to add one jar file, and the "@Typed" annotation at the top of the code.

Furthermore, it adds a new features, such as "GrUnit", and allows mixing dynamic and static types, which I hope will allow for the creation of DSL's. So it allow mixing with existing Groovy code, and use with Grails.

The project seems go be young, but truly promising. I am already testing the waters, and checking how far can I take it.

So, this answer actually doesn't say how convert Groovy to Java, but you can get something even better: the benefits of both worlds plus and optional third world --no pun intended :-) -- static typing and performance.




回答6:


has a some question a year ago. After one or two month of groovy euphoria, it was changer on real world weak. Even GReclipse 2.0 doesn't do a less pain, code becomes unmanageabe and painful. Short answer to your question - no. There is not a real good instrument to do this. GSql i'm replace with Spring JdbcTemplate, Closures with callbacks. All of this need a manual decision of replace strategy, so if you want get a good code you need do int manauly. Else you may use some java decompilers, but i'm think it's not what you really want.




回答7:


We would change about 1000 Groovy Classes to Java 8.

  • automatically : i think its possible in Combination with manual steps

Possible Steps for the Migration

  • def Variables : replace manually with the correct class name in the IDE

  • def Methodes : replace manually with the correct class name as Return Value in the IDE

  • File rename : Automatically replace all .groovy Files to .java Files (Java 8)

  • clousures : replace manually with Java 8 Lambdas and Java 8 Streams

  • Groovy overritten Methodes : replace manually with newer Java jdk Klasses i.e. New File io, New Date API or include Apache Libraries

Use your Unit Tests to control your refactoring.

PS: We usw. Eclipse with the Groovy Plugin.

Greetings Roebi




回答8:


for someone who has similar question, you can take a look on GMavenPlus



来源:https://stackoverflow.com/questions/5302103/is-there-a-way-to-convert-groovy-to-java-automatically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!