Automatically convert Scala code to Java code [closed]
Asked Answered
P

5

22

I have an app written in Scala and some of my team members want a Java version of it. It is a demo app to use another API written in Scala, and they want a Java version of the app to be able to use the API from Java. However, the app is somewhat large and I don't want to manually rewerite in Java (and they don't want to learn Scala). Is there any tool that will automatically generate (readable) Java code from the Scala code?

Plath answered 26/3, 2012 at 22:28 Comment(3)
I've written a proof-of-concept in Prolog that translates a subset of Scala into Java. It's based on my universal-transpiler library.Finger
if you just want code level conversion, in Intellij iDEA when you copy scala code and paste it in a java project, it asks you if you want it to be converted. HTHPooi
Found a good converter online which converted some complex scala code to java - javainuse.com/sc2jaKnighton
E
10

they want a Java version of the app to be able to use the API from Java

Scala classes are usable from Java (since it's all JVM bytecode in the end). Can you just package a jar with your classes for them to use?

Eureetloir answered 26/3, 2012 at 22:29 Comment(3)
I did that but they want a bit more than the API (i.e., the functionality, which requires a few complex examples).Plath
Maybe you could give them the API and some Java examples of how to use it?Allotrope
I guess I will do that. The decompiler option does not generate really usable Java code.Plath
A
9

I don't think it's possible to convert from scala back to standard java since Scala does some pretty low-level byte-code manipulation. I'm 90% sure they do some things that can't exactly be translated back into normal Java code.

Allotrope answered 26/3, 2012 at 22:33 Comment(3)
Any Scala code could, in principle, be re-written as Java (after all, they're both Turing-complete); it just wouldn't be as nice. And any automated Scala-to-Java translation would probably result in some pretty hideous code.Eureetloir
Are you sure of that? I'm not convinced all valid Java bytecode sequences can really be generated by some compiling program...Judgment
True, they're both Turing-complete. I just meant that I don't think you can produce Java code that would compile into exactly the same byte code as scala. You couldn't make a direct translation.Allotrope
P
6

No, there is no such tool.
Scala in some sense is a coffescript of Java. Look how all that funky classes in coffescript are translated to javascript. The same* would be with scala.
I don't think that there is a lot features that can't be translated to Java in any way, but most of the features will be translated to extremely cluttered code, even if the human will do that work.

But there is java to scala translators.

* not saying literally about classes

Phonogram answered 26/3, 2012 at 22:35 Comment(1)
Had a look at the java to scala link. It seems soon my team members will be asking the same question :)Plath
M
5

You have to decompile the .class files to java source files, beware that scala produces many more .class files

You can use javap from oracle included in the sdk or jad decompiler

for detailed explanation read the following article:

Link scala class to java source

Monjan answered 15/7, 2014 at 23:2 Comment(0)
C
4

Not sure if this would work, but you could run the class files through a java decompiler.

If it works at all the result is bound to be ugly as hell.

Chu answered 27/3, 2012 at 6:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.