Now I love Kotlin even more!
Hi everyone! I started my career a long time ago with Java and Android. Later Kotlin appeared on the market and I loved our smooth industry migration. Now Kotlin is the official programming language for Android and we are widely using it. Kotlin is also supported by many popular server-side frameworks like Spring, Micronaut, Javalin, and of course Ktor. I even tried Ktor once for one of my pet projects and was surprised at how simple it was to write server-side code in my native programming language.
But today Kotlin just saved a week of work for my team, so let me tell you this story. As it usually happens on projects one of our customers came up with a request to port quite a big Java library into JavaScript! We were surprised and the original estimation was something like a week of work. During the discussion we found that it’s just a temporary solution and they are not going to maintain or support this library in the future, so they don’t care about the code quality, maintainability, and other quality attributes.
After getting this information we started looking for some tool that can automatically convert Java code to JS but we were not able to fount some out-of-the-box solutions. Later we remembered that Kotlin works not only on JVM but also KotlinJS project exists. And that was our solution!
We walked through the docs and found that we could do it in just two steps!
The first step was quite obvious — just converting Java code to Kotlin. We decided to put everything into one file to have fewer dependencies and after converting received a huge Kotlin file:
ConvertedCode.kt
But the second step is more interesting: we were taking this file and converted it to the JS using kotlinc-js compiler:
kotlinc-js -output Result.js -meta-info ConvertedCode.kt
Please NOTE that before conversion you should remove all dependencies from the Java code. Only pure Java code can be converted this way.
And that's it! We have a huge Result.js fine and it’s still human readable. We found that all Kotlin library functions are available through mapping. Even StringBuilder and Collections are supported!
var println = Kotlin.kotlin.io.println_s8jyv4$;
var joinToString = Kotlin.kotlin.collections.joinToString_fmv235$;
var ArrayList_init = Kotlin.kotlin.collections.ArrayList_init_287e2$;
var StringBuilder = Kotlin.kotlin.text.StringBuilder;
var StringBuilder_init = Kotlin.kotlin.text.StringBuilder_init;
var contains = Kotlin.kotlin.text.contains_li3zpu$;
var indexOf = Kotlin.kotlin.text.indexOf_l5u8uk$;
var indexOf_0 = Kotlin.kotlin.text.indexOf_8eortd$;
var lastIndexOf = Kotlin.kotlin.text.lastIndexOf_l5u8uk$;
var unboxChar = Kotlin.unboxChar;
var equals = Kotlin.equals;
var equals_0 = Kotlin.kotlin.text.equals_igcy3c$;
var print = Kotlin.kotlin.io.print_s8jyv4$;
var toString = Kotlin.toString;
var Kind_OBJECT = Kotlin.Kind.OBJECT;
var throwCCE = Kotlin.throwCCE;
var toBoxedChar = Kotlin.toBoxedChar;
So after importing Kotlin to the JS project we were able to run the code.
Of course, we covered everything with unit tests to check if the behavior is the same and were surprised that the result is the same! Exactly what we needed — porting without porting. Kotlin just saved us again!
Thanks for reading!