one of the strong point of Kotlin, in my opinion, is that it can be automatically and magically generated from Java source code. When working in an existing code base, it it super important since I can easily copy and paste the old similar code in Java. However, isn’t it better if there is a reverse tool for converting Kotlin to Java?
Above all, the biggest problem is that there are many organizations unwilling to change. Creating a Kotlin-to-Java converter will surely make it easier to work with them. What do you think? I am not sure if I overestimate Jetbrains, but I think for Jetbrains to create something like that is basically easy. It is the company that creates Intellij and Kotlin that we are talking about.
There is not really a kotlin to java converter yet, but one thing you can do is compile your kotlin code to jvm byte-code and then run a java decompiler. The code you get needs some cleaning up to be usable, but I guess it is a start.
But I don’t think there is any good reason for JetBrains to build a kotlin to java converter. If a company would start using a kotlin to java converter they would be using kotlin anyways. In that case it is easier to switch the entire build system to kotlin instead of having both versions working parallel.
The java 2 kotlin converter is not used to write code in java, then convert it to kotlin and then compile. It is used to make the switch from java to kotlin easier. So why would JetBrains build a tool to switch form kotlin to java?
I think the biggest reason I have found so far is that in any team, there always exist rebellious members who always wanted to tried something new. Like in my team, our code base is completely in Java and the leader and vice-leader have no intention of moving to Kotlin, but I and my colleague, the younger members, both learn Kotlin ourselves and want to tried it. The team leaders are very open-minded, so they allowed us to use in a new project, but for the existing projects, we still MUST use Java. Even seeing mixed codes and the over complicated maven pom files makes my boss uncomfortable.
Another reason is that there are many AI research competitions (GVGAI for example) whose requirement is Java. The organizers are great people but they are also old people who are unwilling to change to a new technology. As participants, I and other students must follow the rule and continue working in Java, then when we graduate and get old, we will see no reason switching to a new technology, then the cycle repeats.
I do not readable Java code, I only need working ones to deal with them. Decompiling from Bytecode is, unfortunately, not working (always full of red errors).
The only thing that does not work is decompiling the file, that contains multiple classes, Java does not allow it, you need to split them manually, as well as clean up some additiona annotations. If it is not the case, you are probably doing something wrong.
I recently had a look at a situation where an intern used Kotlin to implement a prototype. They thought that they might go back to Java using the decompiler. It doesn’t work. I actually had a look at that Kotlin code, and the problem is the use of idiomatic Kotlin being problematic: 1) it requires the Kotlin standard libary 2) It results in very poor Java code. 3) this code was older so no coroutines which are hand-unmaintainable.
Btw. the reason for not using Kotlin is a (perhaps perceived) challenge in recruitment and the almost irreversability of going to Kotlin.
Just wonder if my boss was thinking the same about me . When I showed him the Kotlin code, I actually had the same idea of decompiling to Java later myself, but it was good that he accepted Kotlin right away.
Yes, it would be troublesome, but if Jetbrains is the one behind it, I think it will just work out of the box.
How much effort do you think would be required to build a compiler that parses bytecode generated by the Kotlin compiler, understands all Kotlin constructs and standard library semantics, and outputs Java source code that does not depend on anything related to Kotlin: person-weeks, person-months, person-years? Do you think that a Kotlin-bytecode-to-Java compiler would be the best project to spent that effort on?
What if the Kotlin compiler team improves the compiler and the resulting bytecode changes? Or what if the standard library is extended? A lot of changes to Kotlin would also require work to be done by the Kotlin-bytecode-to-Java compiler team.
JetBrains has a lot of skilled developers, but it is not infallible. Search this forum for the issues people have run into.
Regradless of how much effort it would take to create a Kotlin-to-Java converter, creating such a project will run counter to the goals of JetBrains with regard to spreading the adoption of Kotlin, and therefore we have no plan to build it (or to improve the existing Java bytecode decompiler in IntelliJ IDEA so that it would handle Kotlin-generated bytecode better).
Using Kotlin as a source-to-source preprocessor would deprive you of most of Kotlin’s advantages (null safety, making it possible to build concise and idiomatic APIs, and so on), would result in a much worse workflow in terms of debugging (how would you fix issues if the debugger shows you the Java code generated from Kotlin? how would your colleagues do that?), and will not result in any objective advantages other than satisfying organizations that are “unwilling to change” without providing any motivations behind said unwilling.
Yes, this means than many tools that work for java code won’t be able to handle kotlin code, but I think this is not a good enough reason to invest into a kotlin to java converter. Jetbrains would probably be better served by improving or developing new tools for kotlin.
And I guess with kotlins popularity rising tools like this will be released in the future.
Also as described in this so answer you can use SimpleUML on the decompiled bytecode. I have not tested this so I don’t know how good it works.