kotlinreflectionkotlin-reflect

Is it possible to access internal class with reflection in Kotlin?


Is it possible to access fields of an internal class with reflection in Kotlin? I need to change the object of the third-party library's internal class.


Solution

  • You can use Suppress annotation in your class with following names: "INVISIBLE_MEMBER", "INVISIBLE_REFERENCE" like this:

    @file:Suppress("INVISIBLE_MEMBER", "INVISIBLE_REFERENCE")
    

    You can see an example here: https://github.com/Kotlin/kotlinx.coroutines/blob/master/kotlinx-coroutines-debug/src/DebugProbes.kt#L5

    In this class, an internal object DebugProbesImpl is accessed.