6

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

anton
  • 675
  • 6
  • 16
  • 2
    I don't think you can reference an internal class (in kotlin) since it is internal, but if it is not annotated with @JvmSynthetic you may be able to pull it from java side into kotlin by writing a boiler-plate to get a reflection. [Because in platform(JVM) the internal is public](https://stackoverflow.com/questions/45393423/kotlin-internal-classes-in-java-visible-publicly). – Animesh Sahu Jun 21 '20 at 15:19

1 Answers1

14

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.