kotlinkotlin-reflect

How do I see if a class has an init block using reflection?


If I have a Kotlin class like the following:

data class Foo(
    val count: AtomicInteger
) {
    init {
        count.incrementAndGet()
    }
}

How do I determine that this class has an init block using reflection?

The init block does not appear in Foo::class.members and I can't see a way of determining it from Foo::class.constructors.

Is there any way to find out information about the init block using kotlin.reflect?


Solution

  • This information is not available after compilation.

    Init blocks and property initializers on JVM are compiled into some mixture of constructors and/or field initializers. It’s up to the compiler how it wants to do this in an optimal way.

    Answer courtesy of Tenfour04