Runtime component for full Kotlin reflection support, enabling inspection and manipulation of Kotlin classes, functions, properties, and types at runtime
—
Basic reflection operations for classes, types, and callables providing the foundational interfaces and operations for runtime introspection of Kotlin code.
Obtain and work with class references for runtime introspection.
/**
* Represents a class and provides introspection capabilities
* @param T the type of the class
*/
interface KClass<T : Any> : KClassifier {
/** Simple name of the class as declared in source, or null if anonymous */
val simpleName: String?
/** Fully qualified dot-separated name, or null if local/anonymous */
val qualifiedName: String?
/** Returns true if value is an instance of this class */
fun isInstance(value: Any?): Boolean
}Usage Examples:
// Get class reference using ::class syntax
val stringClass = String::class
val myClass = MyClass::class
// Check class properties
println(stringClass.simpleName) // "String"
println(stringClass.qualifiedName) // "kotlin.String"
// Instance checking
val obj: Any = "hello"
println(stringClass.isInstance(obj)) // trueWork with type information including generics, nullability, and type arguments.
/**
* Represents a type with optional type arguments and nullability
*/
interface KType {
/** The declaration of the classifier used in this type */
val classifier: KClassifier?
/** Type arguments passed for the parameters of the classifier */
val arguments: List<KTypeProjection>
/** True if this type was marked nullable in source code */
val isMarkedNullable: Boolean
}
/**
* Returns runtime representation of the given reified type as KType
*/
inline fun <reified T> typeOf(): KTypeUsage Examples:
// Get type references
val stringType = typeOf<String>()
val listType = typeOf<List<String>>()
val nullableType = typeOf<String?>()
// Inspect type properties
println(listType.classifier) // class kotlin.collections.List
println(listType.arguments.size) // 1
println(nullableType.isMarkedNullable) // true
// Access type arguments
val elementType = listType.arguments.first().type
println(elementType?.classifier) // class kotlin.StringAccess and inspect functions and properties as callable entities.
/**
* Represents a callable entity, such as a function or property
* @param R return type of the callable
*/
interface KCallable<out R> {
/** The name of this callable as declared in source code */
val name: String
}
/**
* Represents a function with introspection capabilities
*/
interface KFunction<out R> : KCallable<R>, Function<R>
/**
* Represents a property, such as a named val or var declaration
* @param V the type of the property value
*/
interface KProperty<out V> : KCallable<V>Usage Examples:
class Example {
val property: String = "value"
fun method(x: Int): String = x.toString()
}
// Get callable references
val propertyRef = Example::property
val methodRef = Example::method
// Access callable information
println(propertyRef.name) // "property"
println(methodRef.name) // "method"
// Check callable types
println(propertyRef is KProperty<*>) // true
println(methodRef is KFunction<*>) // trueWork with type classifiers including classes and type parameters.
/**
* A classifier is either a class or a type parameter
*/
interface KClassifier
/**
* Represents a type parameter of a class or function
*/
interface KTypeParameter : KClassifier {
val name: String
val upperBounds: List<KType>
val variance: KVariance
val isReified: Boolean
}Perform fundamental type and class casting operations.
/**
* Casts the given value to the class represented by this KClass
* Throws exception if value is null or not an instance
*/
fun <T : Any> KClass<T>.cast(value: Any?): T
/**
* Casts the given value to the class represented by this KClass
* Returns null if value is null or not an instance
*/
fun <T : Any> KClass<T>.safeCast(value: Any?): T?Usage Examples:
val stringClass = String::class
val anyValue: Any = "hello"
// Safe casting
val result = stringClass.safeCast(anyValue)
println(result) // "hello"
// Exception throwing cast
try {
val number = stringClass.cast(123) // throws ClassCastException
} catch (e: ClassCastException) {
println("Cast failed: ${e.message}")
}Install with Tessl CLI
npx tessl i tessl/maven-org-jetbrains-kotlin--kotlin-reflect