如何将 Java 代码自动转换为 Groovy 代码
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26584320/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to convert Java code to Groovy code automatically
提问by Siva Rajendran
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?
我已经用 Java 编写了一些代码,我需要将代码转换为 Groovy,以利用 Groovy 提供的优势。有什么方法可以将 Java 代码自动转换为 Groovy 或一些现有的插件,可以让我朝这个方向开始?
回答by Burt Beckwith
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
.
有趣的事实——Groovy 已经有一个工具,叫做java2groovy
——你可以在bin
你的 Groovy 安装目录中看到它。它是一个包装脚本,用于调用发行版中的类 - 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.
我的建议与其他人所说的相似 - 保持原样,就好像它正在工作和测试一样。如果您确实需要在 Groovy 中使用它,那么 Groovy 和 Java 之间存在一些有问题的差异,主要是因为 Groovy 使用 { } 字符作为闭包;需要转换使用这些(例如数组)的 Java 构造。另外,奇怪的是 - Groovy 中没有do
/while
循环。在某些情况下,运行时行为与编译时不同,但 Java 使用已编译的内容。动态分派通常会产生更好的选择,因为 Groovy 着眼于类型是什么,而不是它们在编译时看起来的样子。但如果你期望它是相同的,更好是一个问题。
See http://groovy.codehaus.org/Differences+from+Javafor a description of the problems and some workarounds.
有关问题的描述和一些解决方法,请参阅http://groovy.codehaus.org/Differences+from+Java。
回答by Ankur Anandapu
I would like to show it with an example program:
我想用一个示例程序来展示它:
private static List<List> subn(int n, List li) {
List<List> ret = new ArrayList<List>();
if (n == 0) {
ret.add(new ArrayList());
return ret;
}
if (li.isEmpty()) {
return ret;
}
T x = li.get(0);
List xs = li.subList(1, li.size());
for (List sub : subn(n-1, xs)) {
sub.add(0, x);
ret.add(sub);
}
ret.addAll(subn(n, xs));
return ret;
}
For the first step I would like to remove static typing, though i am not agaist it but for such a small code, i would go for it.
对于第一步,我想删除静态类型,虽然我不反对它,但对于这么小的代码,我会去做。
def subn(n, li) {
def ret = new ArrayList();
if (n == 0) {
ret.add(new ArrayList());
return ret;
}
if (li.isEmpty()) {
return ret;
}
def x = li.get(0);
def xs = li.subList(1, li.size());
for (sub in subn(n-1, xs)) {
sub.add(0, x);
ret.add(sub);
}
ret.addAll(subn(n, xs));
return ret;
}
This will change the character count:) but not the structure. let us now change the array literals:
这将改变字符数:) 但不会改变结构。现在让我们更改数组文字:
def subn(n, li) {
def ret = [];
if (n == 0) {
ret.add([]);
return ret;
}
if (li.isEmpty()) {
return ret;
}
def x = li.get(0);
def xs = li.subList(1, li.size());
for (sub in subn(n-1, xs)) {
sub.add(0, x);
ret.add(sub);
}
ret.addAll(subn(n, xs));
return ret;
}
We see more chars trimmed, now lets change the if statement.
我们看到修剪了更多字符,现在让我们更改 if 语句。
def subn(n, li) {
if (n == 0) return [[]];
if (li.isEmpty()) return [];
def ret = [];
def x = li.get(0);
def xs = li.subList(1, li.size());
for (sub in subn(n-1, xs)) {
sub.add(0, x);
ret.add(sub);
}
ret.addAll(subn(n, xs));
return ret;
}
I'm not really a fan of short variable names so next step is to rename some variables. In addition to this I'll change the loop slightly to use the each function. The ‘it' magic variable replaces the ‘sub' variable.
我不是很喜欢短变量名,所以下一步是重命名一些变量。除此之外,我将稍微更改循环以使用 each 函数。'it' 魔法变量替换了 'sub' 变量。
def subn(n, list) {
if (n == 0) return [[]];
if (list.isEmpty()) return [];
def ret = [];
def head = list.get(0);
def remainder = list.subList(1, list.size());
subn(n-1, remainder).each {
it.add(0, head);
ret.add(it);
}
ret.addAll(subn(n, remainder));
return ret;
}
By adding the collections together we can simplify the loop a bit more:
通过将集合添加在一起,我们可以进一步简化循环:
def subn(n, list) {
if (n == 0) return [[]];
if (list.isEmpty()) return [];
def ret = [];
def head = list.get(0);
def remainder = list.subList(1, list.size());
subn(n-1, remainder).each {
ret.add([head] + it);
}
ret.addAll(subn(n, remainder));
return ret;
}
Looking at the code it becomes clear that the ‘ret' variable is just accumulating the results. We can use the collect method for this purpose instead.
查看代码,很明显 'ret' 变量只是在累积结果。为此,我们可以使用 collect 方法。
def subn(n, list) {
if (n == 0) return [[]];
if (list.isEmpty()) return [];
def head = list.get(0);
def remainder = list.subList(1, list.size());
def ret = subn(n-1, remainder).collect { [head] + it }
ret.addAll(subn(n, remainder));
return ret;
}
In fact we really don't need ‘ret' at all if instead we just add the two collections together:
事实上,如果我们只是将两个集合加在一起,我们根本不需要 'ret':
def subn(n, list) {
if (n == 0) return [[]];
if (list.isEmpty()) return [];
def head = list.get(0);
def remainder = list.subList(1, list.size());
return subn(n-1, remainder).collect { [head] + it } + subn(n, remainder);
}
Finally lets inline the head variable. It's only used in one place anyway so not really worth keeping it. In addition we'll use Groovy list indexing to remove the get() call.
最后让我们内联 head 变量。无论如何它只在一个地方使用,所以不值得保留它。此外,我们将使用 Groovy 列表索引来删除 get() 调用。
def subn(n, list) {
if (n == 0) return [[]];
if (list.isEmpty()) return [];
def remainder = list.subList(1, list.size());
return subn(n-1, remainder).collect { [list[ 0 ]] + it } + subn(n, remainder);
}
回答by Siva Rajendran
I recommend This ! Its worth Reading . And it worked for me ! Kind of simple.
我推荐这个!值得一读。它对我有用!有点简单。