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

class-introspection.mddocs/

0

# Class Introspection

1

2

Enhanced class inspection capabilities including member enumeration, hierarchy traversal, constructor access, companion object handling, and instance operations for comprehensive class analysis.

3

4

## Capabilities

5

6

### Member Enumeration

7

8

Access all members of a class including functions, properties, and constructors with various filtering options.

9

10

```kotlin { .api }

11

/** All functions and properties declared in this class, excluding inherited */

12

val KClass<*>.declaredMembers: Collection<KCallable<*>>

13

14

/** All functions and properties including inherited from supertypes */

15

val KClass<*>.members: Collection<KCallable<*>>

16

17

/** All constructors of this class */

18

val <T : Any> KClass<T>.constructors: Collection<KFunction<T>>

19

20

/** Primary constructor of this class, or null if none */

21

val <T : Any> KClass<T>.primaryConstructor: KFunction<T>?

22

```

23

24

**Usage Examples:**

25

26

```kotlin

27

open class Parent {

28

val parentProperty: String = "parent"

29

fun parentMethod(): String = "parent"

30

}

31

32

class Child : Parent() {

33

val childProperty: Int = 42

34

fun childMethod(): Int = 42

35

36

constructor() : super()

37

constructor(value: Int) : super()

38

}

39

40

val childClass = Child::class

41

42

// Get all members (including inherited)

43

val allMembers = childClass.members

44

println("Total members: ${allMembers.size}")

45

46

// Get only declared members

47

val declaredMembers = childClass.declaredMembers

48

println("Declared members: ${declaredMembers.size}")

49

50

// Get constructors

51

val constructors = childClass.constructors

52

println("Constructors: ${constructors.size}") // 2

53

54

val primaryConstructor = childClass.primaryConstructor

55

println("Has primary constructor: ${primaryConstructor != null}")

56

```

57

58

### Function Access

59

60

Access functions with different visibility and extension patterns.

61

62

```kotlin { .api }

63

/** All functions including inherited and static */

64

val KClass<*>.functions: Collection<KFunction<*>>

65

66

/** Non-extension non-static functions declared in class and supertypes */

67

val KClass<*>.memberFunctions: Collection<KFunction<*>>

68

69

/** Extension functions declared in class and supertypes */

70

val KClass<*>.memberExtensionFunctions: Collection<KFunction<*>>

71

72

/** Static functions declared in this class */

73

val KClass<*>.staticFunctions: Collection<KFunction<*>>

74

75

/** All functions declared only in this class */

76

val KClass<*>.declaredFunctions: Collection<KFunction<*>>

77

78

/** Non-extension non-static functions declared only in this class */

79

val KClass<*>.declaredMemberFunctions: Collection<KFunction<*>>

80

81

/** Extension functions declared only in this class */

82

val KClass<*>.declaredMemberExtensionFunctions: Collection<KFunction<*>>

83

```

84

85

**Usage Examples:**

86

87

```kotlin

88

class MathUtils {

89

fun add(a: Int, b: Int): Int = a + b

90

91

companion object {

92

@JvmStatic

93

fun multiply(a: Int, b: Int): Int = a * b

94

}

95

}

96

97

fun MathUtils.subtract(a: Int, b: Int): Int = a - b

98

99

val mathClass = MathUtils::class

100

101

// Get different types of functions

102

val allFunctions = mathClass.functions

103

val memberFunctions = mathClass.memberFunctions

104

val staticFunctions = mathClass.staticFunctions

105

val declaredFunctions = mathClass.declaredFunctions

106

107

println("All functions: ${allFunctions.size}")

108

println("Member functions: ${memberFunctions.size}")

109

println("Static functions: ${staticFunctions.size}")

110

println("Declared functions: ${declaredFunctions.size}")

111

112

// Find specific functions

113

val addFunction = memberFunctions.find { it.name == "add" }

114

val multiplyFunction = staticFunctions.find { it.name == "multiply" }

115

116

println("Found add function: ${addFunction != null}")

117

println("Found multiply function: ${multiplyFunction != null}")

118

```

