Kotlin Reflection (kotlin-reflect)
Inspecting and invoking Kotlin declarations at runtime through the KClass and KCallable hierarchy. It is powerful, it is slow, and it needs an extra artifact — three facts an interviewer expects together.
part ofKotlinoverview, primer and where to startread it →on this pageshowhide
explore
- KClass & ::class References5 questions
- KClass vs Java Class (.java/.kotlin)5 questions
- KCallable, KFunction & KProperty5 questions
- Callable References (::fn, ::prop)5 questions
- Calling Members Reflectively5 questions
- KType & typeOf()5 questions
- kotlin-reflect Dependency & Cost5 questions
- Kotlin vs Java Reflection5 questions
questions
page 2 of 2You're designing a generic serializer/registry API in Kotlin that interops with Java libraries. When would you accept/store a KClass<T> versus a java.lang.Class<T>, and how do you convert at the boundary?
basics
~10 sUse KClass in your Kotlin-facing API so callers write MyType::class and you can read Kotlin info; convert to .java at the exact point you call into Java libraries. Store whichever the consumers key on.
What is starProjectedType, and when would you use it instead of typeOf<T>()?
basics
~20 sstarProjectedType turns a KClass into a KType where every generic parameter is replaced by a star (*), meaning 'unknown'. You use it when you have a KClass at runtime but no concrete type arguments to fill in.
How is a Kotlin suspend function represented in bytecode, and how does kotlin-reflect expose it that java.lang.reflect cannot?
basics
~10 sA suspend function compiles to a normal method with an extra hidden Continuation parameter. Java reflection just sees that odd extra parameter; kotlin-reflect tells you it's actually suspend via isSuspend.
When would you deliberately use java.lang.reflect instead of kotlin-reflect in a Kotlin codebase, and how do you bridge between the two?
basics
~10 sUse Java reflection when you don't need Kotlin-specific info, want to avoid the extra kotlin-reflect dependency and startup cost, or interop with Java frameworks. Bridge with .java and .kotlin and the jvm extension functions.
Is `SomeClass::class.java.kotlin === SomeClass::class` guaranteed? What identity guarantees do the .java/.kotlin conversions give?
basics
~10 sRound-tripping a class through .java and back to .kotlin gives you an equal KClass, and comparing with == works. The conversions are cheap lookups, not new copies of your data.
When invoking reflectively, how do KParameter objects work — their kind, index, isOptional, isVararg — and how do you use them to build correct call/callBy invocations?
basics
~20 sEach parameter of a callable is a KParameter with a kind (instance receiver, extension receiver, or value), an index, a name, a type, and flags like isOptional and isVararg. You use these to know the right order for call() or to build the map for callBy().
You need to build a generic 'object to map' serializer using declaredMemberProperties. How do you read each property's value and type generically, and what pitfalls (visibility, isAccessible, nullability) must you handle?
basics
~10 sLoop over the class's declaredMemberProperties, call get(instance) on each to read the value, and use property.returnType for the type. Watch out for private properties (set isAccessible) and nullable values.
Explain `simpleName` vs `qualifiedName` on a KClass, when each can be null, and how they behave for nested, local, and anonymous classes.
basics
~10 ssimpleName is the short class name; qualifiedName is the full dotted name including package. Both are nullable: anonymous objects and local classes have no real name, so they return null.
Your service's hot path uses kotlin-reflect to map data objects, and it's both slow and bloating the deployable. As a senior/principal, how do you decide whether to keep reflection, cache it, or eliminate the dependency entirely?
basics
~20 sMeasure first. If reflection is only used a little, cache the resolved handles. If it's on a hot path or you're size-constrained, replace it with compile-time code generation so you can drop the library entirely.
Why do libraries like kotlinx.serialization accept a KType (often via typeOf<T>()) instead of a KClass, and what are the implications?
basics
~20 sA KClass forgets the generic arguments, so it can't tell List<String> from List<Int>. KType keeps them, letting the library pick the right serializer for each element. typeOf<T>() captures that full type at the call site.
showing 31–40 of 40