mirror of
https://github.com/jlengrand/kotlin.git
synced 2026-05-11 08:31:30 +00:00
This was already working for accessor-based properties because the assertions were already generated into corresponding accessors, but for fields we must manually check the value against null
44 lines
1.3 KiB
Kotlin
Vendored
44 lines
1.3 KiB
Kotlin
Vendored
import kotlin.reflect.*
|
|
import kotlin.reflect.jvm.*
|
|
import kotlin.platform.platformStatic as static
|
|
|
|
class A {
|
|
private var foo: String = ""
|
|
}
|
|
|
|
object O {
|
|
private static var bar: String = ""
|
|
}
|
|
|
|
class CounterTest<T>(t: T) {
|
|
private var baz: String? = ""
|
|
private var generic: T = t
|
|
}
|
|
|
|
fun box(): String {
|
|
val p = A::class.memberProperties.single() as KMutableProperty1<A, String?>
|
|
p.isAccessible = true
|
|
try {
|
|
p.setter.call(A(), null)
|
|
return "Fail: exception should have been thrown"
|
|
} catch (e: IllegalArgumentException) {}
|
|
|
|
|
|
val o = O::class.memberProperties.single() as KMutableProperty1<O, String?>
|
|
o.isAccessible = true
|
|
try {
|
|
o.setter.call(O, null)
|
|
return "Fail: exception should have been thrown"
|
|
} catch (e: IllegalArgumentException) {}
|
|
|
|
|
|
val c = CounterTest::class.memberProperties.single { it.name == "baz" } as KMutableProperty1<CounterTest<*>, String?>
|
|
c.isAccessible = true
|
|
c.setter.call(CounterTest(""), null) // Should not fail, because CounterTest::baz is nullable
|
|
val d = CounterTest::class.memberProperties.single { it.name == "generic" } as KMutableProperty1<CounterTest<*>, String?>
|
|
d.isAccessible = true
|
|
d.setter.call(CounterTest(""), null) // Also should not fail, because we can't be sure about nullability of 'generic'
|
|
|
|
return "OK"
|
|
}
|