119

120

### Property Access

121

122

Access properties with different receiver patterns and mutability.

123

124

```kotlin { .api }

125

/** Non-extension properties declared in class and supertypes */

126

val <T : Any> KClass<T>.memberProperties: Collection<KProperty1<T, *>>

127

128

/** Extension properties declared in class and supertypes */

129

val <T : Any> KClass<T>.memberExtensionProperties: Collection<KProperty2<T, *, *>>

130

131

/** Static properties declared in this class */

132

val KClass<*>.staticProperties: Collection<KProperty0<*>>

133

134

/** Non-extension properties declared only in this class */

135

val <T : Any> KClass<T>.declaredMemberProperties: Collection<KProperty1<T, *>>

136

137

/** Extension properties declared only in this class */

138

val <T : Any> KClass<T>.declaredMemberExtensionProperties: Collection<KProperty2<T, *, *>>

139

```

140

141

**Usage Examples:**

142

143

```kotlin

144

class Person(val name: String) {

145

var age: Int = 0

146

147

companion object {

148

const val SPECIES = "Homo sapiens"

149

}

150

}

151

152

val Person.displayName: String

153

get() = "Person: $name"

154

155

val personClass = Person::class

156

157

// Get different types of properties

158

val memberProperties = personClass.memberProperties

159

val staticProperties = personClass.staticProperties

160

val declaredProperties = personClass.declaredMemberProperties

161

162

memberProperties.forEach { property ->

163

println("Property: ${property.name}, type: ${property.returnType}")

164

}

165

// Property: name, type: kotlin.String

166

// Property: age, type: kotlin.Int

167

168

staticProperties.forEach { property ->

169

println("Static property: ${property.name}")

170

}

171

// Static property: SPECIES

172

173

// Check property mutability

174

val nameProperty = memberProperties.find { it.name == "name" }

175

val ageProperty = memberProperties.find { it.name == "age" }

176

177

println("name is mutable: ${nameProperty is KMutableProperty1<*, *>}") // false

178

println("age is mutable: ${ageProperty is KMutableProperty1<*, *>}") // true

179

```

180

181

### Class Hierarchy

182

183

Navigate class inheritance hierarchies and inspect supertype relationships.

184

185

```kotlin { .api }

186

/** Immediate superclasses of this class in source order */

187

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

188

189

/** Immediate supertypes of this class including type arguments */

190

val KClass<*>.supertypes: Collection<KType>

191

192

/** All superclasses including indirect ones in no particular order */

193

val KClass<*>.allSuperclasses: Collection<KClass<*>>

194

195

/** All supertypes including indirect ones */

196

val KClass<*>.allSupertypes: Collection<KType>

197

198

/** Returns true if this class is the same or subclass of base */

199

fun KClass<*>.isSubclassOf(base: KClass<*>): Boolean

200

201

/** Returns true if this class is the same or superclass of derived */

202

fun KClass<*>.isSuperclassOf(derived: KClass<*>): Boolean

203

```

204

205

**Usage Examples:**

206

207

```kotlin

208

interface Drawable {

209

fun draw()

210

}

211

212

open class Shape : Drawable {

213

override fun draw() {}

214

}

215

216

class Circle : Shape()

217

218

val circleClass = Circle::class

219

val shapeClass = Shape::class

220

val drawableClass = Drawable::class

221

222

// Inspect immediate hierarchy

223

val superclasses = circleClass.superclasses

224

println("Immediate superclasses: ${superclasses.map { it.simpleName }}")

225

// [Shape]

226

227

val supertypes = circleClass.supertypes

228

println("Immediate supertypes: ${supertypes.size}") // 1

229

230

// Inspect complete hierarchy

231

val allSuperclasses = circleClass.allSuperclasses

232

println("All superclasses: ${allSuperclasses.map { it.simpleName }}")

233

// [Shape, Drawable, Any]

234

235

// Check relationships

236

println("Circle is subclass of Shape: ${circleClass.isSubclassOf(shapeClass)}") // true

237

println("Circle is subclass of Drawable: ${circleClass.isSubclassOf(drawableClass)}") // true

238

println("Shape is superclass of Circle: ${shapeClass.isSuperclassOf(circleClass)}") // true

239

```

