Is it possible to access extension functions from Java code?
I defined the extension function in a Kotlin file.
package com.test.extensions
import com.test.model.MyModel
/**
*
*/
public fun MyModel.bar(): Int {
return this.name.length()
}
Where MyModel
is a (generated) java class.
Now, I wanted to access it in my normal java code:
MyModel model = new MyModel();
model.bar();
However, that doesn't work. The IDE won't recognize the bar()
method and compilation fails.
What does work is using with a static function from kotlin:
public fun bar(): Int {
return 2*2
}
by using import com.test.extensions.ExtensionsPackage
so my IDE seems to be configured correctly.
I searched through the whole Java-interop file from the kotlin docs and also googled a lot, but I couldn't find it.
What am I doing wrong? Is this even possible?
I have a Kotlin file called NumberFormatting.kt that has the following function
In java I simple access it over the file NumberFormattingKt in the following way after the required import
import ....extensions.NumberFormattingKt;
The other answers here cover the case of calling an extension function located at the top level of a Kotlin package file.
However, my case was that I needed to call an Extension function located inside a Class. Specifically, I was dealing with an Object.
The solution is incredibly simple.
All you have to do is annotate your extension function as
@JvmStatic
, and voila! Your Java code will be able to access it and use it.As far as I can tell this isn't possible. From my reading of the extensions docs, it appears that
creates a new method with the signature
Then, Kotlin maps that function to calls of the form
myModel.bar()
, where ifbar()
isn't found in theMyModel
class it looks for static methods matching the signature and naming scheme it outputs. Note that this is just an assumption from their statements about extensions being statically imported and not overriding defined methods. I haven't gotten far enough in their source to know for sure.So, assuming the above is true there's no way for Kotlin extensions to be called from plain old java code, as the compiler will just see an unknown method being called on an object and error out.
All Kotlin functions declared in a file will be compiled by default to static methods in a class within the same package and with a name derived from the Kotlin source file (First letter capitalized and ".kt" extension replaced with the "Kt" suffix). Methods generated for extension functions will have an additional first parameter with the extension function receiver type.
Applying it to the original question, Java compiler will see Kotlin source file with the name example.kt
as if the following Java class was declared
As nothing happens with the extended class from the Java point of view, you can't just use dot-syntax to access such methods. But they are still callable as normal Java static methods:
Static import can be used for ExampleKt class:
Kotlin top-level extension function are compiled as Java static methods.
Given Kotlin file
Extensions.kt
in packagefoo.bar
containing:The equivalent Java code would be:
Unless, that is,
Extensions.kt
contained the lineIn which case the Java static class would be named
DemoUtils
In Kotlin, extension methods can be declared in other ways. (For example, as a member function or as an extension of a companion object.)
When you extend a class like this:
It will compile to this:
There are more examples here.