or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

callable-system.mdclass-introspection.mdcore-reflection.mdindex.mdjvm-interoperability.mdtype-system.md
tile.json

tessl/maven-org-jetbrains-kotlin--kotlin-reflect

Runtime component for full Kotlin reflection support, enabling inspection and manipulation of Kotlin classes, functions, properties, and types at runtime

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
mavenpkg:maven/org.jetbrains.kotlin/kotlin-reflect@2.2.x

To install, run

npx @tessl/cli install tessl/maven-org-jetbrains-kotlin--kotlin-reflect@2.2.0

index.mddocs/

Kotlin Reflect

Kotlin Reflect is the runtime component for full Kotlin reflection support, enabling inspection and manipulation of Kotlin classes, functions, properties, and types at runtime. It provides comprehensive reflection capabilities including class metadata access, callable invocation, property inspection, type system navigation, and seamless JVM interoperability.

Package Information

  • Package Name: kotlin-reflect
  • Package Type: maven
  • Language: Kotlin
  • Installation: implementation("org.jetbrains.kotlin:kotlin-reflect:$kotlin_version") (should match your Kotlin version)

Core Imports

import kotlin.reflect.*
import kotlin.reflect.full.*
import kotlin.reflect.jvm.*

Basic Usage

import kotlin.reflect.*
import kotlin.reflect.full.*

// Get class reference
val clazz = MyClass::class

// Inspect class members
val properties = clazz.memberProperties
val functions = clazz.memberFunctions

// Create instance using no-arg constructor
val instance = clazz.createInstance()

// Call function dynamically
val function = clazz.functions.find { it.name == "doSomething" }
function?.call(instance)

// Access property value
val property = clazz.memberProperties.find { it.name == "value" }
val value = property?.get(instance)

// Get type information
val type = typeOf<List<String>>()
val classifier = type.classifier
val isNullable = type.isMarkedNullable

Architecture

Kotlin Reflect is organized into three main layers:

  • Core Interfaces (kotlin.reflect): Basic reflection contracts including KClass, KType, KCallable, KFunction, and KProperty
  • Full Reflection (kotlin.reflect.full): Enhanced capabilities providing comprehensive class introspection, member enumeration, type operations, and instance manipulation
  • JVM Bridge (kotlin.reflect.jvm): Seamless interoperability between Kotlin and Java reflection APIs

The library uses lazy evaluation and caching for performance, with all reflection operations providing full type safety where possible.

Capabilities

Core Reflection

Basic reflection operations for classes, types, and callables. Provides the foundational interfaces and operations for runtime introspection.

interface KClass<T : Any> : KDeclarationContainer, KAnnotatedElement, KClassifier {
  val simpleName: String?
  val qualifiedName: String?
  val constructors: Collection<KFunction<T>>
  val nestedClasses: Collection<KClass<*>>
  val objectInstance: T?
  val typeParameters: List<KTypeParameter>
  val supertypes: List<KType>
  val sealedSubclasses: List<KClass<out T>>
  val visibility: KVisibility?
  val isFinal: Boolean
  val isOpen: Boolean
  val isAbstract: Boolean
  val isSealed: Boolean
  val isData: Boolean
  val isInner: Boolean
  val isCompanion: Boolean
  val isFun: Boolean
  val isValue: Boolean
  fun isInstance(value: Any?): Boolean
}

interface KType {
  val classifier: KClassifier?
  val arguments: List<KTypeProjection>
  val isMarkedNullable: Boolean
}

interface KCallable<out R> : KAnnotatedElement {
  val name: String
  val parameters: List<KParameter>
  val returnType: KType
  val typeParameters: List<KTypeParameter>
  val visibility: KVisibility?
  val isFinal: Boolean
  val isOpen: Boolean
  val isAbstract: Boolean
  val isSuspend: Boolean
  fun call(vararg args: Any?): R
  fun callBy(args: Map<KParameter, Any?>): R
}

Core Reflection

Type System

Comprehensive type representation and manipulation including type creation, variance handling, and subtype relationships.

data class KTypeProjection(
  val variance: KVariance?,
  val type: KType?
) {
  companion object {
    val STAR: KTypeProjection
    fun invariant(type: KType): KTypeProjection
    fun contravariant(type: KType): KTypeProjection
    fun covariant(type: KType): KTypeProjection
  }
}

fun KClassifier.createType(
  arguments: List<KTypeProjection> = emptyList(),
  nullable: Boolean = false,
  annotations: List<Annotation> = emptyList()
): KType

Type System

Callable System

Function and property reflection with dynamic invocation capabilities, parameter inspection, and extension support.

interface KFunction<out R> : KCallable<R>, Function<R>

interface KProperty<out V> : KCallable<V>
interface KMutableProperty<V> : KProperty<V>

interface KProperty0<out V> : KProperty<V>, () -> V {
  fun get(): V
}

interface KMutableProperty0<V> : KProperty0<V>, KMutableProperty<V> {
  fun set(value: V)
}

Callable System

Class Introspection

Enhanced class inspection capabilities including member enumeration, hierarchy traversal, constructor access, and instance operations.

val <T : Any> KClass<T>.primaryConstructor: KFunction<T>?
val KClass<*>.companionObject: KClass<*>?
val KClass<*>.memberFunctions: Collection<KFunction<*>>
val <T : Any> KClass<T>.memberProperties: Collection<KProperty1<T, *>>

fun <T : Any> KClass<T>.createInstance(): T
fun <T : Any> KClass<T>.cast(value: Any?): T
fun KClass<*>.isSubclassOf(base: KClass<*>): Boolean

Class Introspection

JVM Interoperability

Seamless bridge between Kotlin and Java reflection APIs, providing access to Java Method, Field, and Constructor objects.

val KProperty<*>.javaField: Field?
val KProperty<*>.javaGetter: Method?
val KFunction<*>.javaMethod: Method?
val <T> KFunction<T>.javaConstructor: Constructor<T>?
val KType.javaType: Type
val KType.jvmErasure: KClass<*>

val Field.kotlinProperty: KProperty<*>?
val Method.kotlinFunction: KFunction<*>?

JVM Interoperability

Common Types

interface KClassifier

enum class KVariance {
  INVARIANT, IN, OUT
}

interface KTypeParameter : KClassifier

interface KParameter : KAnnotatedElement {
  val index: Int
  val name: String?
  val type: KType
  val kind: Kind
  val isOptional: Boolean
  val isVararg: Boolean
  
  enum class Kind {
    INSTANCE, EXTENSION_RECEIVER, VALUE, CONTEXT
  }
}

interface KAnnotatedElement {
  val annotations: List<Annotation>
}

interface KDeclarationContainer {
  val members: Collection<KCallable<*>>
}

enum class KVisibility {
  PUBLIC, PROTECTED, INTERNAL, PRIVATE
}