What is the easiest way to convert Java code to Ko...
# intellij
d
What is the easiest way to convert Java code to Kotlin in IntelliJ? I am looking for a way to simplify the manual process of moving the source file. Here are my steps: 1. Open Terminal 2.
git mv src/main/java/some/package/Example.java src/main/kotlin/some/package/Example.kt
3. Create a commit just for the rename 4. Fix code 5. Commit changes to file I am looking for a way to simplify steps 1–3. For example, Refactor | Rename does not allow to change the file extension.
r
If you use “Convert Java to Kotlin” action in IntelliJ and then commit both the deleted
.java
and the new
.kt
file, then IntelliJ will automatically create a “Rename .java to .kt” commit for you:
d
I'll give it a try!
I tried Code | Convert Java File to Kotlin. It created a Kotlin file from the Java source. However, if I committed that, I would my Git history because git would not detect it as related (due to the massive changes). I must be using it wrong?
e
git file move detection is based on heuristics, some of which can be tuned (e.g.
git diff --find-renames=#
)
you can't even necessarily guarantee that it is picked up as a rename in all scenarios, as
git merge
will stop looking after a (configurable) number of file, etc.
d
Yes, this is also my understanding. That is why the first commit should be the rename (without any changes to the content) and the subsequent second commit should contain the necessary changes to the file. This is what my original five steps do. I understood Roman's response that IntelliJ should also be able to do this automatically. But it did not work that way for me.
e
what I was trying to say is even with the separate rename commit, there are cases where git will not identify a rename. so I wouldn't try too hard to force it to.
d
Good to know, though I have never run into such problems.
r
@dirk.dittert if possible, could you please record a short video with your exact scenario and the sequence of actions that you’ve performed in the IDE? 🙏
j
What I'd do: Do the conversion, stash it, rename, commit, unstash, commit.
r
Here is a small demonstration of this in action. The code is gibberish, but you should get the main workflow from it: