I am a maintainer of PermissionsDispatcher Plugin which generates Java and Kotlin for PermissionsDispatcher Since Kotlin is getting famous for Android developers, I thought IntelliJ plugins, which generate Android code, should support both Java and Kotlin. (By the way, Kotlin 1.0.2 now supports Android lint! This definitely will lead more developers use Kotlin!)
However, while I was developing this plugin, I found really hard to generate both Java and Kotlin code.
So, this blog post describes what are the differences between generating Java and generating Kotlin using IntelliJ plugin.
Before start taking about the differences, the followings are the environment for this blog post.
- Kotlin for writing the plugin
- Gradle for build
KtFile. Both of them implements
PsiClassOwner which means both of them has the method
This method is useful for
PsiJavaFile, it lets access classes of the file. The plugin can read/write contents of the classes.
KtFile, I expected the same. I want to read/write code of classes. Yes, you can read
.class file. It is not classes inside
.kt file. So, even though, it has methods
KtFile#classes#add throw an exception says the plugin won’t be able to write contents to
If you want to get objects for generating code of Kotlin, then use
This could be because I could not find the best way to generate code…but PsiFactory of Java and Kotlin is different
PsiMethod has modifierList
On the other hand,
To insert new line after annotation, you need to add new line manually.
PsiClass is easy to add method. Use
1 2 3 4
For kotlin, it’s almost the same.
1 2 3 4