or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

Files

docs

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

core-reflection.mddocs/

0

# Core Reflection

1

2

Basic reflection operations for classes, types, and callables providing the foundational interfaces and operations for runtime introspection of Kotlin code.

3

4

## Capabilities

5

6

### Class References

7

8

Obtain and work with class references for runtime introspection.

9

10

```kotlin { .api }

11

/**

12

* Represents a class and provides introspection capabilities

13

* @param T the type of the class

14

*/

15

interface KClass<T : Any> : KClassifier {

16

/** Simple name of the class as declared in source, or null if anonymous */

17

val simpleName: String?

18

/** Fully qualified dot-separated name, or null if local/anonymous */

19

val qualifiedName: String?

20

/** Returns true if value is an instance of this class */

21

fun isInstance(value: Any?): Boolean

22

}

23

```

24

25

**Usage Examples:**

26

27

```kotlin

28

// Get class reference using ::class syntax

29

val stringClass = String::class

30

val myClass = MyClass::class

31

32

// Check class properties

33

println(stringClass.simpleName) // "String"

34

println(stringClass.qualifiedName) // "kotlin.String"

35

36

// Instance checking

37

val obj: Any = "hello"

38

println(stringClass.isInstance(obj)) // true

39

```

40

41

### Type References

42

43

Work with type information including generics, nullability, and type arguments.

44

45

```kotlin { .api }

46

/**

47

* Represents a type with optional type arguments and nullability

48

*/

49

interface KType {

50

/** The declaration of the classifier used in this type */

51

val classifier: KClassifier?

52

/** Type arguments passed for the parameters of the classifier */

53

val arguments: List<KTypeProjection>

54

/** True if this type was marked nullable in source code */

55

val isMarkedNullable: Boolean

56

}

57

58

/**

59

* Returns runtime representation of the given reified type as KType

60

*/

61

inline fun <reified T> typeOf(): KType

62

```

63

64

**Usage Examples:**

65

66

```kotlin

67

// Get type references

68

val stringType = typeOf<String>()

69

val listType = typeOf<List<String>>()

70

val nullableType = typeOf<String?>()

71

72

// Inspect type properties

73

println(listType.classifier) // class kotlin.collections.List

74

println(listType.arguments.size) // 1

75

println(nullableType.isMarkedNullable) // true

76

77

// Access type arguments

78

val elementType = listType.arguments.first().type

79

println(elementType?.classifier) // class kotlin.String

80

```

81

82

### Callable References

83

84

Access and inspect functions and properties as callable entities.

85

86

```kotlin { .api }

87

/**

88

* Represents a callable entity, such as a function or property

89

* @param R return type of the callable

90

*/

91

interface KCallable<out R> {

92

/** The name of this callable as declared in source code */

93

val name: String

94

}

95

96

/**

97

* Represents a function with introspection capabilities

98

*/

99

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

100

101

/**

102

* Represents a property, such as a named val or var declaration

103

* @param V the type of the property value

104

*/

105

interface KProperty<out V> : KCallable<V>

106

```

107

108

**Usage Examples:**

109

110

```kotlin

111

class Example {

112

val property: String = "value"

113

fun method(x: Int): String = x.toString()

114

}

115

116

// Get callable references

117

val propertyRef = Example::property

118

val methodRef = Example::method

119

120

// Access callable information

121

println(propertyRef.name) // "property"

122

println(methodRef.name) // "method"

123

124

// Check callable types

125

println(propertyRef is KProperty<*>) // true

126

println(methodRef is KFunction<*>) // true

127

```

128

129

### Classifier Types

130

131

Work with type classifiers including classes and type parameters.

132

133

```kotlin { .api }

134

/**

135

* A classifier is either a class or a type parameter

136

*/

137

interface KClassifier

138

139

/**

140

* Represents a type parameter of a class or function

141

*/

142

interface KTypeParameter : KClassifier {

143

val name: String

144

val upperBounds: List<KType>

145

val variance: KVariance

146

val isReified: Boolean

147

}

148

```

149

150

### Basic Type Operations

151

152

Perform fundamental type and class casting operations.

153

154

```kotlin { .api }

155

/**

156

* Casts the given value to the class represented by this KClass

157

* Throws exception if value is null or not an instance

158

*/

159

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

160

161

/**

162

* Casts the given value to the class represented by this KClass

163

* Returns null if value is null or not an instance

164

*/

165

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

166

```

167

168

**Usage Examples:**

169

170

```kotlin

171

val stringClass = String::class

172

val anyValue: Any = "hello"

173

174

// Safe casting

175

val result = stringClass.safeCast(anyValue)

176

println(result) // "hello"

177

178

// Exception throwing cast

179

try {

180

val number = stringClass.cast(123) // throws ClassCastException

181

} catch (e: ClassCastException) {

182

println("Cast failed: ${e.message}")

183

}

184

```