Support mapping between Kotlin functions and JVM methods/constructors
This commit is contained in:
+7
@@ -0,0 +1,7 @@
|
||||
public class javaConstructor {
|
||||
public final String result;
|
||||
|
||||
public javaConstructor(String result) {
|
||||
this.result = result;
|
||||
}
|
||||
}
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
import kotlin.reflect.*
|
||||
import kotlin.reflect.jvm.*
|
||||
import javaConstructor as J
|
||||
|
||||
fun box(): String {
|
||||
val reference = ::J
|
||||
val javaConstructor = reference.javaConstructor ?: return "Fail: no Constructor for reference"
|
||||
val j = javaConstructor.newInstance("OK")
|
||||
val kotlinConstructor = javaConstructor.kotlinFunction
|
||||
if (reference != kotlinConstructor) return "Fail: reference != kotlinConstructor"
|
||||
return j.result
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
public class javaMethods {
|
||||
public String f(String s) {
|
||||
return s;
|
||||
}
|
||||
|
||||
public static String g(String s) {
|
||||
return s;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
import kotlin.reflect.*
|
||||
import kotlin.reflect.jvm.*
|
||||
import javaMethods as J
|
||||
|
||||
fun box(): String {
|
||||
val f = J::f
|
||||
val fm = f.javaMethod ?: return "Fail: no Method for f"
|
||||
if (fm.invoke(J(), "abc") != "abc") return "Fail fm"
|
||||
val ff = fm.kotlinFunction ?: return "Fail: no KFunction for fm"
|
||||
if (f != ff) return "Fail f != ff"
|
||||
|
||||
val g = J::g
|
||||
val gm = g.javaMethod ?: return "Fail: no Method for g"
|
||||
if (gm.invoke(null, "ghi") != "ghi") return "Fail gm"
|
||||
val gg = gm.kotlinFunction ?: return "Fail: no KFunction for gm"
|
||||
if (g != gg) return "Fail g != gg"
|
||||
|
||||
return "OK"
|
||||
}
|
||||
Reference in New Issue
Block a user