Support KCallable.call

#KT-2187 Fixed
This commit is contained in:
Alexander Udalov
2015-07-16 21:45:28 +03:00
parent ffe32e0b6d
commit e079b8f425
22 changed files with 462 additions and 3 deletions
@@ -0,0 +1,25 @@
class A
data class D(val s: String)
fun box(): String {
val a = A()
assert(A::equals.call(a, a))
assert(!A::equals.call(a, 0))
assert(A::hashCode.call(a) == A::hashCode.call(a))
assert(A::toString.call(a).startsWith("A@"))
assert(D::equals.call(D("foo"), D("foo")))
assert(!D::equals.call(D("foo"), D("bar")))
assert(D::hashCode.call(D("foo")) == D::hashCode.call(D("foo")))
assert(D::toString.call(D("foo")) == "D(s=foo)")
assert(Int::equals.call(-1, -1))
assert(Int::hashCode.call(0) != Int::hashCode.call(1))
assert(Int::toString.call(42) == "42")
assert(String::equals.call("beer", "beer"))
String::hashCode.call("beer")
return String::toString.call("OK")
}
@@ -0,0 +1,17 @@
// FULL_JDK
import java.lang.reflect.InvocationTargetException
fun fail(message: String) {
throw AssertionError(message)
}
fun box(): String {
try {
::fail.call("OK")
} catch (e: InvocationTargetException) {
return e.getTargetException().getMessage().toString()
}
return "Fail: no exception was thrown"
}
@@ -0,0 +1,8 @@
class A {
class Nested(val result: String)
inner class Inner(val result: String)
}
fun box(): String {
return (A::Nested).call("O").result + (A::Inner).call((::A).call(), "K").result
}
@@ -0,0 +1,17 @@
import kotlin.platform.platformStatic as static
object Obj {
static fun foo() {}
}
class C {
companion object {
static fun bar() {}
}
}
fun box(): String {
(Obj::foo).call(Obj)
(C.Companion::bar).call(C.Companion)
return "OK"
}
@@ -0,0 +1,37 @@
import kotlin.platform.platformStatic as static
object Obj {
static fun foo(s: String) {}
static fun bar() {}
static fun sly(obj: Obj) {}
}
fun box(): String {
// This should succeed
(Obj::foo).call(Obj, "")
(Obj::bar).call(Obj)
(Obj::sly).call(Obj, Obj)
// This shouldn't: first argument should always be Obj
try {
(Obj::foo).call(null, "")
return "Fail foo"
} catch (e: IllegalArgumentException) {}
try {
(Obj::bar).call("")
return "Fail bar"
} catch (e: IllegalArgumentException) {}
try {
(Obj::sly).call(Obj)
return "Fail sly 1"
} catch (e: IllegalArgumentException) {}
try {
(Obj::sly).call(null, Obj)
return "Fail sly 2"
} catch (e: IllegalArgumentException) {}
return "OK"
}
@@ -0,0 +1,17 @@
import kotlin.reflect.*
import kotlin.reflect.jvm.*
import kotlin.test.*
class A(private var result: String)
fun box(): String {
val a = A("abc")
val p = A::class.declaredProperties.single() as KMutableProperty1<A, String>
p.accessible = true
assertEquals("abc", p.call(a))
assertEquals(Unit, p.setter.call(a, "def"))
assertEquals("def", p.getter.call(a))
return "OK"
}
@@ -0,0 +1,47 @@
import kotlin.reflect.*
import kotlin.test.assertEquals
val p0 = 1
val Int.p1: Int get() = this
class A {
val Int.p2: Int get() = this
}
var globalCounter = 0
var mp0 = 1
set(value) { globalCounter += value }
var Int.mp1: Int
get() = this
set(value) { globalCounter += value }
class B {
var Int.mp2: Int
get() = this
set(value) { globalCounter += value }
}
fun box(): String {
assertEquals(1, (::p0).call())
assertEquals(1, (::p0).getter.call())
assertEquals(2, (Int::p1).call(2))
assertEquals(2, (Int::p1).getter.call(2))
val p2 = A::class.extensionProperties.single()
assertEquals(3, p2.call(A(), 3))
assertEquals(3, p2.getter.call(A(), 3))
assertEquals(1, (::mp0).call())
assertEquals(1, (::mp0).getter.call())
assertEquals(2, (Int::mp1).call(2))
assertEquals(2, (Int::mp1).getter.call(2))
val mp2 = B::class.extensionProperties.single() as KMutableProperty2
assertEquals(3, mp2.call(B(), 3))
assertEquals(3, mp2.getter.call(B(), 3))
assertEquals(Unit, (::mp0).setter.call(1))
assertEquals(Unit, (Int::mp1).setter.call(0, 3))
assertEquals(Unit, mp2.setter.call(B(), 0, 5))
if (globalCounter != 9) return "Fail: $globalCounter"
return "OK"
}
@@ -0,0 +1,24 @@
import kotlin.test.assertEquals
fun foo() {}
class A {
fun bar() {}
}
object O {
kotlin.platform.platformStatic fun baz() {}
}
fun nullableUnit(unit: Boolean): Unit? = if (unit) Unit else null
fun box(): String {
assertEquals(Unit, ::foo.call())
assertEquals(Unit, A::bar.call(A()))
assertEquals(Unit, O::baz.call(O))
assertEquals(Unit, (::nullableUnit).call(true))
assertEquals(null, (::nullableUnit).call(false))
return "OK"
}
@@ -0,0 +1,6 @@
class A(val result: String)
fun box(): String {
val a = (::A).call("OK")
return a.result
}
@@ -0,0 +1,16 @@
class A {
fun foo(x: Int, y: Int) = x + y
}
fun box(): String {
val x = (A::foo).call(A(), 42, 239)
if (x != 281) return "Fail: $x"
try {
(A::foo).call()
return "Fail: no exception"
}
catch (e: Exception) {}
return "OK"
}
@@ -0,0 +1,27 @@
fun String.foo(): Int = length()
var state = "Fail"
fun bar(result: String) {
state = result
}
fun box(): String {
val f = (String::foo).call("abc")
if (f != 3) return "Fail: $f"
try {
(String::foo).call()
return "Fail: IllegalArgumentException should have been thrown"
}
catch (e: IllegalArgumentException) {}
try {
(String::foo).call(42)
return "Fail: IllegalArgumentException should have been thrown"
}
catch (e: IllegalArgumentException) {}
(::bar).call("OK")
return state
}