Automated Java to Scala source code conversion? [closed]
Asked Answered
W

6

44

(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!)

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

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/

Are there any alternatives?

EDIT: This is a question about "software tools commonly used by programmers" which is within current rules of the site, so I'm editing to re-open.

Wallaroo answered 20/3, 2010 at 17:44 Comment(8)
I'd be somewhat wary of automatic conversion even if it exists. One of the biggest advantages of Scala is the ability to express your coding problems in a more compact and comprehensible way. Automatic conversion will express a Java-style solution in Scala syntax.Biomass
@Rex Kerr - +1, and you should turn your comment into an answer. Scala idioms are so dramatically different from Java that even well-written Java code would turn into poorly written Scala code.Fennelflower
@Alex R - I don't know how much Java code you have, but if it's only a few thousand classes, you're going to be better off converting it by hand. You should be able to proceed in a piecemeal fashion, replacing one set of classes at a time. And you'll almost certainly learn something about both Java and Scala in the process.Fennelflower
How about the case where you first auto-convert and then proceed to manually refactoring the code? Seems like a safer (and faster) alternative to fully manual conversion.Kamchatka
IntelliJ works like a cham.Inversion
Regarding your most recent edit: asking for recommendations for tools is explicitly off-topic on this site. See also How to Ask, and more specifically [What topics can I ask about here? ](stackoverflow.com/help/on-topic): "Questions asking us to recommend or find a book, tool, software library, tutorial or other off-site resource are off-topic for Stack Overflow as they tend to attract opinionated answers and spam. Instead, describe the problem and what has been done so far to solve it.".Oringas
@MarkRotteveel the very first paragraph of the page you linked states: "if your question generally covers… [...] software tools commonly used by programmers; and is a practical, answerable problem that is unique to software development …then you’re in the right place to ask your question!" The fact that there is not exactly a bustling commercial market for this problem makes your reference to "opinionated answers and spam" completely baseless and not applicable here. Why does it appear you are selectively applying different parts of the rules?Wallaroo
But you're not asking about a tool (i.e. how can I do Y with tool X), you are asking for a tool, which is asking for a recommendation for an off-source resource, and thus is off-topic, as explained on the page I linked and what I quoted.Oringas
I
39

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.

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 IntelliJ and the Scala Plugin. The Community Edition is free.

Aside from this, Paul Phillips once started the Scalify project 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.

Internationalize answered 20/3, 2010 at 18:11 Comment(3)
Excellent. I will try IntelliJ shortly. I can't believe there are people who think even the trivial stuff like converting angle brackets to square brackets and moving the type declarations to the other side of a ':' should be done by hand! They must not be serious :-)Wallaroo
Awesome. Very happy about this IntelliJ feature.Thadeus
This is an available fork of Paul Phillips' Scalify project (does not seem to be maintained anymore): github.com/mbana/scalify Also, there is javatoscala.com .Exanthema
C
28

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.

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

Cosma answered 30/3, 2011 at 16:31 Comment(7)
I don't see this option in IDEA 10.5.1 with Scala plugin 0.4.1338.Clingstone
Select any java class or open it in editor and this menu item should appear (tested in IDEA 10.5.1)Cosma
I also don't se this option with 11.1 community edition and Scala plugin 0.5.808.Monck
I still have it, though I use ultimate edition (IDEA: IU-117.747, Scala plugin: 0.5.913). Just make sure you have a java file opened, there is no this option if it's a scala or other fileCosma
You need to make sure your project supports Scala before this option appears. Right click on your project, and select Add Framework Support... and add Scala.Nonpareil
in 15+latest scala plugin, I didn't have it in my refactor context menu - only in the refactor main menu menu (down at the bottom)Appointed
I have community edition 15.0.4 EAP (143.2167.2) and scala plugin 2.2.5. I still see "Convert To Scala" under main Refactor menu as well as under context menu's Refactor submenu.Cosma
P
16

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.

Prioress answered 6/1, 2012 at 14:56 Comment(5)
That looks neat, but is there a way to run it on a project that isn't using Maven?Wolframite
You can run the scala code of course directly, the Maven plugin is just a thin wrapper.Squalid
Looking at your mojo class you are using Converter.instance().convert(in, out);. I don't see a main harness around that in your code. By running the scala code directly do you mean coding something to specifically convert my project or is there a built in way to do this?Wolframite
No built in way, but you can contribute something if you want.Squalid
Done, github.com/mysema/scalagen/pull/36. Keep in mind I threw this together pretty quickly just to mess around with scalagen.Wolframite
G
9

You can use online Java to Scala converter which uses Scalagen library.

Gavan answered 27/9, 2013 at 15:31 Comment(0)
B
6

http://code.google.com/p/jatran/

(I haven't used this)


noticed this, Mar 2012: http://blog.mysema.com/2012/03/scalagen-java-to-scala-conversion.html

Bootlick answered 4/10, 2010 at 5:17 Comment(2)
I just followed the link, and the project is empty, no files, no wiki pages, no activity :-(Transient
@Hugh: The following worked for me: 1) check out the code (code.google.com/p/jatran/source/checkout), 2) ant, 3) ./jatran.sh --input SomeClass.javaForsooth
B
2

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).

Brunn answered 20/3, 2010 at 18:39 Comment(5)
Fields and static members do exist. You can easily create an object if you encounter a static field etc. For the generics without a parameter specified, you could always use Any in Scala. It is absolutely possible (if not trivial) to convert Java to Scala. Especially since both languages run on the JVM you have no problems at all feature wise since you can even call into the Java standard library. And both languages are statically typed. To conclude, if you want to convert language A to B then Java to Scala is one of the easiest cases you could choose.Highbred
There are "logical equivalents" but they are not in the least bit the same, so the translation ends up being approximate. A naive translation may work for trivial examples but will break for complex code.Brunn
It seems that the other way round (Scala to Java) is not possible https://mcmap.net/q/375170/-automatically-convert-scala-code-to-java-code-closed/243233Deach
"and Scala doesn't have raw types like Java (generics without the generic parameters specified" - what about [_] parameters?Dorothadorothea
Static members and raw types are trivially mapped to Scala; and I don't see how fields don't exist in Scala—or you mean they're always auto-wrapped with setter-getter pairs? But that's also a trivial 1-1 mapping.Kamchatka

© 2022 - 2024 — McMap. All rights reserved.