Fix KFunction.javaMethod for property accessors

This commit is contained in:
Alexander Udalov
2015-07-23 23:26:49 +03:00
parent 9c4ba711b8
commit 262099c899
4 changed files with 52 additions and 2 deletions
@@ -0,0 +1,34 @@
import kotlin.reflect.jvm.*
import kotlin.test.*
var foo = "foo"
class A {
var bar = "bar"
}
fun box(): String {
val fooGetter = ::foo.getter.javaMethod ?: return "Fail fooGetter"
assertEquals("foo", fooGetter.invoke(null))
val fooSetter = ::foo.setter.javaMethod ?: return "Fail fooSetter"
fooSetter.invoke(null, "foof")
assertEquals("foof", foo)
assertNull(::foo.getter.javaConstructor)
assertNull(::foo.setter.javaConstructor)
val a = A()
val barGetter = A::bar.getter.javaMethod ?: return "Fail barGetter"
assertEquals("bar", barGetter.invoke(a))
val barSetter = A::bar.setter.javaMethod ?: return "Fail barSetter"
barSetter.invoke(a, "barb")
assertEquals("barb", a.bar)
assertNull(A::bar.getter.javaConstructor)
assertNull(A::bar.setter.javaConstructor)
return "OK"
}
@@ -3237,6 +3237,12 @@ public class BlackBoxWithStdlibCodegenTestGenerated extends AbstractBlackBoxCode
doTestWithStdlib(fileName);
}
@TestMetadata("propertyAccessors.kt")
public void testPropertyAccessors() throws Exception {
String fileName = JetTestUtils.navigationMetadata("compiler/testData/codegen/boxWithStdlib/reflection/mapping/propertyAccessors.kt");
doTestWithStdlib(fileName);
}
@TestMetadata("syntheticFields.kt")
public void testSyntheticFields() throws Exception {
String fileName = JetTestUtils.navigationMetadata("compiler/testData/codegen/boxWithStdlib/reflection/mapping/syntheticFields.kt");
@@ -57,6 +57,8 @@ interface KMutablePropertyImpl<R> : KMutableProperty<R>, KPropertyImpl<R> {
override val setter: Setter<R>
abstract class Setter<R> : KMutableProperty.Setter<R>, KPropertyImpl.Accessor<R>, KCallableImpl<Unit> {
abstract override val property: KMutablePropertyImpl<R>
override val name: String get() = "<set-${property.name}>"
override val descriptor: PropertySetterDescriptor by ReflectProperties.lazySoft {
@@ -68,7 +68,12 @@ public val KMutableProperty<*>.javaSetter: Method?
* or `null` if this function is a constructor or cannot be represented by a Java [Method].
*/
public val KFunction<*>.javaMethod: Method?
get() = ((this as? KFunctionImpl)?.caller as? FunctionCaller.Method)?.method
get() = when (this) {
is KFunctionImpl -> (caller as? FunctionCaller.Method)?.method
is KPropertyImpl.Getter<*> -> property.javaGetter
is KMutablePropertyImpl.Setter<*> -> property.javaSetter
else -> null
}
/**
* Returns a Java [Constructor] instance corresponding to the given Kotlin function,
@@ -76,7 +81,10 @@ public val KFunction<*>.javaMethod: Method?
*/
@suppress("UNCHECKED_CAST")
public val <T> KFunction<T>.javaConstructor: Constructor<T>?
get() = ((this as? KFunctionImpl)?.caller as? FunctionCaller.Constructor)?.constructor as? Constructor<T>
get() = when (this) {
is KFunctionImpl -> (caller as? FunctionCaller.Constructor)?.constructor as? Constructor<T>
else -> null
}