240

241

### Companion Objects

242

243

Access companion objects and their instances.

244

245

```kotlin { .api }

246

/** Returns KClass representing the companion object, or null if none */

247

val KClass<*>.companionObject: KClass<*>?

248

249

/** Returns instance of the companion object, or null if none */

250

val KClass<*>.companionObjectInstance: Any?

251

```

252

253

**Usage Examples:**

254

255

```kotlin

256

class MyClass {

257

companion object {

258

const val CONSTANT = "value"

259

fun companionFunction(): String = "companion"

260

}

261

}

262

263

val myClass = MyClass::class

264

265

// Access companion object

266

val companionClass = myClass.companionObject

267

val companionInstance = myClass.companionObjectInstance

268

269

println("Has companion: ${companionClass != null}") // true

270

271

if (companionClass != null && companionInstance != null) {

272

// Get companion members

273

val companionMembers = companionClass.memberFunctions

274

val companionFunction = companionMembers.find { it.name == "companionFunction" }

275

276

// Call companion function

277

val result = companionFunction?.call(companionInstance)

278

println("Companion function result: $result") // "companion"

279

}

280

```

281

282

### Instance Operations

283

284

Create instances and perform type casting operations.

285

286

```kotlin { .api }

287

/**

288

* Creates a new instance using a constructor with no parameters

289

* or all optional parameters

290

*/

291

fun <T : Any> KClass<T>.createInstance(): T

292

293

/**

294

* Casts the given value to this class type

295

* Throws exception if value is not an instance

296

*/

297

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

298

299

/**

300

* Safely casts the given value to this class type

301

* Returns null if value is not an instance

302

*/

303

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

304

```

305

306

**Usage Examples:**

307

308

```kotlin

309

class Person(val name: String = "Unknown", val age: Int = 0)

310

311

val personClass = Person::class

312

313

// Create instance using no-arg constructor

314

val person = personClass.createInstance()

315

println("Created person: ${person.name}, ${person.age}") // "Unknown", 0

316

317

// Type casting

318

val anyValue: Any = Person("Alice", 25)

319

val stringValue: Any = "not a person"

320

321

// Safe casting

322

val castedPerson = personClass.safeCast(anyValue)

323

val failedCast = personClass.safeCast(stringValue)

324

325

println("Safe cast result: ${castedPerson?.name}") // "Alice"

326

println("Failed cast result: $failedCast") // null

327

328

// Exception throwing cast

329

try {

330

val forceCast = personClass.cast(stringValue)

331

} catch (e: ClassCastException) {

332

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

333

}

334

```

335

336

### Type Information

337

338

Access detailed type information about classes including their generic type structure.

339

340

```kotlin { .api }

341

/** Returns type with star projections for all type parameters */

342

val KClassifier.starProjectedType: KType

343

344

/** Type parameters declared by this class */

345

val KClass<*>.typeParameters: List<KTypeParameter>

346

```

347

348

**Usage Examples:**

349

350

```kotlin

351

class GenericClass<T, U : Number> {

352

fun method(param: T): U? = null

353

}

354

355

val genericClass = GenericClass::class

356

357

// Get type parameters

358

val typeParams = genericClass.typeParameters

359

println("Type parameters: ${typeParams.size}") // 2

360

361

typeParams.forEachIndexed { index, param ->

362

println("Parameter $index: ${param.name}, bounds: ${param.upperBounds}")

363

}

364

// Parameter 0: T, bounds: [kotlin.Any?]

365

// Parameter 1: U, bounds: [kotlin.Number]

366

367

// Get star projected type

368

val starProjectedType = genericClass.starProjectedType

369

println("Star projected type: $starProjectedType") // GenericClass<*, *>

370

```