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.
*/
@SinceKotlin("1.1")
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.
*/
@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
}
@@ -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.
*/
@SinceKotlin("1.1")
public val typeParameters: List<KTypeParameter>
/**
* 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>
/**
* 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<T : Any> : 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<T : Any> : 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<T : Any> : 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<T : Any> : 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
/**
@@ -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
@@ -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)
* for more information.
*/
@SinceKotlin("1.1")
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)
* for more information.
*/
@SinceKotlin("1.1")
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)
* for more information.
*/
@SinceKotlin("1.1")
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)
* for more information.
*/
@SinceKotlin("1.1")
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)
* for more information.
*/
@SinceKotlin("1.1")
public val isTailrec: Boolean
/**
* `true` if this is a suspending function.
*/
@SinceKotlin("1.1")
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)
* 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
}
@@ -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)
* for more information.
*/
@SinceKotlin("1.1")
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)
* for more information.
*/
@SinceKotlin("1.1")
public val isConst: Boolean
/** 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.
*/
@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<A, B>.Inner<C, D>` the returned list is `[C, D, A, B]`.
*/
@SinceKotlin("1.1")
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)
* 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.
@@ -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.
@@ -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.
@@ -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.
@@ -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<KParameter>
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 }
}
@@ -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<KCallable<*>>
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<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.
* There is not more than one type in the returned collection that has any given classifier.
*/
@SinceKotlin("1.1")
val KClass<*>.allSupertypes: Collection<KType>
get() = DFS.dfs(
supertypes,
@@ -201,6 +204,7 @@ val KClass<*>.allSupertypes: Collection<KType>
* 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<KClass<*>>
get() = allSupertypes.map { 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.
*/
@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 <T : Any> KClass<T>.cast(value: Any?): T {
if (!isInstance(value)) throw TypeCastException("Value cannot be cast to $qualifiedName")
return value as T
@@ -239,6 +246,7 @@ fun <T : Any> KClass<T>.cast(value: Any?): T {
* @see [KClass.isInstance]
* @see [KClass.cast]
*/
@SinceKotlin("1.1")
fun <T : Any> KClass<T>.safeCast(value: Any?): T? {
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
* (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 {
// TODO: throw a meaningful exception
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
* not `inner`, or is declared on the top level.
*/
@SinceKotlin("1.1")
fun KClassifier.createType(
arguments: List<KTypeProjection> = 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
@@ -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)
}
@@ -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")