skip to content

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 pageshow

explore

questions

page 2 of 2

You'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?

level: seniorimportance: should knowfreq 25%

basics

~10 s

Use 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.

open as a page

What is starProjectedType, and when would you use it instead of typeOf<T>()?

level: seniorimportance: should knowfreq 28%

basics

~20 s

starProjectedType 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.

open as a page

How is a Kotlin suspend function represented in bytecode, and how does kotlin-reflect expose it that java.lang.reflect cannot?

level: seniorimportance: should knowfreq 30%

basics

~10 s

A 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.

open as a page

When would you deliberately use java.lang.reflect instead of kotlin-reflect in a Kotlin codebase, and how do you bridge between the two?

level: principalimportance: should knowfreq 28%

basics

~10 s

Use 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.

open as a page

Is `SomeClass::class.java.kotlin === SomeClass::class` guaranteed? What identity guarantees do the .java/.kotlin conversions give?

level: middleimportance: nice to knowfreq 20%

basics

~10 s

Round-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.

open as a page

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?

level: seniorimportance: nice to knowfreq 25%

basics

~20 s

Each 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().

open as a page

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?

level: seniorimportance: nice to knowfreq 22%

basics

~10 s

Loop 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.

open as a page

Explain `simpleName` vs `qualifiedName` on a KClass, when each can be null, and how they behave for nested, local, and anonymous classes.

level: seniorimportance: nice to knowfreq 30%

basics

~10 s

simpleName 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.

open as a page

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?

level: principalimportance: nice to knowfreq 22%

basics

~20 s

Measure 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.

open as a page

Why do libraries like kotlinx.serialization accept a KType (often via typeOf<T>()) instead of a KClass, and what are the implications?

level: principalimportance: nice to knowfreq 22%

basics

~20 s

A 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.

open as a page

showing 31–40 of 40