W3cubDocs

/Kotlin

KClass

Platform and version requirements:
interface KClass<T : Any>
Platform and version requirements: JVM (1.0), JS (1.1), Native (1.3)
interface KClass<T : Any> : 
    KDeclarationContainer, 
    KAnnotatedElement, 
    KClassifier

Represents a class and provides introspection capabilities. Instances of this class are obtainable by the ::class syntax. See the Kotlin language documentation for more information.

Parameters

T - the type of the class.

Properties

Platform and version requirements: JVM (1.0), JS (1.0)

constructors

All constructors declared in this class.

abstract val constructors: Collection<KFunction<T>>
Platform and version requirements: JVM (1.1), JS (1.1)

isAbstract

true if this class is abstract.

abstract val isAbstract: Boolean
Platform and version requirements: JVM (1.1), JS (1.1)

isCompanion

true if this class is a companion object. See the Kotlin language documentation for more information.

abstract val isCompanion: Boolean
Platform and version requirements: JVM (1.1), JS (1.1)

isData

true if this class is a data class. See the Kotlin language documentation for more information.

abstract val isData: Boolean
Platform and version requirements: JVM (1.1), JS (1.1)

isFinal

true if this class is final.

abstract val isFinal: Boolean
Platform and version requirements: JVM (1.1), JS (1.1)

isInner

true if this class is an inner class. See the Kotlin language documentation for more information.

abstract val isInner: Boolean
Platform and version requirements: JVM (1.1), JS (1.1)

isOpen

true if this class is open.

abstract val isOpen: Boolean
Platform and version requirements: JVM (1.1), JS (1.1)

isSealed

true if this class is sealed. See the Kotlin language documentation for more information.

abstract val isSealed: Boolean
Platform and version requirements: JVM (1.0), JS (1.0)

members

All functions and properties accessible in this class, including those declared in this class and all of its superclasses. Does not include constructors.

abstract val members: Collection<KCallable<*>>
Platform and version requirements: JVM (1.0), JS (1.0)

nestedClasses

All classes declared inside this class. This includes both inner and static nested classes.

abstract val nestedClasses: Collection<KClass<*>>
Platform and version requirements: JVM (1.0), JS (1.0)

objectInstance

The instance of the object declaration, or null if this class is not an object declaration.

abstract val objectInstance: T?
Platform and version requirements: JVM (1.0), JS (1.0), Native (1.0)

qualifiedName

The fully qualified dot-separated name of the class, or null if the class is local or it is an anonymous object literal.

abstract val qualifiedName: String?
Platform and version requirements: JVM (1.3), JS (1.3)

sealedSubclasses

The list of the immediate subclasses if this class is a sealed class, or an empty list otherwise.

abstract val sealedSubclasses: List<KClass<out T>>
Platform and version requirements: JVM (1.0), JS (1.0), Native (1.0)

simpleName

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).

abstract val simpleName: String?
Platform and version requirements: JVM (1.1), JS (1.1)

supertypes

The list of immediate supertypes of this class, in the order they are listed in the source code.

abstract val supertypes: List<KType>
Platform and version requirements: JVM (1.1), JS (1.1)

typeParameters

The list of type parameters of this class. This list does not include type parameters of outer classes.

abstract val typeParameters: List<KTypeParameter>
Platform and version requirements: JVM (1.1), JS (1.1)

visibility

Visibility of this class, or null if its visibility cannot be represented in Kotlin.

abstract val visibility: KVisibility?

Functions

Platform and version requirements: JVM (1.0), JS (1.0), Native (1.0)

equals

Returns true if this KClass instance represents the same Kotlin class as the class represented by other. On JVM this means that all of the following conditions are satisfied:

abstract fun equals(other: Any?): Boolean
Platform and version requirements: JVM (1.0), JS (1.0), Native (1.0)

hashCode

Returns a hash code value for the object. The general contract of hashCode is:

abstract fun hashCode(): Int
Platform and version requirements: JVM (1.1), JS (1.1), Native (1.1)

isInstance

Returns true if value is an instance of this class on a given platform.

abstract fun isInstance(value: Any?): Boolean

Extension Properties

Platform and version requirements: JVM (1.1)

allSuperclasses

All superclasses of this class, including indirect ones, in no particular order. 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.

val KClass<*>.allSuperclasses: Collection<KClass<*>>
Platform and version requirements: JVM (1.1)

allSupertypes

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.

val KClass<*>.allSupertypes: Collection<KType>
Platform and version requirements: JVM (1.1)

companionObject

Returns a KClass instance representing the companion object of a given class, or null if the class doesn't have a companion object.

val KClass<*>.companionObject: KClass<*>?
Platform and version requirements: JVM (1.1)

companionObjectInstance

Returns an instance of the companion object of a given class, or null if the class doesn't have a companion object.

val KClass<*>.companionObjectInstance: Any?
Platform and version requirements: JVM (1.1)

declaredFunctions

Returns all functions declared in this class. If this is a Java class, it includes all non-static methods (both extensions and non-extensions) declared in the class and the superclasses, as well as static methods declared in the class.

val KClass<*>.declaredFunctions: Collection<KFunction<*>>
Platform and version requirements: JVM (1.1)

declaredMemberExtensionFunctions

Returns extension functions declared in this class.

val KClass<*>.declaredMemberExtensionFunctions: Collection<KFunction<*>>
Platform and version requirements: JVM (1.1)

declaredMemberExtensionProperties

Returns extension properties declared in this class.

val <T : Any> KClass<T>.declaredMemberExtensionProperties: Collection<KProperty2<T, *, *>>
Platform and version requirements: JVM (1.1)

declaredMemberFunctions

Returns non-extension non-static functions declared in this class.

val KClass<*>.declaredMemberFunctions: Collection<KFunction<*>>
Platform and version requirements: JVM (1.1)

declaredMemberProperties

Returns non-extension properties declared in this class.

val <T : Any> KClass<T>.declaredMemberProperties: Collection<KProperty1<T, *>>
Platform and version requirements: JVM (1.1)

declaredMembers

Returns all functions and properties declared in this class. Does not include members declared in supertypes.

val KClass<*>.declaredMembers: Collection<KCallable<*>>
Platform and version requirements: JVM (1.1)

defaultType

Returns a type corresponding to the given class with type parameters of that class substituted as the corresponding arguments. For example, for class MyMap<K, V> defaultType would return the type MyMap<K, V>.

val KClass<*>.defaultType: KType
Platform and version requirements: JVM (1.1)

functions

Returns all functions declared in this class, including all non-static methods declared in the class and the superclasses, as well as static methods declared in the class.

val KClass<*>.functions: Collection<KFunction<*>>
Platform and version requirements: JVM (1.0)

java

Returns a Java Class instance corresponding to the given KClass instance.

val <T> KClass<T>.java: Class<T>
Platform and version requirements: JVM (1.0)

javaClass

val <T : Any> KClass<T>.javaClass: Class<KClass<T>>
Platform and version requirements: JVM (1.0)

javaObjectType

Returns a Java Class instance corresponding to the given KClass instance. In case of primitive types it returns corresponding wrapper classes.

val <T : Any> KClass<T>.javaObjectType: Class<T>
Platform and version requirements: JVM (1.0)

javaPrimitiveType

Returns a Java Class instance representing the primitive type corresponding to the given KClass if it exists.

val <T : Any> KClass<T>.javaPrimitiveType: Class<T>?
Platform and version requirements: JS (1.1)

js

Obtains a constructor reference for the given KClass.

val <T : Any> KClass<T>.js: JsClass<T>
Platform and version requirements: JVM (1.0)

jvmName

Returns the JVM name of the class represented by this KClass instance.

val KClass<*>.jvmName: String
Platform and version requirements: JVM (1.1)

