Hello, I'm trying to use the Java to Kotlin (j2k) ...
# intellij
m
Hello, I'm trying to use the Java to Kotlin (j2k) functionality in IntelliJ to convert a somewhat large codebase to Kotlin. Unfortunately it ends up with a lot of errors and it is too much to try to correct manually. Is there any way to get the code to convert properly? Some examples of the errors I see: • Unresolved reference: Nonnull • Only safe (?.) or non-null asserted (!!.) calls are allowed on a nullable receiver of type... • Inferred type is ... but ... was expected • '....' overrides nothing The project compiles fine with Java. Also I don't know if j2k is considered to be a transpiler, but this Java code works flawlessly with another well-known transpiler
a
j2k is a really useful tool, but it needs some help. Does the Java code have non-null/nullable annotations everywhere? Are you using Maven or Gradle? Are you converting multiple files at once, or file-by-file, or even function-by-function? I don't think it indented to be a direct source-to-source util. What's the other transpiler?
m
Hey Adam, thanks for your response. Yes, there are @Nullable and @Nonnull annotations (from the javax.annotation package). Some getter functions have these properties, and there's other cases where parameters may have such properties. I am using Gradle and had converted multiple files at once. (Selecting the folder with the entire codebase and converting it) The other transpiler is J2ObjC (https://github.com/google/j2objc)
j
you will be a lot better off leaving the good Java code, and converting it by hand, rather than ending up with terrible generated Kotlin code
1
m
Unfortunately it's a lot of code and will take up a lot of time which is why I was hoping to automate it. I'm not really worried about readability at the moment if that becomes an issue
r
Twice now I’ve converted a large Java codebase piecemeal using J2K over about a year. Whenever I need to touch a file I convert it, and I do the odd package when I get some downtime. J2K does 85% of it, then I fix up the rest and make it idiomatic.
Not having to do it all in a single pass is brilliant.
👍 1
a
Nullable annotations will help a lot 👍 > I am using Gradle and had converted multiple files at once. (Selecting the folder with the entire codebase and converting it) Sometimes multiple files can work, but yeah, it can be a real pain to figure out. What I've done before is to convert the main sources file-by-file, and if it doesn't work first time (or requires a lot of fixing after conversion), function-by-function. I'll leave the test sources intact and make the new Kotlin code compatible with Java code (e.g. adding
@JvmStatic
,
@JvmField
, or
@JvmOverloads
if necessary). That way it's easier to verify the converted code still works.
k
To rewind it a bit… Why convert everything to Kotlin? There’s no tool that exists right now that is able to convert any non-trivial Java code (just even one class) to idiomatic Kotlin. So even if you manage to get j2k convert your codebase to something that compiles, you’ll have the same exact codebase, but now written in a Java’ish Kotlin, neither here nor there. The only “benefit” I can see here is to check off some self-imposed target of having a Kotlin only codebase. And now imagine somebody joining the team and having to work on that codebase.
👍 2
m
My goal is not to convert the whole codebase so that everything can be 100% Kotlin. My plan was to use the resulting Kotlin code with Kotlin Multiplatform so that I could have a Java codebase work on iOS. Converting the code seems like the only option. Unfortunately KMM obviously does not allow any Java code nor does it also work with bytecode. Any dependency must also be a KMM project which makes this tough
k
If the source Java code has any non trivial JDK dependencies, then 🫣