From 2ab0f489b5a1858ddbd6a5e93df89b8ffba84f5e Mon Sep 17 00:00:00 2001 From: Alexander Udalov Date: Mon, 18 Jul 2016 21:08:53 +0300 Subject: [PATCH] Reflection: add KClassifier, KTypeParameter, KTypeParameter.name The ability to obtain classifier from types is going to be added later --- core/builtins/src/kotlin/reflect/KClass.kt | 2 +- .../src/kotlin/reflect/KClassifier.kt | 24 +++++++++++++++ .../src/kotlin/reflect/KTypeParameter.kt | 29 +++++++++++++++++++ .../kotlin/reflect/jvm/internal/KClassImpl.kt | 5 ++-- .../reflect/jvm/internal/KClassifierImpl.kt | 23 +++++++++++++++ .../jvm/internal/KTypeParameterImpl.kt | 25 ++++++++++++++++ 6 files changed, 105 insertions(+), 3 deletions(-) create mode 100644 core/builtins/src/kotlin/reflect/KClassifier.kt create mode 100644 core/builtins/src/kotlin/reflect/KTypeParameter.kt create mode 100644 core/reflection.jvm/src/kotlin/reflect/jvm/internal/KClassifierImpl.kt create mode 100644 core/reflection.jvm/src/kotlin/reflect/jvm/internal/KTypeParameterImpl.kt diff --git a/core/builtins/src/kotlin/reflect/KClass.kt b/core/builtins/src/kotlin/reflect/KClass.kt index b3327ae0c2a..7d5b98bc8a9 100644 --- a/core/builtins/src/kotlin/reflect/KClass.kt +++ b/core/builtins/src/kotlin/reflect/KClass.kt @@ -24,7 +24,7 @@ package kotlin.reflect * * @param T the type of the class. */ -public interface KClass : KDeclarationContainer, KAnnotatedElement { +public interface KClass : KDeclarationContainer, KAnnotatedElement, KClassifier { /** * The simple name of the class as it was declared in the source code, * or `null` if the class has no name (if, for example, it is an anonymous object literal). diff --git a/core/builtins/src/kotlin/reflect/KClassifier.kt b/core/builtins/src/kotlin/reflect/KClassifier.kt new file mode 100644 index 00000000000..d6328026a5f --- /dev/null +++ b/core/builtins/src/kotlin/reflect/KClassifier.kt @@ -0,0 +1,24 @@ +/* + * Copyright 2010-2016 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package kotlin.reflect + +/** + * A classifier is either a class, or a type parameter, or a type alias. + * + * TODO: improve doc + */ +public interface KClassifier diff --git a/core/builtins/src/kotlin/reflect/KTypeParameter.kt b/core/builtins/src/kotlin/reflect/KTypeParameter.kt new file mode 100644 index 00000000000..08cdcfbba1f --- /dev/null +++ b/core/builtins/src/kotlin/reflect/KTypeParameter.kt @@ -0,0 +1,29 @@ +/* + * Copyright 2010-2016 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package kotlin.reflect + +/** + * Represents a declaration of a type parameter of a class or a callable. + * See the [Kotlin language documentation](http://kotlinlang.org/docs/reference/generics.html#generics) + * for more information. + */ +public interface KTypeParameter : KClassifier { + /** + * The name of this type parameter as it was declared in the source code. + */ + public val name: String +} diff --git a/core/reflection.jvm/src/kotlin/reflect/jvm/internal/KClassImpl.kt b/core/reflection.jvm/src/kotlin/reflect/jvm/internal/KClassImpl.kt index a53230fa41d..39ae54e76ec 100644 --- a/core/reflection.jvm/src/kotlin/reflect/jvm/internal/KClassImpl.kt +++ b/core/reflection.jvm/src/kotlin/reflect/jvm/internal/KClassImpl.kt @@ -32,7 +32,8 @@ import kotlin.reflect.KClass import kotlin.reflect.KFunction import kotlin.reflect.KotlinReflectionInternalError -internal class KClassImpl(override val jClass: Class) : KDeclarationContainerImpl(), KClass, KAnnotatedElementImpl { +internal class KClassImpl(override val jClass: Class) : + KDeclarationContainerImpl(), KClass, KClassifierImpl, KAnnotatedElementImpl { private val descriptor_ = ReflectProperties.lazySoft { val classId = classId @@ -43,7 +44,7 @@ internal class KClassImpl(override val jClass: Class) : KDeclaration descriptor ?: reportUnresolvedClass() } - val descriptor: ClassDescriptor + override val descriptor: ClassDescriptor get() = descriptor_() override val annotated: Annotated get() = descriptor diff --git a/core/reflection.jvm/src/kotlin/reflect/jvm/internal/KClassifierImpl.kt b/core/reflection.jvm/src/kotlin/reflect/jvm/internal/KClassifierImpl.kt new file mode 100644 index 00000000000..ad9d4ad2e2f --- /dev/null +++ b/core/reflection.jvm/src/kotlin/reflect/jvm/internal/KClassifierImpl.kt @@ -0,0 +1,23 @@ +/* + * Copyright 2010-2016 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package kotlin.reflect.jvm.internal + +import org.jetbrains.kotlin.descriptors.ClassifierDescriptor + +internal interface KClassifierImpl { + val descriptor: ClassifierDescriptor +} diff --git a/core/reflection.jvm/src/kotlin/reflect/jvm/internal/KTypeParameterImpl.kt b/core/reflection.jvm/src/kotlin/reflect/jvm/internal/KTypeParameterImpl.kt new file mode 100644 index 00000000000..14d23c6b7bf --- /dev/null +++ b/core/reflection.jvm/src/kotlin/reflect/jvm/internal/KTypeParameterImpl.kt @@ -0,0 +1,25 @@ +/* + * Copyright 2010-2016 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package kotlin.reflect.jvm.internal + +import org.jetbrains.kotlin.descriptors.TypeParameterDescriptor +import kotlin.reflect.KTypeParameter + +internal class KTypeParameterImpl(override val descriptor: TypeParameterDescriptor) : KTypeParameter, KClassifierImpl { + override val name: String + get() = descriptor.name.asString() +}