Runtime component for full Kotlin reflection support, enabling inspection and manipulation of Kotlin classes, functions, properties, and types at runtime
npx @tessl/cli install tessl/maven-org-jetbrains-kotlin--kotlin-reflect@2.2.00
# Kotlin Reflect
1
2
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.
3
4
## Package Information
5
6
- **Package Name**: kotlin-reflect
7
- **Package Type**: maven
8
- **Language**: Kotlin
9
- **Installation**: `implementation("org.jetbrains.kotlin:kotlin-reflect:$kotlin_version")` (should match your Kotlin version)
10
11
## Core Imports
12
13
```kotlin
14
import kotlin.reflect.*
15
import kotlin.reflect.full.*
16
import kotlin.reflect.jvm.*
17
```
18
19
## Basic Usage
20
21
```kotlin
22
import kotlin.reflect.*
23
import kotlin.reflect.full.*
24
25
// Get class reference
26
val clazz = MyClass::class
27
28
// Inspect class members
29
val properties = clazz.memberProperties
30
val functions = clazz.memberFunctions
31
32
// Create instance using no-arg constructor
33
val instance = clazz.createInstance()
34
35
// Call function dynamically
36
val function = clazz.functions.find { it.name == "doSomething" }
37
function?.call(instance)
38
39
// Access property value
40
val property = clazz.memberProperties.find { it.name == "value" }
41
val value = property?.get(instance)
42
43
// Get type information
44
val type = typeOf<List<String>>()
45
val classifier = type.classifier
46
val isNullable = type.isMarkedNullable
47
```
48
49
## Architecture
50
51
Kotlin Reflect is organized into three main layers:
52
53
- **Core Interfaces** (`kotlin.reflect`): Basic reflection contracts including KClass, KType, KCallable, KFunction, and KProperty
54
- **Full Reflection** (`kotlin.reflect.full`): Enhanced capabilities providing comprehensive class introspection, member enumeration, type operations, and instance manipulation
55
- **JVM Bridge** (`kotlin.reflect.jvm`): Seamless interoperability between Kotlin and Java reflection APIs
56
57
The library uses lazy evaluation and caching for performance, with all reflection operations providing full type safety where possible.
58
59
## Capabilities
60
61
### Core Reflection
62
63
Basic reflection operations for classes, types, and callables. Provides the foundational interfaces and operations for runtime introspection.
64
65
```kotlin { .api }
66
interface KClass<T : Any> : KDeclarationContainer, KAnnotatedElement, KClassifier {
67
val simpleName: String?
68
val qualifiedName: String?
69
val constructors: Collection<KFunction<T>>
70
val nestedClasses: Collection<KClass<*>>
71
val objectInstance: T?
72
val typeParameters: List<KTypeParameter>
73
val supertypes: List<KType>
74
val sealedSubclasses: List<KClass<out T>>
75
val visibility: KVisibility?
76
val isFinal: Boolean
77
val isOpen: Boolean
78
val isAbstract: Boolean
79
val isSealed: Boolean
80
val isData: Boolean
81
val isInner: Boolean
82
val isCompanion: Boolean
83
val isFun: Boolean
84
val isValue: Boolean
85
fun isInstance(value: Any?): Boolean
86
}
87
88
interface KType {
89
val classifier: KClassifier?
90
val arguments: List<KTypeProjection>
91
val isMarkedNullable: Boolean
92
}
93
94
interface KCallable<out R> : KAnnotatedElement {
95
val name: String
96
val parameters: List<KParameter>
97
val returnType: KType
98
val typeParameters: List<KTypeParameter>
99
val visibility: KVisibility?
100
val isFinal: Boolean
101
val isOpen: Boolean
102
val isAbstract: Boolean
103
val isSuspend: Boolean
104
fun call(vararg args: Any?): R
105
fun callBy(args: Map<KParameter, Any?>): R
106
}
107
```
108
109
[Core Reflection](./core-reflection.md)
110
111
### Type System
112
113
Comprehensive type representation and manipulation including type creation, variance handling, and subtype relationships.
114
115
```kotlin { .api }
116
data class KTypeProjection(
117
val variance: KVariance?,
118
val type: KType?
119
) {
120
companion object {
121
val STAR: KTypeProjection
122
fun invariant(type: KType): KTypeProjection
123
fun contravariant(type: KType): KTypeProjection
124
fun covariant(type: KType): KTypeProjection
125
}
126
}
127
128
fun KClassifier.createType(
129
arguments: List<KTypeProjection> = emptyList(),
130
nullable: Boolean = false,
131
annotations: List<Annotation> = emptyList()
132
): KType
133
```
134
135
[Type System](./type-system.md)
136
137
### Callable System
138
139
Function and property reflection with dynamic invocation capabilities, parameter inspection, and extension support.
140
141
```kotlin { .api }
142
interface KFunction<out R> : KCallable<R>, Function<R>
143
144
interface KProperty<out V> : KCallable<V>
145
interface KMutableProperty<V> : KProperty<V>
146
147
interface KProperty0<out V> : KProperty<V>, () -> V {
148
fun get(): V
149
}
150
151
interface KMutableProperty0<V> : KProperty0<V>, KMutableProperty<V> {
152
fun set(value: V)
153
}
154
```
155
156
[Callable System](./callable-system.md)
157
158
### Class Introspection
159
160
Enhanced class inspection capabilities including member enumeration, hierarchy traversal, constructor access, and instance operations.
161
162
```kotlin { .api }
163
val <T : Any> KClass<T>.primaryConstructor: KFunction<T>?
164
val KClass<*>.companionObject: KClass<*>?
165
val KClass<*>.memberFunctions: Collection<KFunction<*>>
166
val <T : Any> KClass<T>.memberProperties: Collection<KProperty1<T, *>>
167
168
fun <T : Any> KClass<T>.createInstance(): T
169
fun <T : Any> KClass<T>.cast(value: Any?): T
170
fun KClass<*>.isSubclassOf(base: KClass<*>): Boolean
171
```
172
173
[Class Introspection](./class-introspection.md)
174
175
### JVM Interoperability
176
177
Seamless bridge between Kotlin and Java reflection APIs, providing access to Java Method, Field, and Constructor objects.
178
179
```kotlin { .api }
180
val KProperty<*>.javaField: Field?
181
val KProperty<*>.javaGetter: Method?
182
val KFunction<*>.javaMethod: Method?
183
val <T> KFunction<T>.javaConstructor: Constructor<T>?
184
val KType.javaType: Type
185
val KType.jvmErasure: KClass<*>
186
187
val Field.kotlinProperty: KProperty<*>?
188
val Method.kotlinFunction: KFunction<*>?
189
```
190
191
[JVM Interoperability](./jvm-interoperability.md)
192
193
## Common Types
194
195
```kotlin { .api }
196
interface KClassifier
197
198
enum class KVariance {
199
INVARIANT, IN, OUT
200
}
201
202
interface KTypeParameter : KClassifier
203
204
interface KParameter : KAnnotatedElement {
205
val index: Int
206
val name: String?
207
val type: KType
208
val kind: Kind
209
val isOptional: Boolean
210
val isVararg: Boolean
211
212
enum class Kind {
213
INSTANCE, EXTENSION_RECEIVER, VALUE, CONTEXT
214
}
215
}
216
217
interface KAnnotatedElement {
218
val annotations: List<Annotation>
219
}
220
221
interface KDeclarationContainer {
222
val members: Collection<KCallable<*>>
223
}
224
225
enum class KVisibility {
226
PUBLIC, PROTECTED, INTERNAL, PRIVATE
227
}
228
```