有没有办法将Groovy自动转换为Java? [英] Is there a way to convert Groovy to Java automatically?

查看:990
本文介绍了有没有办法将Groovy自动转换为Java?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我继承了大量的Groovy代码,并且由于以下几种原因,我发现很难维护:


  1. 很多时候很难告诉什么是变量的类型。

  2. 推论:修改一个不同类型的变量并且不知道它是很容易的。

  3. 在运行之前会发现许多错误,时间(如果你的单元测试几乎涵盖了所有的东西,这很可怕)。

  4. 参数的类型基本上被忽略。我使用的IDE(STS Pro)很有用,但远远落后于Java。例如,重构只是不可用。

  5. 建议有时可用,有些则不可用。



虽然我很欣赏这种语言的紧凑性,但维护起来很困难和麻烦。


我试图手动将一些片段转换为Java,这是一个痛苦。您知道任何有助于此转换的工具或插件吗?

解决方案

使用Groovy ++找到并使用替代解决方案。它几乎具有Groovy的所有优点,但具有Java的性能和强大的输入。此外,它基于Groovy,所以显然你只需要添加一个jar文件和代码顶部的@Typed注解。



另外,它增加了一个新的功能,如GrUnit,并允许混合动态和静态类型,我希望可以创建DSL。因此,它允许与现有的Groovy代码混合,并用于Grails。



该项目似乎很年轻,但确实很有前途。我已经测试了水域,并且检查了我可以采用多远。



所以,这个答案实际上并没有说如何将Groovy转换为Java,但是您可以获得一些更好的东西:两个世界加上和可选的第三世界的好处 - 没有双关语意图:-) - 静态类型和性能。


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?

解决方案

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.

这篇关于有没有办法将Groovy自动转换为Java?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