Migration from Kotlin to Java, notes
ℹ️ Draft: This is a list of some activities that we had to do moving from Kotlin to Java. We are just at the beginning, I will update the page with new steps.
Remove typealias - Easy
In Kotlin we can have typealias
like:
typealias Name = String
or
typealias HidingComplexity = Map<Name, Map<Card, Shop>>
After this you can use the type Name
to replace String. This, on one side, can help the readability of the code, on the other side, in case of excessive utilisation can create confusion because the types used are not immediately identifiable.
Java doesn't support directly this feature. For the migration to Java we have to remove these aliases and replace them with the original type.
Lombok issue - create getter and setters
We use Lombok in our project, this helps to reduce the ceremonies of the out-of-the-box Java.
With mixed code Kotlin - Java, Kotlin doesn't recognize the Lombok annotations. Probably is possible to have some build configurations that allow a smooth integration of Lombok with Kotlin.
If you have private fields of classes annotated with @Getter
or @Data
, in Java you can use the standard get
method to access the value.
Kotlin automatically remove the get
from the method and it tries to access directly the field, doing this it will fail during the compilation because your filed is private
and it cannot be accessed.
Kotlin updated to the last version
To simplify the migration it's better to use the latest version of kotlin. Kotlin is moving quickly and maybe some support to feature that you need during the migration can be present in the latest version.
Kotlin will tell you if you are trying to use features that are more recent: e.g. The feature "references to synthetic java properties" is only available since language version 2.1
(we got this when we migrated getter and setters using version 1.9).
enum
This is another easy step, migrate enum class
of Kotlin to Java. Pretty straightforward e.g.:
enum class DevSkills(val skill: String) {
KOTLIN("kotlin");
}
to
import lombok.RequiredArgsConstructor;
@Getter
@RequiredArgsConstructor
public enum DevSkills {
KOTLIN("kotlin");
private final String skill;
}