I want to call Kotlin functions from Java, but whe...
# announcements
j
I want to call Kotlin functions from Java, but when I disassamble the compiled Kotlin code, I can see that the types are e.g. kotlin.String instead of java.lang.String. Is there a way to compile the Kotlin code to Java types directly, or do we need to call the Kotlin code with e.g. kotlin.String, and if so, how do we convert from Java types to Kotlin types?
d
kotlin.String
is a fictional type, it becomes
java.lang.String
when compiling for the JVM. https://kotlinlang.org/docs/reference/java-interop.html#mapped-types
j
Copy code
package jocke.polylith.kotlin

public fun hello(value: kotlin.String): kotlin.Unit { /* compiled code */ }

public fun helloval(value: <http://kotlin.Int|kotlin.Int>): kotlin.Unit { /* compiled code */ }

public fun main(args: kotlin.Array<kotlin.String>): kotlin.Unit { /* compiled code */ }
When I decompiled the class file, it doesn't seem so.
d
You decompiled it to kotlin (not sure how you did that?), which means it'll do the translation again.
j
I just dropped the class file in IntelliJ IDEA, and then it showed up like this.
d
In a kotlin class you can just do
Tools > Kotlin > Show Kotlin Bytecode
or even
Tools > Kotlin > Decompile Kotlin to Java
j
We are building a library that will be consumed from Java (or actually Clojure, but they are compatible). I now tried to call a function with no arguments and now I got the error: Syntax error (NoSuchFieldException) ...even though the intellisense works from IntelliJ IDEA.
It works now. Thanks for the help!