Add SinceKotlin("1.1") annotation to new reflection API

This commit is contained in:
Alexander Udalov
2016-10-12 13:13:52 +03:00
parent 8828e671f5
commit ed1490dbc4
15 changed files with 54 additions and 2 deletions
@@ -47,6 +47,7 @@ public interface KCallable<out R> : KAnnotatedElement {
/** /**
* The list of type parameters of this callable. * The list of type parameters of this callable.
*/ */
@SinceKotlin("1.1")
public val typeParameters: List<KTypeParameter> public val typeParameters: List<KTypeParameter>
/** /**
@@ -66,20 +67,24 @@ public interface KCallable<out R> : KAnnotatedElement {
/** /**
* Visibility of this callable, or `null` if its visibility cannot be represented in Kotlin. * Visibility of this callable, or `null` if its visibility cannot be represented in Kotlin.
*/ */
@SinceKotlin("1.1")
public val visibility: KVisibility? public val visibility: KVisibility?
/** /**
* `true` if this callable is `final`. * `true` if this callable is `final`.
*/ */
@SinceKotlin("1.1")
public val isFinal: Boolean public val isFinal: Boolean
/** /**
* `true` if this callable is `open`. * `true` if this callable is `open`.
*/ */
@SinceKotlin("1.1")
public val isOpen: Boolean public val isOpen: Boolean
/** /**
* `true` if this callable is `abstract`. * `true` if this callable is `abstract`.
*/ */
@SinceKotlin("1.1")
public val isAbstract: Boolean public val isAbstract: Boolean
} }
@@ -67,31 +67,37 @@ public interface KClass<T : Any> : KDeclarationContainer, KAnnotatedElement, KCl
/** /**
* The list of type parameters of this class. This list does *not* include type parameters of outer classes. * 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<KTypeParameter> public val typeParameters: List<KTypeParameter>
/** /**
* The list of immediate supertypes of this class, in the order they are listed in the source code. * 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<KType> public val supertypes: List<KType>
/** /**
* Visibility of this class, or `null` if its visibility cannot be represented in Kotlin. * Visibility of this class, or `null` if its visibility cannot be represented in Kotlin.
*/ */
@SinceKotlin("1.1")
public val visibility: KVisibility? public val visibility: KVisibility?
/** /**
* `true` if this class is `final`. * `true` if this class is `final`.
*/ */
@SinceKotlin("1.1")
public val isFinal: Boolean public val isFinal: Boolean
/** /**
* `true` if this class is `open`. * `true` if this class is `open`.
*/ */
@SinceKotlin("1.1")
public val isOpen: Boolean public val isOpen: Boolean
/** /**
* `true` if this class is `abstract`. * `true` if this class is `abstract`.
*/ */
@SinceKotlin("1.1")
public val isAbstract: Boolean public val isAbstract: Boolean
/** /**
@@ -99,6 +105,7 @@ public interface KClass<T : Any> : KDeclarationContainer, KAnnotatedElement, KCl
* See the [Kotlin language documentation](https://kotlinlang.org/docs/reference/classes.html#sealed-classes) * See the [Kotlin language documentation](https://kotlinlang.org/docs/reference/classes.html#sealed-classes)
* for more information. * for more information.
*/ */
@SinceKotlin("1.1")
public val isSealed: Boolean public val isSealed: Boolean
/** /**
@@ -106,6 +113,7 @@ public interface KClass<T : Any> : KDeclarationContainer, KAnnotatedElement, KCl
* See the [Kotlin language documentation](https://kotlinlang.org/docs/reference/data-classes.html) * See the [Kotlin language documentation](https://kotlinlang.org/docs/reference/data-classes.html)
* for more information. * for more information.
*/ */
@SinceKotlin("1.1")
public val isData: Boolean public val isData: Boolean
/** /**
@@ -113,6 +121,7 @@ public interface KClass<T : Any> : KDeclarationContainer, KAnnotatedElement, KCl
* See the [Kotlin language documentation](https://kotlinlang.org/docs/reference/nested-classes.html#inner-classes) * See the [Kotlin language documentation](https://kotlinlang.org/docs/reference/nested-classes.html#inner-classes)
* for more information. * for more information.
*/ */
@SinceKotlin("1.1")
public val isInner: Boolean public val isInner: Boolean
/** /**
@@ -120,6 +129,7 @@ public interface KClass<T : Any> : KDeclarationContainer, KAnnotatedElement, KCl
* See the [Kotlin language documentation](https://kotlinlang.org/docs/reference/object-declarations.html#companion-objects) * See the [Kotlin language documentation](https://kotlinlang.org/docs/reference/object-declarations.html#companion-objects)
* for more information. * for more information.
*/ */
@SinceKotlin("1.1")
public val isCompanion: Boolean public val isCompanion: Boolean
/** /**
@@ -18,7 +18,7 @@ package kotlin.reflect
/** /**
* A classifier is either a class, or a type parameter, or a type alias. * 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 public interface KClassifier
@@ -25,6 +25,7 @@ public interface KFunction<out R> : KCallable<R>, Function<R> {
* See the [Kotlin language documentation](https://kotlinlang.org/docs/reference/inline-functions.html) * See the [Kotlin language documentation](https://kotlinlang.org/docs/reference/inline-functions.html)
* for more information. * for more information.
*/ */
@SinceKotlin("1.1")
public val isInline: Boolean public val isInline: Boolean
/** /**
@@ -32,6 +33,7 @@ public interface KFunction<out R> : KCallable<R>, Function<R> {
* See the [Kotlin language documentation](https://kotlinlang.org/docs/reference/java-interop.html#using-jni-with-kotlin) * See the [Kotlin language documentation](https://kotlinlang.org/docs/reference/java-interop.html#using-jni-with-kotlin)
* for more information. * for more information.
*/ */
@SinceKotlin("1.1")
public val isExternal: Boolean public val isExternal: Boolean
/** /**
@@ -39,6 +41,7 @@ public interface KFunction<out R> : KCallable<R>, Function<R> {
* See the [Kotlin language documentation](https://kotlinlang.org/docs/reference/operator-overloading.html) * See the [Kotlin language documentation](https://kotlinlang.org/docs/reference/operator-overloading.html)
* for more information. * for more information.
*/ */
@SinceKotlin("1.1")
public val isOperator: Boolean public val isOperator: Boolean
/** /**
@@ -46,6 +49,7 @@ public interface KFunction<out R> : KCallable<R>, Function<R> {
* See the [Kotlin language documentation](https://kotlinlang.org/docs/reference/functions.html#infix-notation) * See the [Kotlin language documentation](https://kotlinlang.org/docs/reference/functions.html#infix-notation)
* for more information. * for more information.
*/ */
@SinceKotlin("1.1")
public val isInfix: Boolean public val isInfix: Boolean
/** /**
@@ -53,10 +57,12 @@ public interface KFunction<out R> : KCallable<R>, Function<R> {
* See the [Kotlin language documentation](https://kotlinlang.org/docs/reference/functions.html#tail-recursive-functions) * See the [Kotlin language documentation](https://kotlinlang.org/docs/reference/functions.html#tail-recursive-functions)
* for more information. * for more information.
*/ */
@SinceKotlin("1.1")
public val isTailrec: Boolean public val isTailrec: Boolean
/** /**
* `true` if this is a suspending function. * `true` if this is a suspending function.
*/ */
@SinceKotlin("1.1")
public val isSuspend: Boolean public val isSuspend: Boolean
} }
@@ -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) * See the [Kotlin language documentation](https://kotlinlang.org/docs/reference/functions.html#variable-number-of-arguments-varargs)
* for more information. * for more information.
*/ */
@SinceKotlin("1.1")
public val isVararg: Boolean 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) * See the [Kotlin language documentation](https://kotlinlang.org/docs/reference/inline-functions.html#noinline)
* for more information. * for more information.
*/ */
@SinceKotlin("1.1")
public val isNoinline: Boolean 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) * See the [Kotlin language documentation](https://kotlinlang.org/docs/reference/inline-functions.html#non-local-returns)
* for more information. * for more information.
*/ */
@SinceKotlin("1.1")
public val isCrossinline: Boolean public val isCrossinline: Boolean
/** /**
* `true` if this parameter is `coroutine`. * `true` if this parameter is `coroutine`.
*/ */
@SinceKotlin("1.1")
public val isCoroutine: Boolean public val isCoroutine: Boolean
} }
@@ -30,6 +30,7 @@ public interface KProperty<out R> : KCallable<R> {
* See the [Kotlin language documentation](https://kotlinlang.org/docs/reference/properties.html#late-initialized-properties) * See the [Kotlin language documentation](https://kotlinlang.org/docs/reference/properties.html#late-initialized-properties)
* for more information. * for more information.
*/ */
@SinceKotlin("1.1")
public val isLateinit: Boolean public val isLateinit: Boolean
/** /**
@@ -37,6 +38,7 @@ public interface KProperty<out R> : KCallable<R> {
* See the [Kotlin language documentation](https://kotlinlang.org/docs/reference/properties.html#compile-time-constants) * See the [Kotlin language documentation](https://kotlinlang.org/docs/reference/properties.html#compile-time-constants)
* for more information. * for more information.
*/ */
@SinceKotlin("1.1")
public val isConst: Boolean public val isConst: Boolean
/** The getter of this property, used to obtain the value of the property. */ /** The getter of this property, used to obtain the value of the property. */
@@ -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. * 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? public val classifier: KClassifier?
/** /**
@@ -37,6 +38,7 @@ public interface KType {
* then its outer class, and so on. * then its outer class, and so on.
* For example, in the type `Outer<A, B>.Inner<C, D>` the returned list is `[C, D, A, B]`. * For example, in the type `Outer<A, B>.Inner<C, D>` the returned list is `[C, D, A, B]`.
*/ */
@SinceKotlin("1.1")
public val arguments: List<KTypeProjection> public val arguments: List<KTypeProjection>
/** /**
@@ -68,6 +70,7 @@ public interface KType {
* See the [Kotlin language documentation](http://kotlinlang.org/docs/reference/generics.html#type-projections) * See the [Kotlin language documentation](http://kotlinlang.org/docs/reference/generics.html#type-projections)
* for more information. * for more information.
*/ */
@SinceKotlin("1.1")
public data class KTypeProjection private constructor( public data class KTypeProjection private constructor(
/** /**
* The use-site variance specified in the projection, or `null` if this is a star projection. * The use-site variance specified in the projection, or `null` if this is a star projection.
@@ -21,6 +21,7 @@ package kotlin.reflect
* See the [Kotlin language documentation](http://kotlinlang.org/docs/reference/generics.html#generics) * See the [Kotlin language documentation](http://kotlinlang.org/docs/reference/generics.html#generics)
* for more information. * for more information.
*/ */
@SinceKotlin("1.1")
public interface KTypeParameter : KClassifier { public interface KTypeParameter : KClassifier {
/** /**
* The name of this type parameter as it was declared in the source code. * The name of this type parameter as it was declared in the source code.
@@ -26,6 +26,7 @@ package kotlin.reflect
* @see [KTypeParameter.variance] * @see [KTypeParameter.variance]
* @see [KTypeProjection] * @see [KTypeProjection]
*/ */
@SinceKotlin("1.1")
enum class KVariance { enum class KVariance {
/** /**
* The affected type parameter or type is *invariant*, which means it has no variance applied to it. * The affected type parameter or type is *invariant*, which means it has no variance applied to it.
@@ -26,6 +26,7 @@ package kotlin.reflect
* See the [Kotlin language documentation](https://kotlinlang.org/docs/reference/visibility-modifiers.html) * See the [Kotlin language documentation](https://kotlinlang.org/docs/reference/visibility-modifiers.html)
* for more information. * for more information.
*/ */
@SinceKotlin("1.1")
enum class KVisibility { enum class KVisibility {
/** /**
* Visibility of declarations marked with the `public` modifier, or with no modifier at all. * Visibility of declarations marked with the `public` modifier, or with no modifier at all.
@@ -21,6 +21,7 @@ package kotlin.reflect
* Returns a parameter representing the `this` instance needed to call this callable, * 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. * 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? val KCallable<*>.instanceParameter: KParameter?
get() = parameters.singleOrNull { it.kind == KParameter.Kind.INSTANCE } 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, * Returns a parameter representing the extension receiver instance needed to call this callable,
* or `null` if this callable is not an extension. * or `null` if this callable is not an extension.
*/ */
@SinceKotlin("1.1")
val KCallable<*>.extensionReceiverParameter: KParameter? val KCallable<*>.extensionReceiverParameter: KParameter?
get() = parameters.singleOrNull { it.kind == KParameter.Kind.EXTENSION_RECEIVER } get() = parameters.singleOrNull { it.kind == KParameter.Kind.EXTENSION_RECEIVER }
/** /**
* Returns parameters of this callable, excluding the `this` instance and the extension receiver parameter. * Returns parameters of this callable, excluding the `this` instance and the extension receiver parameter.
*/ */
@SinceKotlin("1.1")
val KCallable<*>.valueParameters: List<KParameter> val KCallable<*>.valueParameters: List<KParameter>
get() = parameters.filter { it.kind == KParameter.Kind.VALUE } 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. * 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? { fun KCallable<*>.findParameterByName(name: String): KParameter? {
return parameters.singleOrNull { it.name == name } return parameters.singleOrNull { it.name == name }
} }
@@ -71,6 +71,7 @@ val KClass<*>.defaultType: KType
* Returns all functions and properties declared in this class. * Returns all functions and properties declared in this class.
* Does not include members declared in supertypes. * Does not include members declared in supertypes.
*/ */
@SinceKotlin("1.1")
val KClass<*>.declaredMembers: Collection<KCallable<*>> val KClass<*>.declaredMembers: Collection<KCallable<*>>
get() = (this as KClassImpl).data().declaredMembers 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. * 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. * Includes superclasses and superinterfaces of the class, but does not include the class itself.
*/ */
@SinceKotlin("1.1")
val KClass<*>.superclasses: List<KClass<*>> val KClass<*>.superclasses: List<KClass<*>>
get() = supertypes.mapNotNull { it.classifier as? KClass<*> } get() = supertypes.mapNotNull { it.classifier as? KClass<*> }
@@ -168,6 +170,7 @@ val KClass<*>.superclasses: List<KClass<*>>
* All supertypes of this class, including indirect ones, in no particular order. * 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. * There is not more than one type in the returned collection that has any given classifier.
*/ */
@SinceKotlin("1.1")
val KClass<*>.allSupertypes: Collection<KType> val KClass<*>.allSupertypes: Collection<KType>
get() = DFS.dfs( get() = DFS.dfs(
supertypes, supertypes,
@@ -201,6 +204,7 @@ val KClass<*>.allSupertypes: Collection<KType>
* Includes superclasses and superinterfaces of the class, but does not include the class itself. * 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. * The returned collection does not contain more than one instance of any given class.
*/ */
@SinceKotlin("1.1")
val KClass<*>.allSuperclasses: Collection<KClass<*>> val KClass<*>.allSuperclasses: Collection<KClass<*>>
get() = allSupertypes.map { supertype -> get() = allSupertypes.map { supertype ->
supertype.classifier as? KClass<*> ?: throw KotlinReflectionInternalError("Supertype not a class: $supertype") supertype.classifier as? KClass<*> ?: throw KotlinReflectionInternalError("Supertype not a class: $supertype")
@@ -209,6 +213,7 @@ val KClass<*>.allSuperclasses: Collection<KClass<*>>
/** /**
* Returns `true` if `this` class is the same or is a (possibly indirect) subclass of [base], `false` otherwise. * 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 = fun KClass<*>.isSubclassOf(base: KClass<*>): Boolean =
this == base || this == base ||
DFS.ifAny(listOf(this), KClass<*>::superclasses) { it == 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. * 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 = fun KClass<*>.isSuperclassOf(derived: KClass<*>): Boolean =
derived.isSubclassOf(this) derived.isSubclassOf(this)
@@ -227,6 +233,7 @@ fun KClass<*>.isSuperclassOf(derived: KClass<*>): Boolean =
* @see [KClass.isInstance] * @see [KClass.isInstance]
* @see [KClass.safeCast] * @see [KClass.safeCast]
*/ */
@SinceKotlin("1.1")
fun <T : Any> KClass<T>.cast(value: Any?): T { fun <T : Any> KClass<T>.cast(value: Any?): T {
if (!isInstance(value)) throw TypeCastException("Value cannot be cast to $qualifiedName") if (!isInstance(value)) throw TypeCastException("Value cannot be cast to $qualifiedName")
return value as T return value as T
@@ -239,6 +246,7 @@ fun <T : Any> KClass<T>.cast(value: Any?): T {
* @see [KClass.isInstance] * @see [KClass.isInstance]
* @see [KClass.cast] * @see [KClass.cast]
*/ */
@SinceKotlin("1.1")
fun <T : Any> KClass<T>.safeCast(value: Any?): T? { fun <T : Any> KClass<T>.safeCast(value: Any?): T? {
return if (isInstance(value)) value as T else null return if (isInstance(value)) value as T else null
} }
@@ -248,6 +256,7 @@ fun <T : Any> KClass<T>.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 * 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. * (see [KParameter.isOptional]). If there are no or many such constructors, an exception is thrown.
*/ */
@SinceKotlin("1.1")
fun <T : Any> KClass<T>.createInstance(): T { fun <T : Any> KClass<T>.createInstance(): T {
// TODO: throw a meaningful exception // TODO: throw a meaningful exception
val noArgsConstructor = constructors.singleOrNull { it.parameters.all(KParameter::isOptional) } val noArgsConstructor = constructors.singleOrNull { it.parameters.all(KParameter::isOptional) }
@@ -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 * 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. * not `inner`, or is declared on the top level.
*/ */
@SinceKotlin("1.1")
fun KClassifier.createType( fun KClassifier.createType(
arguments: List<KTypeProjection> = emptyList(), arguments: List<KTypeProjection> = emptyList(),
nullable: Boolean = false, nullable: Boolean = false,
@@ -81,6 +82,7 @@ private fun createKotlinType(
* *
* @see [KClassifier.createType] * @see [KClassifier.createType]
*/ */
@SinceKotlin("1.1")
val KClassifier.starProjectedType: KType val KClassifier.starProjectedType: KType
get() { get() {
val descriptor = (this as? KClassifierImpl)?.descriptor val descriptor = (this as? KClassifierImpl)?.descriptor
@@ -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. * 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 { fun KType.withNullability(nullable: Boolean): KType {
if (isMarkedNullable) { if (isMarkedNullable) {
return if (nullable) this else KTypeImpl(TypeUtils.makeNotNullable((this as KTypeImpl).type)) { javaType } 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. * 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 { fun KType.isSubtypeOf(other: KType): Boolean {
return (this as KTypeImpl).type.isSubtypeOf((other as KTypeImpl).type) 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. * 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 { fun KType.isSupertypeOf(other: KType): Boolean {
return other.isSubtypeOf(this) return other.isSubtypeOf(this)
} }
@@ -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. * Returns the [KClass] instance representing the runtime class to which this type is erased to on JVM.
*/ */
@SinceKotlin("1.1")
val KType.jvmErasure: KClass<*> val KType.jvmErasure: KClass<*>
get() = classifier?.jvmErasure ?: throw KotlinReflectionInternalError("Cannot calculate JVM erasure for type: $this") get() = classifier?.jvmErasure ?: throw KotlinReflectionInternalError("Cannot calculate JVM erasure for type: $this")