自动化 Java 到 Scala 源代码的转换?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2484064/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 08:16:07  来源:igfitidea点击:

Automated Java to Scala source code conversion?

javascalacode-conversion

提问by Alex R

(Yes I know I can call Java code from Scala; but that is pointless; I want to DELETE the Java code, not keep it around and have to look at it and maintain it forever!)

(是的,我知道我可以从 Scala 调用 Java 代码;但这毫无意义;我想删除 Java 代码,而不是保留它,并且必须查看它并永远维护它!)

Are there any utilities out there to convert Java source to Scala source?

是否有任何实用程序可以将 Java 源代码转换为 Scala 源代码?

I believe theoretically it should be possible to accomplish with minimal lossage.

我相信理论上应该可以以最小的损失完成。

I have found this but it seems inactive, probably buggy/incomplete... http://sourceforge.net/projects/java2scala/

我发现了这个,但它似乎不活跃,可能有问题/不完整...... http://sourceforge.net/projects/java2scala/

Any alternatives?

任何替代方案?

采纳答案by retronym

IntelliJ kinda, sorta, does this. You need to open a project with your Java sources. You can then copy/paste expressions, methods, or entire classes in to a .scala file. This converts to equivalent Scala code.

IntelliJ 有点,有点,这样做。您需要使用 Java 源打开一个项目。然后,您可以将表达式、方法或整个类复制/粘贴到 .scala 文件中。这将转换为等效的 Scala 代码。

The fidelity of conversion isn't perfect, and, for this reason, it doesn't support a bulk conversion yet.

转换的保真度并不完美,因此,它还不支持批量转换。

I recommend using the latest version of IntelliJand the Scala Plugin. The Community Edition is free.

我建议使用最新版本的IntelliJScala Plugin。社区版是免费的。

Aside from this, Paul Phillips once started the Scalifyproject to translate code from Java to Scala (or, potentially, your favourite language), and even improve it in the process! He explains the concept in this video. However this effort was stalled, presumably because he turned his attention to directly contributing to the Scala compiler and standard library.

除此之外,Paul Phillips 曾经启动了Scalify项目,将代码从 Java 翻译成 Scala(或者,可能是你最喜欢的语言),甚至在这个过程中改进它!他解释了这个视频中的概念。然而,这项工作停滞不前,大概是因为他将注意力转向直接为 Scala 编译器和标准库做出贡献。

回答by Erik Engbrecht

I don't think it's possible to automatically convert Java to Scala in the general case. Many of the lower-level constructs in Java don't exist in Scala (e.g. fields and static members), Scala places limitations on constructors that don't exist in Java, and Scala doesn't have raw types like Java (generics without the generic parameters specified).

我认为在一般情况下不可能自动将 Java 转换为 Scala。Java 中的许多低级构造在 Scala 中不存在(例如字段和静态成员),Scala 对 Java 中不存在的构造函数进行了限制,并且 Scala 没有像 Java 那样的原始类型(没有指定了通用参数)。

回答by Eugene Platonov

In IntelliJ IDEA theres is a refactoring called "Convert to Scala". It's under Refactor menu (after you install Scala plugin) Ctrl+Shift+G. So you don't need to play with copy/paste.

在 IntelliJ IDEA 中有一个名为“Convert to Scala”的重构。它在重构菜单下(安装 Scala 插件后)Ctrl+Shift+G。所以你不需要玩复制/粘贴。

Just make sure you have a java file opened, there is no this option if it's a scala or other file.

只要确保您打开了一个 java 文件,如果它是 Scala 或其他文件,则没有此选项。

回答by Timo Westk?mper

I just published the following tool : Scalagen. I tried Jatran as well, but was frustrated with some bugs and difficult integration. Scalagen is extensible and comes with a Maven plugin.

我刚刚发布了以下工具:Scalagen。我也尝试过 Jatran,但对一些错误和困难的集成感到沮丧。Scalagen 是可扩展的,并带有一个 Maven 插件。

回答by Luka Zakraj?ek

You can use online Java to Scala converterwhich uses Scalagenlibrary.

您可以使用使用Scalagen库的在线Java 到 Scala 转换器