KT-13440 Bound callable references in reflection

This commit is contained in:
Dmitry Petrov
2016-11-23 18:54:58 +03:00
parent 5879e201cd
commit 3d2696c81b
33 changed files with 1276 additions and 70 deletions
@@ -1,3 +1,6 @@
// TODO investigate should it be ran for JS or not
// IGNORE_BACKEND: JS
class A {
companion object {
fun ok() = "OK"
@@ -1,3 +1,6 @@
// TODO investigate should it be ran for JS or not
// IGNORE_BACKEND: JS
object Singleton {
fun ok() = "OK"
}
@@ -1,6 +1,3 @@
// TODO: investigate should it be ran for JS or not
// IGNORE_BACKEND: JS
fun Boolean.foo() = 1
fun Byte.foo() = 2
fun Short.foo() = 3
@@ -33,8 +33,9 @@ fun check(lambda: () -> Unit) {
lambda()
} catch (e: Throwable) {
if (e !is InvocationTargetException && e !is StackOverflowError) {
throw AssertionError("The current implementation uses reflection to get the value of the property," +
"so either InvocationTargetException or StackOverflowError should have happened, but was: ${e}")
throw RuntimeException("The current implementation uses reflection to get the value of the property," +
"so either InvocationTargetException or StackOverflowError should have happened",
e)
}
return
}
@@ -0,0 +1,53 @@
// TODO: investigate should it be ran for JS or not
// IGNORE_BACKEND: JS
// WITH_REFLECT
import kotlin.reflect.*
import kotlin.test.assertEquals
class Host {
companion object {
val x = 1
var y = 2
val xx: Int
get() = x
var yy: Int
get() = y
set(value) { y = value }
}
}
val c_x = Host.Companion::x
val c_xx = Host.Companion::xx
val c_y = Host.Companion::y
val c_yy = Host.Companion::yy
fun box(): String {
assertEquals(1, c_x.getter())
assertEquals(1, c_x.getter.call())
assertEquals(1, c_xx.getter())
assertEquals(1, c_xx.getter.call())
assertEquals(2, c_y.getter())
assertEquals(2, c_y.getter.call())
assertEquals(2, c_yy.getter())
assertEquals(2, c_yy.getter.call())
c_y.setter(10)
assertEquals(10, c_y.getter())
assertEquals(10, c_yy.getter())
c_yy.setter(20)
assertEquals(20, c_y.getter())
assertEquals(20, c_yy.getter())
c_y.setter.call(100)
assertEquals(100, c_yy.getter.call())
c_yy.setter.call(200)
assertEquals(200, c_y.getter.call())
return "OK"
}
@@ -0,0 +1,12 @@
// TODO: investigate should it be ran for JS or not
// IGNORE_BACKEND: JS
// WITH_REFLECT
import kotlin.reflect.*
fun String.foo(x: String) = this + x
fun String?.bar(x: String) = x
fun box() =
(""::foo).call("O") + (null::bar).call("K")
@@ -0,0 +1,45 @@
// TODO: investigate should it be ran for JS or not
// IGNORE_BACKEND: JS
// WITH_REFLECT
import kotlin.reflect.*
import kotlin.test.assertEquals
class C(val x: Int, var y: Int)
val C.xx: Int
get() = x
var C.yy: Int
get() = y
set(value) { y = value }
val c = C(1, 2)
val c_xx = c::xx
val c_y = c::y
val c_yy = c::yy
fun box(): String {
assertEquals(1, c_xx.getter())
assertEquals(1, c_xx.getter.call())
assertEquals(2, c_yy.getter())
assertEquals(2, c_yy.getter.call())
c_y.setter(10)
assertEquals(10, c_yy.getter())
c_yy.setter(20)
assertEquals(20, c_y.getter())
assertEquals(20, c_yy.getter())
c_y.setter.call(100)
assertEquals(100, c_yy.getter.call())
c_yy.setter.call(200)
assertEquals(200, c_y.getter.call())
return "OK"
}
@@ -0,0 +1,16 @@
// TODO: investigate should it be ran for JS or not
// IGNORE_BACKEND: JS
// WITH_REFLECT
class Outer(val x: String) {
inner class Inner(val y: String) {
fun foo() = x + y
}
}
fun box(): String {
val innerCtor = Outer("O")::Inner
val inner = innerCtor.call("K")
return inner.foo()
}
@@ -0,0 +1,40 @@
// TODO: investigate should it be ran for JS or not
// IGNORE_BACKEND: JS
// WITH_REFLECT
// FILE: J.java
public class J {
public final int finalField;
public String mutableField;
public J(int f, String m) {
this.finalField = f;
this.mutableField = m;
}
}
// FILE: K.kt
import kotlin.reflect.*
import kotlin.reflect.jvm.*
import kotlin.test.assertEquals
fun box(): String {
val j = J(0, "")
val jf = j::finalField
val jm = j::mutableField
assertEquals(0, jf.getter())
assertEquals(0, jf.getter.call())
assertEquals("", jm.getter())
assertEquals("", jm.getter.call())
jm.setter("1")
assertEquals("1", j.mutableField)
jm.setter.call("2")
assertEquals("2", j.mutableField)
return "OK"
}
@@ -0,0 +1,35 @@
// TODO: investigate should it be ran for JS or not
// IGNORE_BACKEND: JS
// WITH_REFLECT
// FILE: J.java
public class J {
private final int param;
public J(int param) {
this.param = param;
}
public String foo(int[] arr, Object[] arr2, Integer y) {
return "" + param + arr[0] + arr2[0] + y;
}
}
// FILE: K.kt
import kotlin.reflect.*
import kotlin.reflect.jvm.*
import kotlin.test.assertEquals
fun box(): String {
val f = J(0)::foo
assertEquals(
listOf(IntArray::class.java, Array<Any>::class.java, Integer::class.java),
f.parameters.map { it.type.javaType }
)
assertEquals(String::class.java, f.returnType.javaType)
assertEquals("01A2", f.call(intArrayOf(1), arrayOf("A"), 2))
return "OK"
}
@@ -0,0 +1,53 @@
// TODO: investigate should it be ran for JS or not
// IGNORE_BACKEND: JS
// WITH_REFLECT
import kotlin.reflect.*
import kotlin.test.assertEquals
class Host {
companion object {
@JvmStatic val x = 1
@JvmStatic var y = 2
@JvmStatic val xx: Int
get() = x
@JvmStatic var yy: Int
get() = y
set(value) { y = value }
}
}
val c_x = Host.Companion::x
val c_xx = Host.Companion::xx
val c_y = Host.Companion::y
val c_yy = Host.Companion::yy
fun box(): String {
assertEquals(1, c_x.getter())
assertEquals(1, c_x.getter.call())
assertEquals(1, c_xx.getter())
assertEquals(1, c_xx.getter.call())
assertEquals(2, c_y.getter())
assertEquals(2, c_y.getter.call())
assertEquals(2, c_yy.getter())
assertEquals(2, c_yy.getter.call())
c_y.setter(10)
assertEquals(10, c_y.getter())
assertEquals(10, c_yy.getter())
c_yy.setter(20)
assertEquals(20, c_y.getter())
assertEquals(20, c_yy.getter())
c_y.setter.call(100)
assertEquals(100, c_yy.getter.call())
c_yy.setter.call(200)
assertEquals(200, c_y.getter.call())
return "OK"
}
@@ -0,0 +1,19 @@
// TODO: investigate should it be ran for JS or not
// IGNORE_BACKEND: JS
// WITH_REFLECT
import kotlin.reflect.*
object Host {
@JvmStatic fun foo(x: String) = x
}
class CompanionOwner {
companion object {
@JvmStatic fun bar(x: String) = x
}
}
fun box(): String =
(Host::foo).call("O") + (CompanionOwner.Companion::bar).call("K")
@@ -0,0 +1,51 @@
// TODO: investigate should it be ran for JS or not
// IGNORE_BACKEND: JS
// WITH_REFLECT
import kotlin.reflect.*
import kotlin.test.assertEquals
object Host {
@JvmStatic val x = 1
@JvmStatic var y = 2
@JvmStatic val xx: Int
get() = x
@JvmStatic var yy: Int
get() = y
set(value) { y = value }
}
val c_x = Host::x
val c_xx = Host::xx
val c_y = Host::y
val c_yy = Host::yy
fun box(): String {
assertEquals(1, c_x.getter())
assertEquals(1, c_x.getter.call())
assertEquals(1, c_xx.getter())
assertEquals(1, c_xx.getter.call())
assertEquals(2, c_y.getter())
assertEquals(2, c_y.getter.call())
assertEquals(2, c_yy.getter())
assertEquals(2, c_yy.getter.call())
c_y.setter(10)
assertEquals(10, c_y.getter())
assertEquals(10, c_yy.getter())
c_yy.setter(20)
assertEquals(20, c_y.getter())
assertEquals(20, c_yy.getter())
c_y.setter.call(100)
assertEquals(100, c_yy.getter.call())
c_yy.setter.call(200)
assertEquals(200, c_y.getter.call())
return "OK"
}
@@ -0,0 +1,14 @@
// TODO: investigate should it be ran for JS or not
// IGNORE_BACKEND: JS
// WITH_REFLECT
import kotlin.reflect.*
class C(val k: String) {
fun foo(s: String) = s + k
}
fun box(): String =
C("K")::foo.call("O")
@@ -0,0 +1,50 @@
// TODO: investigate should it be ran for JS or not
// IGNORE_BACKEND: JS
// WITH_REFLECT
import kotlin.reflect.*
import kotlin.test.assertEquals
class C(val x: Int, var y: Int) {
val xx: Int
get() = x
var yy: Int
get() = y
set(value) { y = value }
}
val c = C(1, 2)
val c_x = c::x
val c_xx = c::xx
val c_y = c::y
val c_yy = c::yy
fun box(): String {
assertEquals(1, c_x.getter())
assertEquals(1, c_x.getter.call())
assertEquals(1, c_xx.getter())
assertEquals(1, c_xx.getter.call())
assertEquals(2, c_y.getter())
assertEquals(2, c_y.getter.call())
assertEquals(2, c_yy.getter())
assertEquals(2, c_yy.getter.call())
c_y.setter(10)
assertEquals(10, c_y.getter())
assertEquals(10, c_yy.getter())
c_yy.setter(20)
assertEquals(20, c_y.getter())
assertEquals(20, c_yy.getter())
c_y.setter.call(100)
assertEquals(100, c_yy.getter.call())
c_yy.setter.call(200)
assertEquals(200, c_y.getter.call())
return "OK"
}
@@ -0,0 +1,19 @@
// TODO: investigate should it be ran for JS or not
// IGNORE_BACKEND: JS
// WITH_REFLECT
import kotlin.reflect.*
object Host {
fun foo(x: String) = x
}
class CompanionOwner {
companion object {
fun bar(x: String) = x
}
}
fun box(): String =
(Host::foo).call("O") + (CompanionOwner.Companion::bar).call("K")
@@ -0,0 +1,51 @@
// TODO: investigate should it be ran for JS or not
// IGNORE_BACKEND: JS
// WITH_REFLECT
import kotlin.reflect.*
import kotlin.test.assertEquals
object Host {
val x = 1
var y = 2
val xx: Int
get() = x
var yy: Int
get() = y
set(value) { y = value }
}
val c_x = Host::x
val c_xx = Host::xx
val c_y = Host::y
val c_yy = Host::yy
fun box(): String {
assertEquals(1, c_x.getter())
assertEquals(1, c_x.getter.call())
assertEquals(1, c_xx.getter())
assertEquals(1, c_xx.getter.call())
assertEquals(2, c_y.getter())
assertEquals(2, c_y.getter.call())
assertEquals(2, c_yy.getter())
assertEquals(2, c_yy.getter.call())
c_y.setter(10)
assertEquals(10, c_y.getter())
assertEquals(10, c_yy.getter())
c_yy.setter(20)
assertEquals(20, c_y.getter())
assertEquals(20, c_yy.getter())
c_y.setter.call(100)
assertEquals(100, c_yy.getter.call())
c_yy.setter.call(200)
assertEquals(200, c_y.getter.call())
return "OK"
}
@@ -0,0 +1,13 @@
// TODO: muted automatically, investigate should it be ran for JS or not
// IGNORE_BACKEND: JS
// WITH_REFLECT
import kotlin.test.assertEquals
fun String.extFun(k: String, s: String = "") = this + k + s
fun box(): String {
val sExtFun = "O"::extFun
return sExtFun.callBy(mapOf(sExtFun.parameters[0] to "K"))
}
@@ -0,0 +1,12 @@
// TODO: muted automatically, investigate should it be ran for JS or not
// IGNORE_BACKEND: JS
// WITH_REFLECT
import kotlin.test.assertEquals
val String.plusK: String
get() = this + "K"
fun box(): String =
("O"::plusK).getter.callBy(mapOf())
@@ -0,0 +1,19 @@
// TODO: muted automatically, investigate should it be ran for JS or not
// IGNORE_BACKEND: JS
// WITH_REFLECT
object Host {
@JvmStatic fun concat(s1: String, s2: String, s3: String = "K", s4: String = "x") =
s1 + s2 + s3 + s4
}
fun box(): String {
val concat = Host::concat
val concatParams = concat.parameters
return concat.callBy(mapOf(
concatParams[0] to "",
concatParams[1] to "O",
concatParams[3] to ""
))
}
@@ -5,6 +5,7 @@
import kotlin.test.assertTrue
import kotlin.test.assertFalse
import kotlin.test.assertEquals
inline fun Unit.foo(
noinline coroutine x: Unit.() -> Continuation<Unit>,
@@ -14,14 +15,13 @@ inline fun Unit.foo(
fun box(): String {
val p = Unit::foo.parameters
assertEquals(2, p.size)
assertFalse(p[0].isVararg)
assertFalse(p[0].isCoroutine)
assertTrue(p[0].isCoroutine)
assertFalse(p[1].isVararg)
assertTrue(p[1].isCoroutine)
assertTrue(p[2].isVararg)
assertFalse(p[2].isCoroutine)
assertTrue(p[1].isVararg)
assertFalse(p[1].isCoroutine)
return "OK"
}
@@ -0,0 +1,27 @@
// TODO: muted automatically, investigate should it be ran for JS or not
// IGNORE_BACKEND: JS
// WITH_REFLECT
import kotlin.reflect.KParameter
import kotlin.test.assertEquals
class Outer(val s1: String) {
inner class Inner(val s2: String, val s3: String = "K") {
val result = s1 + s2 + s3
}
}
fun KParameter.check(name: String) {
assertEquals(name, this.name!!)
assertEquals(KParameter.Kind.VALUE, this.kind)
}
fun box(): String {
val ctor = Outer("O")::Inner
val ctorPararms = ctor.parameters
ctorPararms[0].check("s2")
ctorPararms[1].check("s3")
return "OK"
}
@@ -0,0 +1,26 @@
// TODO: investigate should it be ran for JS or not
// IGNORE_BACKEND: JS
// WITH_REFLECT
import kotlin.reflect.*
import kotlin.reflect.jvm.*
import kotlin.test.assertEquals
object Host {
fun foo(i: Int, s: String) {}
}
fun box(): String {
val fooParams = Host::foo.parameters
assertEquals(2, fooParams.size)
assertEquals("i", fooParams[0].name)
assertEquals(Int::class.java, fooParams[0].type.javaType)
assertEquals("s", fooParams[1].name)
assertEquals(String::class.java, fooParams[1].type.javaType)
return "OK"
}
@@ -0,0 +1,35 @@
// TODO: investigate should it be ran for JS or not
// IGNORE_BACKEND: JS
// WITH_REFLECT
import kotlin.reflect.*
import kotlin.reflect.jvm.*
import kotlin.test.*
class C {
fun foo() {}
var bar = "OK"
}
fun C.extFun(i: Int) {}
fun KParameter.check(name: String) {
assertEquals(name, this.name!!)
assertEquals(KParameter.Kind.VALUE, this.kind)
}
fun box(): String {
val cFoo = C()::foo
val cBar = C()::bar
val cExtFun = C()::extFun
assertEquals(0, cFoo.parameters.size)
assertEquals(0, cBar.getter.parameters.size)
assertEquals(1, cBar.setter.parameters.size)
assertEquals(1, cExtFun.parameters.size)
cExtFun.parameters[0].check("i")
return "OK"
}