views:

555

answers:

3

(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/

Any alternatives?

+9  A: 

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.

retronym
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 :-)
Alex R
+2  A: 

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

Erik Engbrecht
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.
Joa Ebert
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.
Erik Engbrecht
+1  A: 

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

(I haven't used this)

Gene T
I will try this shortly...
Alex R