Accessing Kotlin extension functions from Java

2020-01-27 01:58发布

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?

8条回答
家丑人穷心不美
2楼-- · 2020-01-27 02:27

You can always see the actual Java code which is getting generated from your Kotlin code by going to Tools > Kotlin > Show Kotlin Bytecode, then clicking Decompile. This can help you tremendously. In your case the Java code will look like this if you have MyModelExtensions.kt

public final class MyModelExtensionsKt {
   public static final int bar(@NotNull MyModel $receiver) {
      Intrinsics.checkParameterIsNotNull($receiver, "$receiver");
      return $receiver.getName().length();
   }
}

you can improve over this by using @JvmName on the file containing bar:

@file:JvmName("MyModels")
package io.sspinc.datahub.transformation

public fun MyModel.bar(): Int {
    return this.name.length
}

and it will result in this code:

public final class MyModels {
   public static final int bar(@NotNull MyModel $receiver) {
      Intrinsics.checkParameterIsNotNull($receiver, "$receiver");
      return $receiver.getName().length();
   }
}

Using MyModels is in line with what Effective Java suggests for utility classes. You can also rename your method like this:

public fun MyModel.extractBar(): Int {
    return this.name.length
}

then from the Java side it will look idiomatic:

MyModels.extractBar(model);
查看更多
Animai°情兽
3楼-- · 2020-01-27 02:27

You need to duplicate your functions in the class files:

Create Kotlin file , for ex Utils.kt

Enter the code

  class Utils {
                companion object {
                    @JvmStatic
                    fun String.getLength(): Int {//duplicate of func for java
                        return this.length
                    }
                }
            }

        fun String.getLength(): Int {//kotlin extension function
            return this.length
        }

OR

class Utils {
    companion object {

        @JvmStatic
        fun getLength(s: String): Int {//init func for java
            return s.length
        }
    }
}

fun String.getLength(): Int {//kotlin extension function
    return Utils.Companion.getLength(this)//calling java extension function in Companion
}

In kotlin use:

val str = ""
val lenth = str.getLength()

In Java use this:

String str = "";
 Integer lenth = Utils.getLength(str);
查看更多
登录 后发表回答