Is it possiable to use reflection to access a object's private field and call a public methods on this field?
i.e
class Hello {
private World word
}
class World {
public BlaBlaBla foo()
}
Hello h = new Hello()
World world = reflect on the h
// And then
world.foo()
It’s possible to make
private
fieldsaccessible
using reflection. The following examples (both written in Kotlin) show it...Using Java Reflection:
Using Kotlin Reflection:
Two useful extension functions for Kotlin:
Using those extension functions we can access private properties and functions of a class. Example:
To use reflection in Kotlin add dependency: