Oke thinking about small kotlin script project - a...
# scripting
e
Oke thinking about small kotlin script project - automate java to kotlin conversion in specific folder. After googling it looks like I can use kotlin plugin jar to call conversion functionality from it or I can grab source of it.
h
Do you need to use the Kotlin api of the Java classes only or do you actually want to convert your Java source code to Kotlin source code to commit it? The first case does not need any script, the Kotlin compiler supports Java classes and generates the api internally, the second case requires a converter. There is a Java to Kotlin converter built-in in the Kotlin IntelliJ plugin accessible from File->Convert Java Class to Kotlin. You could run IntelliJ in headless mode but use the UI to call this action, or use the IntelliJ API to create a cli application. I tried the latter https://github.com/hfhbd/j2k but the Kotlin IntelliJ plugin is deeply wired into IntelliJ so it is still work in progress.
e
Thank you! I joined project where 10% (~1000 files) are still java
c
I would recommend doing them one by one. The IntelliJ converter is a great start, but its output is not really idiomatic Kotlin.
e
Sure, idea is automate this via script - preserve git history and first conversion. And after it will be delegated to people to make it idiomatic.
h
It really depends on your use-case but I wouldn’t convert working Java code to Kotlin just because it’s possible, but only when necessary (eg multiplattform) or to have better Kotlin interop, like sequences, coroutines.
e
I wanted to do it for two reasons - reduce the cognitive load (double tooling for automation, two languages) and java knowledge is shrinking in the team
We could postpone conversion until we really work on these files
Java still compiles faster than Kotlin. But who knows also how build will be impacted by eliminating java from.
But that is not a driving point
c
If you're going to have humans clean up the generated code anyway, I would let them do the conversion too using IntelliJ. This way, you have an easy way to measure how many unconverted files are left, instead of having many "Java files written in Kotlin" laying around. It also lets you review the conversion more accurately. I doubt converting all files at once is going to save any time if humans have to go through them all to clean them up.
h
Well, I get your point, we have a very similar situation in my team as well (but COBOL to Kotlin), but you still need the knowledge of the platform when targeting Kotlin/JVM and automatic converting isn’t optimal, so I recommend waiting until needing.
e
Thank you for the input!
h
Your (old) code also often contains logic written in a legacy way so when rewriting manually you will also update the logic/functions as well to meet modern standards. Automatic converting does not update the standards so you will have legacy code but Kotlin.
e
That are two separate things for me - refactoring for the better/new and updating infra from components. We can combine these but it depends.
h
I don’t know what you mean with updating infra, I talk about your code only. If you rewrite your code manually, you will also update it to modern standards. Eg when updating old Java code to modern Java code you will also update to the new Path api, or just use automatic resource management
try (InputStream is =
. But this could change your business logic, so you really really need tests, tests and tests.
Converting tools won’t update your standards but keep it, so you get some „weird“ but same working code.