ensureAccessible
Ensures that a Java reflection AccessibleObject (like Method, Field, Constructor) is accessible. This method sets the accessible flag to true if it's not already set, allowing access to private, protected, or package-private members.
This is a convenience method that safely handles the accessibility check and setting.
Example usage:
val method = MyClass::class.java.getDeclaredMethod("privateMethod")
method.ensureAccessible() // Now accessible even if private
method.invoke(instance)Content copied to clipboard
Ensures that a Kotlin reflection KCallable (like KFunction, KProperty) is accessible. This method sets the accessible flag to true if it's not already set, allowing access to private, protected, or package-private members.
This is a convenience method that safely handles the accessibility check and setting.
Example usage:
val property = MyClass::class.members.find { it.name == "privateProperty" } as KProperty1<MyClass, *>
property.ensureAccessible() // Now accessible even if private
val value = property.get(instance)Content copied to clipboard