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/

Any alternatives?

durron597
  • 30,764
  • 16
  • 92
  • 150
Alex R
  • 10,320
  • 12
  • 76
  • 145
  • 6
    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. – Rex Kerr Mar 20 '10 at 18:43
  • @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. – kdgregory Mar 20 '10 at 19:43
  • @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. – kdgregory Mar 20 '10 at 19:45
  • 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. – Erik Kaplun Nov 20 '13 at 20:10
  • IntelliJ works like a cham. – Vishrant Jul 05 '18 at 19:08

6 Answers6

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.

retronym
  • 53,652
  • 11
  • 151
  • 168
  • 10
    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 Mar 21 '10 at 17:00
  • 2
    Awesome. Very happy about this IntelliJ feature. – laher May 04 '11 at 23:23
  • 2
    This is an available fork of Paul Phillips' Scalify project (does not seem to be maintained anymore): https://github.com/mbana/scalify Also, there is http://javatoscala.com . – Roland Ewald Dec 10 '13 at 16:16
27

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.

Eugene Platonov
  • 2,435
  • 2
  • 22
  • 20
  • I don't see this option in IDEA 10.5.1 with Scala plugin 0.4.1338. – Jeff Axelrod Aug 26 '11 at 15:12
  • 1
    Select any java class or open it in editor and this menu item should appear (tested in IDEA 10.5.1) – Eugene Platonov Aug 30 '11 at 21:02
  • I also don't se this option with 11.1 community edition and Scala plugin 0.5.808. – devth Jul 09 '12 at 15:36
  • 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 file – Eugene Platonov Jul 19 '12 at 01:46
  • 1
    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. – Jonathan Hult Sep 20 '15 at 19:43
  • 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) – ycomp Feb 23 '16 at 02:59
  • 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. – Eugene Platonov Feb 25 '16 at 16:39
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.

Timo Westkämper
  • 20,443
  • 4
  • 67
  • 103
  • 1
    That looks neat, but is there a way to run it on a project that isn't using Maven? – James McMahon Jun 23 '12 at 03:24
  • You can run the scala code of course directly, the Maven plugin is just a thin wrapper. – Timo Westkämper Jun 23 '12 at 09:28
  • 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? – James McMahon Jun 23 '12 at 10:47
  • No built in way, but you can contribute something if you want. – Timo Westkämper Jun 23 '12 at 11:00
  • Done, https://github.com/mysema/scalagen/pull/36. Keep in mind I threw this together pretty quickly just to mess around with scalagen. – James McMahon Jun 23 '12 at 19:32
9

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

Luka Zakrajšek
  • 1,049
  • 11
  • 12
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

Gene T
  • 5,156
  • 1
  • 22
  • 24
  • I just followed the link, and the project is empty, no files, no wiki pages, no activity :-( – Hugh Perkins Jan 30 '11 at 01:37
  • @Hugh: The following worked for me: 1) check out the code (http://code.google.com/p/jatran/source/checkout), 2) ant, 3) ./jatran.sh --input SomeClass.java – Matt R Apr 02 '11 at 11:57
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).

Erik Engbrecht
  • 3,144
  • 15
  • 23
  • 2
    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 Mar 20 '10 at 20:38
  • 4
    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 Mar 24 '10 at 18:24
  • It seems that the other way round (Scala to Java) is not possible http://stackoverflow.com/q/9880805/243233 – Jus12 Mar 26 '12 at 22:49
  • "and Scala doesn't have raw types like Java (generics without the generic parameters specified" - what about [_] parameters? – Display Name May 19 '13 at 15:11
  • 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. – Erik Kaplun Nov 20 '13 at 20:14