visit

fun <T : Any> KClass<T>.visit(visitor: ClassVisitor<T, *>)

Visits all metadata elements of a Kotlin class using the provided visitor. The visit follows a structured pattern: start, visit types, constructors, properties, functions, and end. This ensures consistent traversal order.

Parameters

visitor

the visitor that will process each class element

class MyVisitor : ClassVisitor<MyClass, Unit> {
override fun start() { println("Starting visit") }
override fun visitType(type: KType) { println("Type: $type") }
override fun visitConstructor(constructor: KFunction<MyClass>) { println("Constructor: $constructor") }
override fun visitProperty(property: KProperty1<MyClass, *>) { println("Property: $property") }
override fun visitFunction(function: KFunction<*>) { println("Function: $function") }
override fun end() { println("Visit complete") }
}

MyClass::class.visit(MyVisitor())

Type Parameters

T

the type of the class being visited