From ed1490dbc43f88696f82e5307df43269ecbb32b1 Mon Sep 17 00:00:00 2001 From: Alexander Udalov Date: Wed, 12 Oct 2016 13:13:52 +0300 Subject: [PATCH] Add SinceKotlin("1.1") annotation to new reflection API --- core/builtins/src/kotlin/reflect/KCallable.kt | 5 +++++ core/builtins/src/kotlin/reflect/KClass.kt | 10 ++++++++++ core/builtins/src/kotlin/reflect/KClassifier.kt | 4 ++-- core/builtins/src/kotlin/reflect/KFunction.kt | 6 ++++++ core/builtins/src/kotlin/reflect/KParameter.kt | 4 ++++ core/builtins/src/kotlin/reflect/KProperty.kt | 2 ++ core/builtins/src/kotlin/reflect/KType.kt | 3 +++ core/builtins/src/kotlin/reflect/KTypeParameter.kt | 1 + core/builtins/src/kotlin/reflect/KVariance.kt | 1 + core/builtins/src/kotlin/reflect/KVisibility.kt | 1 + core/reflection.jvm/src/kotlin/reflect/KCallables.kt | 4 ++++ core/reflection.jvm/src/kotlin/reflect/KClasses.kt | 9 +++++++++ core/reflection.jvm/src/kotlin/reflect/KClassifiers.kt | 2 ++ core/reflection.jvm/src/kotlin/reflect/KTypes.kt | 3 +++ .../reflection.jvm/src/kotlin/reflect/jvm/KTypesJvm.kt | 1 + 15 files changed, 54 insertions(+), 2 deletions(-) diff --git a/core/builtins/src/kotlin/reflect/KCallable.kt b/core/builtins/src/kotlin/reflect/KCallable.kt index 4f9510f4e62..b1f60b8cce1 100644 --- a/core/builtins/src/kotlin/reflect/KCallable.kt +++ b/core/builtins/src/kotlin/reflect/KCallable.kt @@ -47,6 +47,7 @@ public interface KCallable : KAnnotatedElement { /** * The list of type parameters of this callable. */ + @SinceKotlin("1.1") public val typeParameters: List /** @@ -66,20 +67,24 @@ public interface KCallable : KAnnotatedElement { /** * Visibility of this callable, or `null` if its visibility cannot be represented in Kotlin. */ + @SinceKotlin("1.1") public val visibility: KVisibility? /** * `true` if this callable is `final`. */ + @SinceKotlin("1.1") public val isFinal: Boolean /** * `true` if this callable is `open`. */ + @SinceKotlin("1.1") public val isOpen: Boolean /** * `true` if this callable is `abstract`. */ + @SinceKotlin("1.1") public val isAbstract: Boolean } diff --git a/core/builtins/src/kotlin/reflect/KClass.kt b/core/builtins/src/kotlin/reflect/KClass.kt index c7dd12cb519..7310893f25c 100644 --- a/core/builtins/src/kotlin/reflect/KClass.kt +++ b/core/builtins/src/kotlin/reflect/KClass.kt @@ -67,31 +67,37 @@ public interface KClass : KDeclarationContainer, KAnnotatedElement, KCl /** * The list of type parameters of this class. This list does *not* include type parameters of outer classes. */ + @SinceKotlin("1.1") public val typeParameters: List /** * The list of immediate supertypes of this class, in the order they are listed in the source code. */ + @SinceKotlin("1.1") public val supertypes: List /** * Visibility of this class, or `null` if its visibility cannot be represented in Kotlin. */ + @SinceKotlin("1.1") public val visibility: KVisibility? /** * `true` if this class is `final`. */ + @SinceKotlin("1.1") public val isFinal: Boolean /** * `true` if this class is `open`. */ + @SinceKotlin("1.1") public val isOpen: Boolean /** * `true` if this class is `abstract`. */ + @SinceKotlin("1.1") public val isAbstract: Boolean /** @@ -99,6 +105,7 @@ public interface KClass : KDeclarationContainer, KAnnotatedElement, KCl * See the [Kotlin language documentation](https://kotlinlang.org/docs/reference/classes.html#sealed-classes) * for more information. */ + @SinceKotlin("1.1") public val isSealed: Boolean /** @@ -106,6 +113,7 @@ public interface KClass : KDeclarationContainer, KAnnotatedElement, KCl * See the [Kotlin language documentation](https://kotlinlang.org/docs/reference/data-classes.html) * for more information. */ + @SinceKotlin("1.1") public val isData: Boolean /** @@ -113,6 +121,7 @@ public interface KClass : KDeclarationContainer, KAnnotatedElement, KCl * See the [Kotlin language documentation](https://kotlinlang.org/docs/reference/nested-classes.html#inner-classes) * for more information. */ + @SinceKotlin("1.1") public val isInner: Boolean /** @@ -120,6 +129,7 @@ public interface KClass : KDeclarationContainer, KAnnotatedElement, KCl * See the [Kotlin language documentation](https://kotlinlang.org/docs/reference/object-declarations.html#companion-objects) * for more information. */ + @SinceKotlin("1.1") public val isCompanion: Boolean /** diff --git a/core/builtins/src/kotlin/reflect/KClassifier.kt b/core/builtins/src/kotlin/reflect/KClassifier.kt index d6328026a5f..6d92f4c63b2 100644 --- a/core/builtins/src/kotlin/reflect/KClassifier.kt +++ b/core/builtins/src/kotlin/reflect/KClassifier.kt @@ -18,7 +18,7 @@ package kotlin.reflect /** * A classifier is either a class, or a type parameter, or a type alias. - * - * TODO: improve doc */ +// TODO: improve doc +@SinceKotlin("1.1") public interface KClassifier diff --git a/core/builtins/src/kotlin/reflect/KFunction.kt b/core/builtins/src/kotlin/reflect/KFunction.kt index bdc14d5d31e..bb72c9315a9 100644 --- a/core/builtins/src/kotlin/reflect/KFunction.kt +++ b/core/builtins/src/kotlin/reflect/KFunction.kt @@ -25,6 +25,7 @@ public interface KFunction : KCallable, Function { * See the [Kotlin language documentation](https://kotlinlang.org/docs/reference/inline-functions.html) * for more information. */ + @SinceKotlin("1.1") public val isInline: Boolean /** @@ -32,6 +33,7 @@ public interface KFunction : KCallable, Function { * See the [Kotlin language documentation](https://kotlinlang.org/docs/reference/java-interop.html#using-jni-with-kotlin) * for more information. */ + @SinceKotlin("1.1") public val isExternal: Boolean /** @@ -39,6 +41,7 @@ public interface KFunction : KCallable, Function { * See the [Kotlin language documentation](https://kotlinlang.org/docs/reference/operator-overloading.html) * for more information. */ + @SinceKotlin("1.1") public val isOperator: Boolean /** @@ -46,6 +49,7 @@ public interface KFunction : KCallable, Function { * See the [Kotlin language documentation](https://kotlinlang.org/docs/reference/functions.html#infix-notation) * for more information. */ + @SinceKotlin("1.1") public val isInfix: Boolean /** @@ -53,10 +57,12 @@ public interface KFunction : KCallable, Function { * See the [Kotlin language documentation](https://kotlinlang.org/docs/reference/functions.html#tail-recursive-functions) * for more information. */ + @SinceKotlin("1.1") public val isTailrec: Boolean /** * `true` if this is a suspending function. */ + @SinceKotlin("1.1") public val isSuspend: Boolean } diff --git a/core/builtins/src/kotlin/reflect/KParameter.kt b/core/builtins/src/kotlin/reflect/KParameter.kt index b44bf8721b7..f885fe002bb 100644 --- a/core/builtins/src/kotlin/reflect/KParameter.kt +++ b/core/builtins/src/kotlin/reflect/KParameter.kt @@ -75,6 +75,7 @@ public interface KParameter : KAnnotatedElement { * See the [Kotlin language documentation](https://kotlinlang.org/docs/reference/functions.html#variable-number-of-arguments-varargs) * for more information. */ + @SinceKotlin("1.1") public val isVararg: Boolean /** @@ -82,6 +83,7 @@ public interface KParameter : KAnnotatedElement { * See the [Kotlin language documentation](https://kotlinlang.org/docs/reference/inline-functions.html#noinline) * for more information. */ + @SinceKotlin("1.1") public val isNoinline: Boolean /** @@ -89,10 +91,12 @@ public interface KParameter : KAnnotatedElement { * See the [Kotlin language documentation](https://kotlinlang.org/docs/reference/inline-functions.html#non-local-returns) * for more information. */ + @SinceKotlin("1.1") public val isCrossinline: Boolean /** * `true` if this parameter is `coroutine`. */ + @SinceKotlin("1.1") public val isCoroutine: Boolean } diff --git a/core/builtins/src/kotlin/reflect/KProperty.kt b/core/builtins/src/kotlin/reflect/KProperty.kt index d53d10d8433..ace48795a4b 100644 --- a/core/builtins/src/kotlin/reflect/KProperty.kt +++ b/core/builtins/src/kotlin/reflect/KProperty.kt @@ -30,6 +30,7 @@ public interface KProperty : KCallable { * See the [Kotlin language documentation](https://kotlinlang.org/docs/reference/properties.html#late-initialized-properties) * for more information. */ + @SinceKotlin("1.1") public val isLateinit: Boolean /** @@ -37,6 +38,7 @@ public interface KProperty : KCallable { * See the [Kotlin language documentation](https://kotlinlang.org/docs/reference/properties.html#compile-time-constants) * for more information. */ + @SinceKotlin("1.1") public val isConst: Boolean /** The getter of this property, used to obtain the value of the property. */ diff --git a/core/builtins/src/kotlin/reflect/KType.kt b/core/builtins/src/kotlin/reflect/KType.kt index 79c91b01a8b..f3f48fdbe29 100644 --- a/core/builtins/src/kotlin/reflect/KType.kt +++ b/core/builtins/src/kotlin/reflect/KType.kt @@ -27,6 +27,7 @@ public interface KType { * * Returns `null` if this type is not denotable in Kotlin, for example if it is an intersection type. */ + @SinceKotlin("1.1") public val classifier: KClassifier? /** @@ -37,6 +38,7 @@ public interface KType { * then its outer class, and so on. * For example, in the type `Outer.Inner` the returned list is `[C, D, A, B]`. */ + @SinceKotlin("1.1") public val arguments: List /** @@ -68,6 +70,7 @@ public interface KType { * See the [Kotlin language documentation](http://kotlinlang.org/docs/reference/generics.html#type-projections) * for more information. */ +@SinceKotlin("1.1") public data class KTypeProjection private constructor( /** * The use-site variance specified in the projection, or `null` if this is a star projection. diff --git a/core/builtins/src/kotlin/reflect/KTypeParameter.kt b/core/builtins/src/kotlin/reflect/KTypeParameter.kt index f6e610fadb4..a01761dfd02 100644 --- a/core/builtins/src/kotlin/reflect/KTypeParameter.kt +++ b/core/builtins/src/kotlin/reflect/KTypeParameter.kt @@ -21,6 +21,7 @@ package kotlin.reflect * See the [Kotlin language documentation](http://kotlinlang.org/docs/reference/generics.html#generics) * for more information. */ +@SinceKotlin("1.1") public interface KTypeParameter : KClassifier { /** * The name of this type parameter as it was declared in the source code. diff --git a/core/builtins/src/kotlin/reflect/KVariance.kt b/core/builtins/src/kotlin/reflect/KVariance.kt index 48b7dd32c73..9a237cbbb9b 100644 --- a/core/builtins/src/kotlin/reflect/KVariance.kt +++ b/core/builtins/src/kotlin/reflect/KVariance.kt @@ -26,6 +26,7 @@ package kotlin.reflect * @see [KTypeParameter.variance] * @see [KTypeProjection] */ +@SinceKotlin("1.1") enum class KVariance { /** * The affected type parameter or type is *invariant*, which means it has no variance applied to it. diff --git a/core/builtins/src/kotlin/reflect/KVisibility.kt b/core/builtins/src/kotlin/reflect/KVisibility.kt index 031188e762b..6ddd7e1769b 100644 --- a/core/builtins/src/kotlin/reflect/KVisibility.kt +++ b/core/builtins/src/kotlin/reflect/KVisibility.kt @@ -26,6 +26,7 @@ package kotlin.reflect * See the [Kotlin language documentation](https://kotlinlang.org/docs/reference/visibility-modifiers.html) * for more information. */ +@SinceKotlin("1.1") enum class KVisibility { /** * Visibility of declarations marked with the `public` modifier, or with no modifier at all. diff --git a/core/reflection.jvm/src/kotlin/reflect/KCallables.kt b/core/reflection.jvm/src/kotlin/reflect/KCallables.kt index 2f304529872..ea3faaac5b6 100644 --- a/core/reflection.jvm/src/kotlin/reflect/KCallables.kt +++ b/core/reflection.jvm/src/kotlin/reflect/KCallables.kt @@ -21,6 +21,7 @@ package kotlin.reflect * Returns a parameter representing the `this` instance needed to call this callable, * or `null` if this callable is not a member of a class and thus doesn't take such parameter. */ +@SinceKotlin("1.1") val KCallable<*>.instanceParameter: KParameter? get() = parameters.singleOrNull { it.kind == KParameter.Kind.INSTANCE } @@ -28,18 +29,21 @@ val KCallable<*>.instanceParameter: KParameter? * Returns a parameter representing the extension receiver instance needed to call this callable, * or `null` if this callable is not an extension. */ +@SinceKotlin("1.1") val KCallable<*>.extensionReceiverParameter: KParameter? get() = parameters.singleOrNull { it.kind == KParameter.Kind.EXTENSION_RECEIVER } /** * Returns parameters of this callable, excluding the `this` instance and the extension receiver parameter. */ +@SinceKotlin("1.1") val KCallable<*>.valueParameters: List get() = parameters.filter { it.kind == KParameter.Kind.VALUE } /** * Returns the parameter of this callable with the given name, or `null` if there's no such parameter. */ +@SinceKotlin("1.1") fun KCallable<*>.findParameterByName(name: String): KParameter? { return parameters.singleOrNull { it.name == name } } diff --git a/core/reflection.jvm/src/kotlin/reflect/KClasses.kt b/core/reflection.jvm/src/kotlin/reflect/KClasses.kt index da61d385112..a65be582445 100644 --- a/core/reflection.jvm/src/kotlin/reflect/KClasses.kt +++ b/core/reflection.jvm/src/kotlin/reflect/KClasses.kt @@ -71,6 +71,7 @@ val KClass<*>.defaultType: KType * Returns all functions and properties declared in this class. * Does not include members declared in supertypes. */ +@SinceKotlin("1.1") val KClass<*>.declaredMembers: Collection> get() = (this as KClassImpl).data().declaredMembers @@ -161,6 +162,7 @@ private val KCallableImpl<*>.isNotExtension: Boolean * Immediate superclasses of this class, in the order they are listed in the source code. * Includes superclasses and superinterfaces of the class, but does not include the class itself. */ +@SinceKotlin("1.1") val KClass<*>.superclasses: List> get() = supertypes.mapNotNull { it.classifier as? KClass<*> } @@ -168,6 +170,7 @@ val KClass<*>.superclasses: List> * All supertypes of this class, including indirect ones, in no particular order. * There is not more than one type in the returned collection that has any given classifier. */ +@SinceKotlin("1.1") val KClass<*>.allSupertypes: Collection get() = DFS.dfs( supertypes, @@ -201,6 +204,7 @@ val KClass<*>.allSupertypes: Collection * Includes superclasses and superinterfaces of the class, but does not include the class itself. * The returned collection does not contain more than one instance of any given class. */ +@SinceKotlin("1.1") val KClass<*>.allSuperclasses: Collection> get() = allSupertypes.map { supertype -> supertype.classifier as? KClass<*> ?: throw KotlinReflectionInternalError("Supertype not a class: $supertype") @@ -209,6 +213,7 @@ val KClass<*>.allSuperclasses: Collection> /** * Returns `true` if `this` class is the same or is a (possibly indirect) subclass of [base], `false` otherwise. */ +@SinceKotlin("1.1") fun KClass<*>.isSubclassOf(base: KClass<*>): Boolean = this == base || DFS.ifAny(listOf(this), KClass<*>::superclasses) { it == base } @@ -216,6 +221,7 @@ fun KClass<*>.isSubclassOf(base: KClass<*>): Boolean = /** * Returns `true` if `this` class is the same or is a (possibly indirect) superclass of [derived], `false` otherwise. */ +@SinceKotlin("1.1") fun KClass<*>.isSuperclassOf(derived: KClass<*>): Boolean = derived.isSubclassOf(this) @@ -227,6 +233,7 @@ fun KClass<*>.isSuperclassOf(derived: KClass<*>): Boolean = * @see [KClass.isInstance] * @see [KClass.safeCast] */ +@SinceKotlin("1.1") fun KClass.cast(value: Any?): T { if (!isInstance(value)) throw TypeCastException("Value cannot be cast to $qualifiedName") return value as T @@ -239,6 +246,7 @@ fun KClass.cast(value: Any?): T { * @see [KClass.isInstance] * @see [KClass.cast] */ +@SinceKotlin("1.1") fun KClass.safeCast(value: Any?): T? { return if (isInstance(value)) value as T else null } @@ -248,6 +256,7 @@ fun KClass.safeCast(value: Any?): T? { * Creates a new instance of the class, calling a constructor which either has no parameters or all parameters of which are optional * (see [KParameter.isOptional]). If there are no or many such constructors, an exception is thrown. */ +@SinceKotlin("1.1") fun KClass.createInstance(): T { // TODO: throw a meaningful exception val noArgsConstructor = constructors.singleOrNull { it.parameters.all(KParameter::isOptional) } diff --git a/core/reflection.jvm/src/kotlin/reflect/KClassifiers.kt b/core/reflection.jvm/src/kotlin/reflect/KClassifiers.kt index c1aaf5be444..21dcb0ff7f3 100644 --- a/core/reflection.jvm/src/kotlin/reflect/KClassifiers.kt +++ b/core/reflection.jvm/src/kotlin/reflect/KClassifiers.kt @@ -33,6 +33,7 @@ import kotlin.reflect.jvm.internal.KTypeImpl * the list should follow with arguments for the type parameters of its outer class, and so forth until a class is * not `inner`, or is declared on the top level. */ +@SinceKotlin("1.1") fun KClassifier.createType( arguments: List = emptyList(), nullable: Boolean = false, @@ -81,6 +82,7 @@ private fun createKotlinType( * * @see [KClassifier.createType] */ +@SinceKotlin("1.1") val KClassifier.starProjectedType: KType get() { val descriptor = (this as? KClassifierImpl)?.descriptor diff --git a/core/reflection.jvm/src/kotlin/reflect/KTypes.kt b/core/reflection.jvm/src/kotlin/reflect/KTypes.kt index 0eae7304fb7..69485c44b2b 100644 --- a/core/reflection.jvm/src/kotlin/reflect/KTypes.kt +++ b/core/reflection.jvm/src/kotlin/reflect/KTypes.kt @@ -25,6 +25,7 @@ import kotlin.reflect.jvm.internal.KTypeImpl /** * Returns a new type with the same classifier, arguments and annotations as the given type, and with the given nullability. */ +@SinceKotlin("1.1") fun KType.withNullability(nullable: Boolean): KType { if (isMarkedNullable) { return if (nullable) this else KTypeImpl(TypeUtils.makeNotNullable((this as KTypeImpl).type)) { javaType } @@ -41,6 +42,7 @@ fun KType.withNullability(nullable: Boolean): KType { /** * Returns `true` if `this` type is the same or is a subtype of [other], `false` otherwise. */ +@SinceKotlin("1.1") fun KType.isSubtypeOf(other: KType): Boolean { return (this as KTypeImpl).type.isSubtypeOf((other as KTypeImpl).type) } @@ -48,6 +50,7 @@ fun KType.isSubtypeOf(other: KType): Boolean { /** * Returns `true` if `this` type is the same or is a supertype of [other], `false` otherwise. */ +@SinceKotlin("1.1") fun KType.isSupertypeOf(other: KType): Boolean { return other.isSubtypeOf(this) } diff --git a/core/reflection.jvm/src/kotlin/reflect/jvm/KTypesJvm.kt b/core/reflection.jvm/src/kotlin/reflect/jvm/KTypesJvm.kt index 163ab3d6c52..84be2a34248 100644 --- a/core/reflection.jvm/src/kotlin/reflect/jvm/KTypesJvm.kt +++ b/core/reflection.jvm/src/kotlin/reflect/jvm/KTypesJvm.kt @@ -26,6 +26,7 @@ import kotlin.reflect.jvm.internal.KTypeImpl /** * Returns the [KClass] instance representing the runtime class to which this type is erased to on JVM. */ +@SinceKotlin("1.1") val KType.jvmErasure: KClass<*> get() = classifier?.jvmErasure ?: throw KotlinReflectionInternalError("Cannot calculate JVM erasure for type: $this")