memberExtensionFunctions

Returns extension functions declared in this class and all of its superclasses.

val KClass<*>.memberExtensionFunctions: Collection<KFunction<*>>
Platform and version requirements: JVM (1.1)

memberExtensionProperties

Returns extension properties declared in this class and all of its superclasses.

val <T : Any> KClass<T>.memberExtensionProperties: Collection<KProperty2<T, *, *>>
Platform and version requirements: JVM (1.1)

memberFunctions

Returns non-extension non-static functions declared in this class and all of its superclasses.

val KClass<*>.memberFunctions: Collection<KFunction<*>>
Platform and version requirements: JVM (1.1)

memberProperties

Returns non-extension properties declared in this class and all of its superclasses.

val <T : Any> KClass<T>.memberProperties: Collection<KProperty1<T, *>>
Platform and version requirements: JVM (1.1)

primaryConstructor

Returns the primary constructor of this class, or null if this class has no primary constructor. See the Kotlin language documentation for more information.

val <T : Any> KClass<T>.primaryConstructor: KFunction<T>?
Platform and version requirements: JVM (1.1)

starProjectedType

Creates an instance of KType with the given classifier, substituting all its type parameters with star projections. The resulting type is not marked as nullable and does not have any annotations.

val KClassifier.starProjectedType: KType
Platform and version requirements: JVM (1.1)

staticFunctions

Returns static functions declared in this class.

val KClass<*>.staticFunctions: Collection<KFunction<*>>
Platform and version requirements: JVM (1.1)

staticProperties

Returns static properties declared in this class. Only properties representing static fields of Java classes are considered static.

val KClass<*>.staticProperties: Collection<KProperty0<*>>
Platform and version requirements: JVM (1.1)

superclasses

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.

val KClass<*>.superclasses: List<KClass<*>>

Extension Functions

Platform and version requirements: JVM (1.1)

cast

Casts the given value to the class represented by this KClass object. Throws an exception if the value is null or if it is not an instance of this class.

fun <T : Any> KClass<T>.cast(value: Any?): T
Platform and version requirements: JVM (1.1)

createInstance

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.

fun <T : Any> KClass<T>.createInstance(): T
Platform and version requirements: JVM (1.1)

createType

Creates a KType instance with the given classifier, type arguments, nullability and annotations. If the number of passed type arguments is not equal to the total number of type parameters of a classifier, an exception is thrown. If any of the arguments does not satisfy the bounds of the corresponding type parameter, an exception is thrown.

fun KClassifier.createType(
    arguments: List<KTypeProjection> = emptyList(), 
    nullable: Boolean = false, 
    annotations: List<Annotation> = emptyList()
): KType
Platform and version requirements: JVM (1.1)

findAnnotation

Returns an annotation of the given type on this element.

fun <T : Annotation> KAnnotatedElement.findAnnotation(): T?
Platform and version requirements: JVM (1.3)

hasAnnotation

Returns true if this element is annotated with an annotation of type T.

fun <T : Annotation> KAnnotatedElement.hasAnnotation(): Boolean
Platform and version requirements: JVM (1.1)

isSubclassOf

Returns true if this class is the same or is a (possibly indirect) subclass of base, false otherwise.

fun KClass<*>.isSubclassOf(base: KClass<*>): Boolean
Platform and version requirements: JVM (1.1)

isSuperclassOf

Returns true if this class is the same or is a (possibly indirect) superclass of derived, false otherwise.

fun KClass<*>.isSuperclassOf(derived: KClass<*>): Boolean
Platform and version requirements: JVM (1.1)

safeCast

Casts the given value to the class represented by this KClass object. Returns null if the value is null or if it is not an instance of this class.

fun <T : Any> KClass<T>.safeCast(value: Any?): T?

© 2010–2019 JetBrains s.r.o.
Licensed under the Apache License, Version 2.0.
https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.reflect/-k-class/