Java to Rust converter for tedious editing tasks


#1

Hello,
I am a former C++ now Java-Programmer, quite new in Rust and am fascinated about the safety features of this language. So I thought, why not try to port some functionality implemented in Java to Rust and find out if a GC is really necessary.
I started with the math3 artefact from apache.

There are some tedious tasks which are always the same, therefore I wrote a small server which is capable of parsing Java-Code and creating “rust”-code. Much of the generated code can be used without changes.

java/rust-converter

Try it out as much as you like, perhaps using the snippet below. Some documentation and the sourcecode (Java) can be found at github. You are welcome to use, comment or even help to improve it.

Example:

class A {
     public A() {
       int i = 10;
       double f = 1;
       for (int i = 10; i < 100; i++) {
         f += i;
       }
     }
}

becomes:

struct A {
}

impl A {

    pub fn new() -> A {
         let i: i32 = 10;
         let mut f: f64 = 1;
         {
            let mut i: i32 = 10;
            while i < 100 {
            {
                f += i;
            }
            i += 1;
         }
     }
   }
}

Java to Rust Converter
Java to Rust Converter
#2

So, if I understand correctly, it’s not intended to perform the full conversion, but rather to automatically convert most of the syntax, so that you only left with the important parts to fix manually?


#3

Exactly, and I must admit it is far from perfect. I try to do Rust, not write Java-code, therefore I often use shortcuts. For example, I recognize the identifiers to be snake_cased by their starting lower-case-character, not by syntactical meaning.


#4

I wonder whether this and corrode could use a similar “backend” to generate the Rust code, and transform it to be more idiomatic.

(I like these projects – even though I’m of the opinion that translating one language into another is almost always bad, and that the implicit refactoring you do while reimplementing something is well worth the time.)


#5

Thank you for pointing this out to me. I suspected that something like a C++ converter should exist. In a similar approach to corrode I really created once a Visual Basic to Java converter. From a working VB-Program a working Java was generated. This could be done because the converted VB-Version was quite similar to a primitive Java. Rust and Java are so different, that most decisions about the usage of polymorphy/inheritance and lambas in my opinion can not be matched. I never wanted to achieve that even though I converted the for-loop. This I did only because of some test-code which used them very extensively.

Why “reusing” foreign language code:

You are right that sourcecode converted normally can’t be as good as sourcecode created from ground up. On the other side there are so many stable implementations of functionality including automatic tests, and proven usability, something like this is very difficult to achieve when coding new. And Rust, I think, is no language for rapid prototyping or coding like Ruby/Clojure/Smalltalk. On the other side, at least C/C+±Libraries can be connected using generated Interfacelibraries, this option does not exist as far as I know for Java.