Add SinceKotlin("1.1") annotation to new reflection API
This commit is contained in:
@@ -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")
|
||||
|
||||
|
||||
Reference in New Issue
Block a user