Support KParameter.kind: instance, extension receiver, or value

This commit is contained in:
Alexander Udalov
2015-08-20 16:27:09 -07:00
parent a73d02418d
commit d4825cf4f0
5 changed files with 48 additions and 3 deletions
@@ -0,0 +1,18 @@
import kotlin.reflect.*
import kotlin.reflect.KParameter.Kind.*
import kotlin.test.assertEquals
class A {
fun Int.foo(x: String) {}
inner class Inner(s: String) {}
}
fun box(): String {
val foo = A::class.memberExtensionFunctions.single()
assertEquals(listOf(INSTANCE, EXTENSION_RECEIVER, VALUE), foo.parameters.map { it.kind })
assertEquals(listOf(INSTANCE, VALUE), A::Inner.parameters.map { it.kind })
return "OK"
}
@@ -3597,6 +3597,12 @@ public class BlackBoxWithStdlibCodegenTestGenerated extends AbstractBlackBoxCode
doTestWithStdlib(fileName);
}
@TestMetadata("kinds.kt")
public void testKinds() throws Exception {
String fileName = JetTestUtils.navigationMetadata("compiler/testData/codegen/boxWithStdlib/reflection/parameters/kinds.kt");
doTestWithStdlib(fileName);
}
@TestMetadata("propertySetter.kt")
public void testPropertySetter() throws Exception {
String fileName = JetTestUtils.navigationMetadata("compiler/testData/codegen/boxWithStdlib/reflection/parameters/propertySetter.kt");
@@ -40,4 +40,24 @@ public interface KParameter : KAnnotatedElement {
* not the individual element.
*/
public val type: KType
/**
* Kind of this parameter.
*/
public val kind: Kind
/**
* Kind represents a particular position of the parameter declaration in the source code,
* such as an instance, an extension receiver parameter or a value parameter.
*/
public enum class Kind {
/** Instance required to make a call to the member, or an outer class instance for an inner class constructor. */
INSTANCE,
/** Extension receiver of an extension function or property. */
EXTENSION_RECEIVER,
/** Ordinary named value parameter. */
VALUE,
}
}
@@ -36,15 +36,15 @@ interface KCallableImpl<out R> : KCallable<R>, KAnnotatedElementImpl {
var index = 0
if (descriptor.dispatchReceiverParameter != null) {
result.add(KParameterImpl(this, index++) { descriptor.dispatchReceiverParameter!! })
result.add(KParameterImpl(this, index++, KParameter.Kind.INSTANCE) { descriptor.dispatchReceiverParameter!! })
}
if (descriptor.extensionReceiverParameter != null) {
result.add(KParameterImpl(this, index++) { descriptor.extensionReceiverParameter!! })
result.add(KParameterImpl(this, index++, KParameter.Kind.EXTENSION_RECEIVER) { descriptor.extensionReceiverParameter!! })
}
for (i in descriptor.valueParameters.indices) {
result.add(KParameterImpl(this, index++) { descriptor.valueParameters[i] })
result.add(KParameterImpl(this, index++, KParameter.Kind.VALUE) { descriptor.valueParameters[i] })
}
result.trimToSize()
@@ -25,6 +25,7 @@ import kotlin.reflect.KType
class KParameterImpl(
val callable: KCallableImpl<*>,
override val index: Int,
override val kind: KParameter.Kind,
computeDescriptor: () -> ParameterDescriptor
) : KParameter, KAnnotatedElementImpl {
private val descriptor: ParameterDescriptor by ReflectProperties.lazySoft(computeDescriptor)